niko9911 / json-to-entity
将 JSON 映射到实体。
1.0.2
2019-03-30 17:06 UTC
Requires
- php: ^7.2
- phpdocumentor/reflection-docblock: ^4.3
- symfony/property-info: ^4.2
Requires (Dev)
- friendsofphp/php-cs-fixer: ^2
- phpunit/phpunit: ^7 || ^8
This package is auto-updated.
Last update: 2024-09-29 05:02:26 UTC
README
将 JSON 映射到实体。这允许您轻松验证负载 JSON 并自动将其映射到实体类。此类可以是例如 ORM 类,当您可以直接将其保存到数据库中时。
如果您懒散并且需要快速开发,这相当不错,但如果您需要高性能应用程序,请手动映射和验证 JSON。具有性能打击,但节省时间。
安装
通过 composer
composer require niko9911/json-to-entity
用法
<?php declare(strict_types=1); // Declare entity where to map. final class Basic { /** * @var string */ private $bar; /** * @var int|null */ private $foo; /** * @var array */ private $fooBar; /** * BasicUnitTestEntity constructor. * * @param string $bar * @param int $foo * @param array $fooBar */ public function __construct(string $bar, ?int $foo, array $fooBar) { $this->bar = $bar; $this->foo = $foo; $this->fooBar = $fooBar; } /** * @return string */ public function getBar(): string { return $this->bar; } /** * @return int|null */ public function getFoo(): ?int { return $this->foo; } /** * @return array */ public function getFooBar(): array { return $this->fooBar; } } // JSON $json = <<<JSON { "bar": "Some_Bar", "foo": 10, "fooBar": ["a", "b", "c"] } JSON; $mapper = new \Niko9911\JsonToEntity\Mapper(); $entity = $mapper->map(\json_decode($json), Basic::class); var_dump($entity); //class Basic#25 (3) { // private $bar => // string(8) "Some_Bar" // private $foo => // int(10) // private $fooBar => // array(3) { // [0] => // string(1) "a" // [1] => // string(1) "b" // [2] => // string(1) "c" // } //}
许可证
根据 MIT 许可证 许可。