lastdragon-ru / lara-asp-serializer
The Awesome Set of Packages for Laravel - The Serializer.
Requires
- php: ^8.1|^8.2|^8.3
- illuminate/container: ^10.34.0|^11.0.0
- illuminate/contracts: ^10.34.0|^11.0.0
- illuminate/database: ^10.34.0|^11.0.0
- illuminate/support: ^10.34.0|^11.0.0
- lastdragon-ru/lara-asp-core: 6.2.0
- phpdocumentor/type-resolver: ^1.7
- phpstan/phpdoc-parser: ^1.25
- symfony/polyfill-php83: ^1.28
- symfony/property-access: ^6.3.0|^7.0.0
- symfony/property-info: ^6.3.9|^7.0.0
- symfony/serializer: ^6.3.3|^7.0.0
Requires (Dev)
- lastdragon-ru/lara-asp-testing: 6.2.0
- mockery/mockery: ^1.6.2
- orchestra/testbench: ^8.0.0|^9.0.0
- phpunit/phpunit: ^10.1.0|^11.0.0
README
This package provides a customizable wrapper around the Symfony Serializer Component to use it inside Laravel application.
Requirements
Requirement | Constraint | Supported by |
---|---|---|
PHP | ^8.3 |
HEAD ⋯ 5.0.0 |
^8.2 |
HEAD ⋯ 5.0.0-beta.0 |
|
^8.1 |
HEAD ⋯ 5.0.0-beta.0 |
|
Laravel | ^11.0.0 |
HEAD , 6.2.0 |
^10.34.0 |
HEAD , 6.2.0 |
|
^10.0.0 |
6.1.0 ⋯ 5.0.0-beta.0 |
|
^9.21.0 |
5.6.0 ⋯ 5.0.0-beta.1 |
|
^9.0.0 |
5.0.0-beta.0 |
Installation
composer require lastdragon-ru/lara-asp-serializer
Usage
<?php declare(strict_types = 1); // phpcs:disable PSR1.Files.SideEffects // phpcs:disable PSR1.Classes.ClassDeclaration namespace LastDragon_ru\LaraASP\Serializer\Docs\Examples\Usage; use DateTimeInterface; use Illuminate\Container\Container; use Illuminate\Support\Facades\Date; use LastDragon_ru\LaraASP\Dev\App\Example; use LastDragon_ru\LaraASP\Serializer\Contracts\Serializable; use LastDragon_ru\LaraASP\Serializer\Contracts\Serializer; class User implements Serializable { public function __construct( public int $id, public string $name, public DateTimeInterface $created, ) { // empty } } $user = new User(1, 'User', Date::parse('2023-08-27T08:30:44.473+00:00')); $serializer = Container::getInstance()->make(Serializer::class); $serialized = $serializer->serialize($user); $deserialized = $serializer->deserialize(User::class, $serialized); Example::dump($serialized); Example::dump($deserialized);
The $serialized
is:
"{"id":1,"name":"User","created":"2023-08-27T08:30:44.473+00:00"}"
The $deserialized
is:
LastDragon_ru\LaraASP\Serializer\Docs\Examples\Usage\User {
+id: 1
+name: "User"
+created: Illuminate\Support\Carbon {
+"date": "2023-08-27 08:30:44.473000"
+"timezone_type": 1
+"timezone": "+00:00"
}
}
Extending
Out of the box, the package supports only the following objects (see Factory
for more details):
- Any object that implement
Serializable
(seeSerializableNormalizer
) - Any object that implement
\DateTimeInterface
(seeDateTimeNormalizer
) \DateTimeZone
\DateInterval
- PHP Enums
Publish the config and add normalizers/denormalizers if you need more:
php artisan vendor:publish --provider=LastDragon_ru\\LaraASP\\Serializer\\Provider --tag=config
Eloquent Cast1
You can use the LastDragon_ru\LaraASP\Serializer\Casts\AsSerializable
cast class to cast a model string attribute to an object:
<?php declare(strict_types = 1); // phpcs:disable PSR1.Files.SideEffects // phpcs:disable PSR1.Classes.ClassDeclaration namespace LastDragon_ru\LaraASP\Serializer\Docs\Examples\AsSerializable; use Illuminate\Database\Eloquent\Model; use LastDragon_ru\LaraASP\Dev\App\Example; use LastDragon_ru\LaraASP\Serializer\Casts\AsSerializable; use LastDragon_ru\LaraASP\Serializer\Contracts\Serializable; use Override; use function array_merge; class UserSettings implements Serializable { public function __construct( public int $perPage, public bool $showSidebar, ) { // empty } } /** * @property UserSettings|null $settings */ class User extends Model { /** * @return array<array-key, mixed> */ #[Override] protected function casts(): array { return array_merge(parent::casts(), [ 'settings' => AsSerializable::using(UserSettings::class), ]); } } $user = new User(); $user->settings = new UserSettings(35, false); Example::dump($user->settings); Example::dump($user->getAttributes());
The $user->settings
is:
LastDragon_ru\LaraASP\Serializer\Docs\Examples\AsSerializable\UserSettings {
+perPage: 35
+showSidebar: false
}
The $user->getAttributes()
is:
[
"settings" => "{"perPage":35,"showSidebar":false}",
]
Upgrading
Please follow Upgrade Guide.
Contributing
This package is the part of Awesome Set of Packages for Laravel. Please use the main repository to report issues, send pull requests, or ask questions.