thiagocordeiro / serializer
3.3.5
2024-05-27 12:30 UTC
Requires
- php: >=8.2
- ext-json: *
Requires (Dev)
- phpstan/phpstan: ^1.4
- phpunit/phpunit: ^9.5
- slevomat/coding-standard: ^7.0
- symfony/var-dumper: ^6.0
README
此包允许您将JSON对象解析为PHP类,无需使用注释的开销,它检查PHP对象构造函数并创建缓存类以将JSON转换为指定的类。
如何使用
对于Symfony项目,可以在serializer-bundle
找到扩展包。
对于Laravel项目,可以在laravel-serializer
找到扩展包。
否则,该包可在composer上使用
composer require thiagocordeiro/serializer
PHP序列化器不使用setter,因此您的类必须有一个构造函数,其中包含所有从JSON中来的属性。
基本示例(使用getter)
<?php declare(strict_types=1); namespace App\ValueObject; class User { private string $name; private string $email; public function __construct(string $name, string $email) { $this->name = $name; $this->email = $email; } public function getName(): string { return $this->name; } public function getEmail(): string { return $this->email; } }
基本示例(使用只读属性)
class User { public readonly string $name; public readonly string $email; public function __construct(string $name, string $email) { $this->name = $name; $this->email = $email; } }
一旦您有了您的类,您可以将JSON字符串转换为它,例如。
use Serializer\Builder\Encoder\EncoderFactory; use Serializer\Builder\Encoder\FileLoader\PipelineEncoderFileLoader; use Serializer\Builder\Decoder\DecoderFactory; use Serializer\Builder\Decoder\FileLoader\PipelineDecoderFileLoader; use Serializer\JsonSerializer; $encoder = new EncoderFactory(PipelineEncoderFileLoader::full('path/to/cache')); $decoder = new DecoderFactory(PipelineDecoderFileLoader::full('path/to/cache')); $serializer = new JsonSerializer($encoder, $decoder); $json = '{"name":"Arthur Dent","email":"arthur.dent@galaxy.org"}'; $serializer->deserialize($json, \App\ValueObject\User::class); // or $json = '[ {"name":"Arthur Dent","email":"arthur.dent@galaxy.org"}, {"name":"Chuck Norris","email":"chuck@norrs.com"}, ... ]'; $serializer->deserialize($json, \App\ValueObject\User::class);
相反的方式,例如。
use Serializer\Builder\Encoder\EncoderFactory; use Serializer\Builder\Encoder\FileLoader\PipelineEncoderFileLoader; use Serializer\Builder\Decoder\DecoderFactory; use Serializer\Builder\Decoder\FileLoader\PipelineDecoderFileLoader; use Serializer\JsonSerializer; $encoder = new EncoderFactory(PipelineEncoderFileLoader::full('path/to/cache')); $decoder = new DecoderFactory(PipelineDecoderFileLoader::full('path/to/cache')); $serializer = new JsonSerializer($encoder, $decoder); $user = new \App\ValueObject\User('Arthur Dent', 'arthur.dent@galaxy.org'); $json = $serializer->serialize($user); // will return {"name":"Arthur Dent","email":"arthur.dent@galaxy.org"} // or $json = $serializer->serialize([$user1, $user2, ...]);
复杂对象构建 - 对象
构造函数也可以包含数组和其他类,其他类必须遵循相同的规则,例如。
<?php declare(strict_types=1); namespace App\ValueObject; class User { // properties... public function __construct(string $name, string $email, ?Address $address) { $this->name = $name; $this->email = $email; $this->address = $address; } // getters }
对于此示例,一个JSON字符串可以包含或不含address
属性
{ "name": "Arthur Dent", "email": "arthur.dent@galaxy.org" }
或
{ "name": "Arthur Dent", "email": "arthur.dent@galaxy.org", "address": { "street": "Times Square", "contry": "USA" } }
复杂对象构建 - 数组
数组也欢迎,但由于无法在对象构造函数中确定数组类型,因此数组需要您编写PHP注释,一个简单的文档块就足够将任何JSON字符串反序列化,例如。
<?php use ValueObject\Address; class User { // properties... /** * @param Address[] $addresses */ public function __construct(string $name, array $addresses) { $this->name = $name; $this->addresses = $addresses; } // getters }
此类的JSON可能是
{ "name": "Arthur Dent", "addresses": [ {"street":"Times Square", "contry": "USA"}, {"street":"Leidseplein", "contry": "Netherlands"} ] }
默认值
默认情况下,所有未提供的值将用null
填充,如果属性不可为空,则将抛出TypeError
,但属性也可以具有默认值,并且在未提供的情况下,将使用默认值,例如。
<?php declare(strict_types=1); namespace App\ValueObject; class User { // properties... public function __construct(string $name, string $email, string $type = 'user') { $this->name = $name; $this->email = $email; $this->type = $type; } // getters }
没有type
的JSON将导致具有$type = 'user'
的对象。注意:默认值仅在未提供时使用,如果提供为null
,则值为null
。
带有参数的构造函数
<?php declare(strict_types=1); namespace App\ValueObject; class User { // properties... public function __construct(Place ...$places) { $this->places = $places; } // getters }
带有DateTime/DateTimeImmutable的构造函数
<?php declare(strict_types=1); namespace App\ValueObject; class User { // properties... public function __construct(DateTime $createdAt, DateTimeImmutable $updatedAt) { $this->createdAt = $createdAt; $this->updatedAt = $updatedAt; } // getters }
贡献
欢迎提出问题或提交PR。
支持
如果您想支持更改,可以发送捐款到以下地址。
比特币地址:bc1qfyudlcxqnvqzxxgpvsfmadwudg4znk2z3asj9h