generated from spatie/package-skeleton-laravel
-
-
Notifications
You must be signed in to change notification settings - Fork 210
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #669 from spatie/data-for-livewire
laravel-data & Livewire
- Loading branch information
Showing
23 changed files
with
766 additions
and
10 deletions.
There are no files selected for viewing
This file contains 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
This file contains 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
This file contains 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
This file contains 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
This file contains 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
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
<?php | ||
|
||
namespace Spatie\LaravelData\Support\Lazy; | ||
|
||
use Exception; | ||
use Spatie\LaravelData\Lazy; | ||
|
||
class LivewireLostLazy extends Lazy | ||
{ | ||
public function __construct( | ||
public string $dataClass, | ||
public string $propertyName | ||
) { | ||
} | ||
|
||
public function resolve(): mixed | ||
{ | ||
return throw new Exception("Lazy property `{$this->dataClass}::{$this->propertyName}` was lost when the data object was transformed to be used by Livewire. You can include the property and then the correct value will be set when creating the data object from Livewire again."); | ||
} | ||
} |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
<?php | ||
|
||
namespace Spatie\LaravelData\Support\Livewire; | ||
|
||
use Livewire\Mechanisms\HandleComponents\ComponentContext; | ||
use Livewire\Mechanisms\HandleComponents\Synthesizers\Synth; | ||
use Spatie\LaravelData\Contracts\BaseData; | ||
use Spatie\LaravelData\DataCollection; | ||
use Spatie\LaravelData\Support\DataConfig; | ||
|
||
class LivewireDataCollectionSynth extends Synth | ||
{ | ||
protected DataConfig $dataConfig; | ||
|
||
public static string $key = 'ldco'; | ||
|
||
public function __construct(ComponentContext $context, $path) | ||
{ | ||
$this->dataConfig = app(DataConfig::class); | ||
|
||
parent::__construct($context, $path); | ||
} | ||
|
||
public static function match($target): bool | ||
{ | ||
return is_a($target, DataCollection::class, true); | ||
} | ||
|
||
public function get(&$target, $key): BaseData | ||
{ | ||
return $target[$key]; | ||
} | ||
|
||
public function set(&$target, $key, $value) | ||
{ | ||
$target[$key] = $value; | ||
} | ||
|
||
/** | ||
* @param callable(array-key, mixed):mixed $dehydrateChild | ||
*/ | ||
public function dehydrate(DataCollection $target, callable $dehydrateChild): array | ||
{ | ||
$morph = $this->dataConfig->morphMap->getDataClassAlias($target->dataClass) ?? $target->dataClass; | ||
|
||
$payload = []; | ||
|
||
foreach ($target->toCollection() as $key => $child) { | ||
$payload[$key] = $dehydrateChild($key, $child); | ||
} | ||
|
||
return [ | ||
$payload, | ||
[ | ||
'dataCollectionClass' => $target::class, | ||
'dataMorph' => $morph, | ||
'context' => encrypt($target->getDataContext()), | ||
], | ||
]; | ||
} | ||
|
||
/** | ||
* @param callable(array-key, mixed):mixed $hydrateChild | ||
*/ | ||
public function hydrate($value, $meta, $hydrateChild) | ||
{ | ||
$context = decrypt($meta['context']); | ||
$dataCollectionClass = $meta['dataCollectionClass']; | ||
$dataClass = $this->dataConfig->morphMap->getMorphedDataClass($meta['dataMorph']) ?? $meta['dataMorph']; | ||
|
||
foreach ($value as $key => $child) { | ||
$value[$key] = $hydrateChild($key, $child); | ||
} | ||
|
||
/** @var DataCollection $dataCollection */ | ||
$dataCollection = new $dataCollectionClass($dataClass, $value); | ||
|
||
$dataCollection->setDataContext($context); | ||
|
||
return $dataCollection; | ||
} | ||
} |
Oops, something went wrong.