addequatte/json-serializer

简单实现JsonSerializable接口,用于将模型转换为JSON

0.0.2-alpha 2022-03-19 12:45 UTC

This package is auto-updated.

Last update: 2024-09-22 17:26:25 UTC


README

简单实现JsonSerializable接口,用于将模型转换为JSON

功能

  • 将子模型转换为JSON。
  • 隐藏你想要的字段。
  • 处理结果字段

安装

composer require addequatte/json-serializer

例如

/**
 * lib/Example/index.php
 */
namespace Addequatte\JsonSerializer\Example;

use Addequatte\JsonSerializer\Model\JsonSerializable;
use Addequatte\JsonSerializer\Processor\FieldProcessor;

require_once dirname(__DIR__) . '/../vendor/autoload.php';

class Author extends JsonSerializable
{
    private $id = 1;

    private $name = 'Author name';

    private $gender = 'male';

    private $birthDate;

    private $books;

    public function __construct()
    {
        $this->birthDate = new \DateTime('2000-01-12');

        $this->books[] = new Book();
    }

    /**
     * @return int
     */
    public function getId(): int
    {
        return $this->id;
    }

    /**
     * @return string
     */
    public function getName(): string
    {
        return $this->name;
    }

    /**
     * @return string
     */
    public function getGender(): string
    {
        return $this->gender;
    }

    /**
     * @return \DateTime
     */
    public function getBirthDate(): \DateTime
    {
        return $this->birthDate;
    }

    /**
     * @return array
     */
    public function getBooks(): array
    {
        return $this->books;
    }
}

class Book extends JsonSerializable
{
    private $id = 1;

    private $name = 'Book Name';

    private $description = 'Book description';

    /**
     * @return int
     */
    public function getId(): int
    {
        return $this->id;
    }

    /**
     * @return string
     */
    public function getName(): string
    {
        return $this->name;
    }

    /**
     * @return string
     */
    public function getDescription(): string
    {
        return $this->description;
    }
}

$author = new Author();

print_r(json_encode($author, JSON_PRETTY_PRINT) . PHP_EOL);

$processor = new FieldProcessor();

$processor->addClosure(Author::class,'birthDate', function ($value) {
    return $value->format('d.m.Y H:i:s');
});

$processor->addClosure(Book::class,'name', function ($value) {
    return 'Processed book name';
});

$jsonSerializeHandler = new \Addequatte\JsonSerializer\Handlers\JsonSerializeHandler($processor);

$jsonSerializeHandler->addHiddenFields(Author::class, ['gender']);
$jsonSerializeHandler->addHiddenFields(Book::class, ['description']);


print_r(json_encode($jsonSerializeHandler->jsonSerialize($author), JSON_PRETTY_PRINT));

第一种情况的结果

{
  "id": 1,
  "name": "Author name",
  "gender": "male",
  "birthDate": {
    "date": "2000-01-12 00:00:00.000000",
    "timezone_type": 3,
    "timezone": "Asia\/Krasnoyarsk"
  },
  "books": [
    {
      "id": 1,
      "name": "Book Name",
      "description": "Book description"
    }
  ]
}

第二种情况的结果

{
  "id": 1,
  "name": "Author name",
  "birthDate": "12.01.2000 00:00:00",
  "books": [
    {
      "id": 1,
      "name": "Processed book name"
    }
  ]
}
  • 你可以获取你的模型拥有的所有getter属性
  • 如你所见,使用起来非常简单,只需通过扩展你的模型使用 Addequatte\JsonSerializer\Model\JsonSerializable
  • 如果你想隐藏某些字段,可以使用 setHiddenFields(array $hiddenFields): void 方法
  • 你可以通过使用 $jsonSerializeHandler->addHiddenFields(Author::class, ['gender']) 轻松地从子模型中隐藏字段
  • 你可以通过使用 $processor->addClosure(Author::class,'birthDate', function ($value) { return $value->format('d.m.Y H:i:s'); }) 轻松地更改模型字段
  • 你可以编写自己的处理器实现 ProcessorInterface