diff --git a/src/SettingsCasts/ArrayDataCast.php b/src/SettingsCasts/ArrayDataCast.php new file mode 100644 index 0000000..dc0c86a --- /dev/null +++ b/src/SettingsCasts/ArrayDataCast.php @@ -0,0 +1,71 @@ +type = $this->ensureDataTypeExists($type); + $this->validate = !in_array($validate, ['false', '0'], true); + } + + /** + * @param array $payload + * + * @return Data[] + */ + public function get($payload): array + { + return array_map( + fn ($data) => $this->createData($data), + $payload + ); + } + + /** + * @param array $payload + * + * @return array + */ + public function set($payload): array + { + return array_map( + fn ($data) => $this->createData($data)->toArray(), + $payload + ); + } + + /** + * @param array|Data $data + */ + protected function createData($data): Data + { + return $this->validate ? $this->type::validateAndCreate($data) : $this->type::from($data); + } + + protected function ensureDataTypeExists(?string $type): string + { + if ($type === null) { + throw new Exception('Cannot create a data cast because no data class was given'); + } + + if (!class_exists($type)) { + throw new Exception("Cannot create a data cast for `$type` because the data does not exist"); + } + + if (!class_implements($type, Data::class)) { + throw new Exception("Cannot create a data cast for `$type` because the class does not implement data"); + } + + return $type; + } +}