Created
August 21, 2023 19:41
-
-
Save ejonasson/0e437179fa20e5311073e7ef266e8b02 to your computer and use it in GitHub Desktop.
Spatie Data Synth
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace App\Shared\DataObjects; | |
use Livewire\Features\SupportWireables\WireableSynth; | |
use Spatie\LaravelData\Contracts\DataObject; | |
use Spatie\LaravelData\DataCollection; | |
use Spatie\LaravelData\Resolvers\DataFromSomethingResolver; | |
class DataObjectSynth extends WireableSynth | |
{ | |
public static $key = 'spatie_data'; | |
public static function match($target) | |
{ | |
return $target instanceof DataObject || $target instanceof DataCollection; | |
} | |
public function get(&$target, $key) | |
{ | |
return data_get($target, $key); | |
} | |
public function set(&$target, $key, $value) | |
{ | |
data_set($target, $key, $value); | |
} | |
public function dehydrate($target, $dehydrateChild) : array | |
{ | |
return [$target->toArray(), ['class' => get_class($target)]]; | |
} | |
function hydrate($value, $meta, $hydrateChild) { | |
return app(DataFromSomethingResolver::class) | |
->ignoreMagicalMethods('fromLivewire') | |
->execute($meta['class'], $value); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment