generated from spatie/package-skeleton-laravel
-
-
Notifications
You must be signed in to change notification settings - Fork 217
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix an issue where anonymous classes in castables were serialized
- Loading branch information
1 parent
30242c4
commit 088b4fd
Showing
4 changed files
with
80 additions
and
2 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
<?php | ||
|
||
namespace Spatie\LaravelData\Casts; | ||
|
||
use Spatie\LaravelData\Support\Creation\CreationContext; | ||
use Spatie\LaravelData\Support\DataProperty; | ||
|
||
class CastableCast implements Cast | ||
{ | ||
protected Cast $cast; | ||
|
||
/** | ||
* @param class-string<\Spatie\LaravelData\Casts\Castable> $castableClass | ||
*/ | ||
public function __construct( | ||
public string $castableClass, | ||
public array $arguments | ||
) { | ||
} | ||
|
||
public function cast(DataProperty $property, mixed $value, array $properties, CreationContext $context): mixed | ||
{ | ||
if (! isset($this->cast)) { | ||
$this->cast = $this->castableClass::dataCastUsing(...$this->arguments); | ||
} | ||
|
||
return $this->cast->cast($property, $value, $properties, $context); | ||
} | ||
|
||
public function __serialize(): array | ||
{ | ||
return [ | ||
'castableClass' => $this->castableClass, | ||
'arguments' => $this->arguments, | ||
]; | ||
} | ||
|
||
public function __unserialize(array $data): void | ||
{ | ||
$this->castableClass = $data['castableClass']; | ||
$this->arguments = $data['arguments']; | ||
} | ||
} |
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