From 1e303244728347952dd7bd5e3e9841b6d9fc0c8f Mon Sep 17 00:00:00 2001 From: edalzell Date: Fri, 13 Oct 2023 15:40:56 -0700 Subject: [PATCH 1/3] Add DataCollectionSynth --- src/Synths/DataCollectionSynth.php | 36 ++++++++++++++++++++++++++++++ 1 file changed, 36 insertions(+) create mode 100644 src/Synths/DataCollectionSynth.php diff --git a/src/Synths/DataCollectionSynth.php b/src/Synths/DataCollectionSynth.php new file mode 100644 index 000000000..2adfa80b2 --- /dev/null +++ b/src/Synths/DataCollectionSynth.php @@ -0,0 +1,36 @@ +toArray(), ['class' => $target->dataClass]]; + } + + public function hydrate($value, $meta) + { + return new DataCollection($meta['class'], $value); + } + + public function get(&$target, $key) + { + return $target[$key] ?? null; + } + + public function set(&$target, $key, $value) + { + $target[$key] = $value; + } +} From 173a546363dda4446096d782e23e386b447d649c Mon Sep 17 00:00:00 2001 From: edalzell Date: Wed, 13 Dec 2023 15:15:33 -0800 Subject: [PATCH 2/3] deydrate recursively --- src/Synths/DataCollectionSynth.php | 13 +++++++++++-- 1 file changed, 11 insertions(+), 2 deletions(-) diff --git a/src/Synths/DataCollectionSynth.php b/src/Synths/DataCollectionSynth.php index 2adfa80b2..50a1a69a9 100644 --- a/src/Synths/DataCollectionSynth.php +++ b/src/Synths/DataCollectionSynth.php @@ -14,9 +14,18 @@ public static function match($target) return get_class($target) == DataCollection::class; } - public function dehydrate(DataCollection $target) + public function dehydrate(DataCollection $target, $dehydrateChild) { - return [$target->toArray(), ['class' => $target->dataClass]]; + $data = $target->all(); + + foreach ($data as $key => $child) { + $data[$key] = $dehydrateChild($key, $child); + } + + return [ + $data, + ['class' => get_class($target)], + ]; } public function hydrate($value, $meta) From d0296a207db89a4976208a3a6e74b84da8485356 Mon Sep 17 00:00:00 2001 From: edalzell Date: Wed, 13 Dec 2023 16:55:27 -0800 Subject: [PATCH 3/3] same with hydrate --- src/Synths/DataCollectionSynth.php | 14 ++++++++++++-- 1 file changed, 12 insertions(+), 2 deletions(-) diff --git a/src/Synths/DataCollectionSynth.php b/src/Synths/DataCollectionSynth.php index 50a1a69a9..c4591eb58 100644 --- a/src/Synths/DataCollectionSynth.php +++ b/src/Synths/DataCollectionSynth.php @@ -28,9 +28,19 @@ public function dehydrate(DataCollection $target, $dehydrateChild) ]; } - public function hydrate($value, $meta) + /** + * @param array $value + * @param array $meta + * @param mixed $hydrateChild + * @return \Spatie\LaravelData\DataCollection + */ + public function hydrate($value, $meta, $hydrateChild) { - return new DataCollection($meta['class'], $value); + foreach ($value as $key => $child) { + $value[$key] = $hydrateChild($key, $child); + } + + return $meta['class']::make($value); } public function get(&$target, $key)