sportuondo/eralda

简单易用的对象到数组转换库。

v1.0.0 2019-03-01 11:12 UTC

This package is auto-updated.

Last update: 2024-09-29 05:34:41 UTC


README

Latest Version on Packagist

本库的目标是集中处理将对象(通常是模型)转换为数组以通过JSON暴露的逻辑。Eralda可以通过指定所需的属性映射来将任何类型的对象(PHP标准对象、Laravel Eloquent模型等)转换为数组。

Eralda还提供了转换属性值以及嵌入相关元素(例如作者→书籍)的可能性。

安装

您可以通过composer安装此包

composer require sportuondo/eralda

用法

给定以下对象

$tolkien = new Author();
$tolkien->id = 1;
$tolkien->name = 'J.R.R. Tolkien';
$tolkien->isFreelance = false;
$tolkien->birthDate = Carbon::createFromFormat('Y-m-d', '1892-01-03');

$hobbit = new Book();
$hobbit->id = 1;
$hobbit->author = $tolkien;
$hobbit->title = 'The Hobbit';
$hobbit->year = 1937;

$lotr = new Book();
$lotr->id = 2;
$lotr->author = $tolkien;
$lotr->title = 'The Lord of the Rings';
$lotr->year = 1968;

$this->tolkien->books = [
    $hobbit,
    $lotr
];

我们可以定义以下转换器

class AuthorArrayTransformer extends ArrayTransformerAbstract
{
    protected $keysMap = [
        'id'          => 'id',
        'name'        => 'name',
        'isFreelance' => 'is_freelance',
        'birthDate'   => 'birth_date',
    ];
    
    protected function presentBirthDate($author)
    {
        return $author->birthDate->format('Y-m-d');
    }
}

$keysMap属性用于定义源对象属性与结果数组键之间的映射。

另一方面,我们包含了一个呈现器来动态修改原始对象属性的结果值。定义呈现器的命名法由关键字present后跟原始对象属性名(大驼峰格式)组成。

要执行转换,我们会这样做

$authorTransformer = new AuthorArrayTransformer();
$authorArray = $authorTransformer->transformItem($tolkien);

json_encode($authorArray)

结果数组看起来像这样

{
  "id": 1,
  "name": "J.R.R. Tolkien",
  "is_freelance": false,
  "birth_date": "1892-01-03"
}

转换集合

与转换单个对象的方式相同,我们还可以转换对象的集合。由于我们有作者的数组,我们可以这样做

$authorTransformer = new AuthorArrayTransformer();
$authorsArray = $authorTransformer->transformCollection($authors);

json_encode($authorsArray)

得到以下结果

{
  [
    {
      "id": 1,
      "name": "J.R.R. Tolkien",
      "is_freelance": false,
      "birth_date": "1892-01-03"
    },
    {
      "id": 2,
      "name": "Isaac Asimov",
      "is_freelance": false,
      "birth_date": "1920-01-02"
    }
  ]
}

嵌入额外元素

我们可以通过使用属性$embeds来指示要嵌入的元素

给定以下书籍转换器

class BookArrayTransformer extends ArrayTransformerAbstract
{
    protected $keysMap = [
        'id'     => 'id',
        'title'  => 'title',
        'author' => 'author',
        'year'   => 'year',
    ];

    protected function presentAuthor($book)
    {
        return $book->author->name;
    }
}

我们可以按照以下方式修改我们的作者转换器

class AuthorArrayTransformer extends ArrayTransformerAbstract
{
    protected $embeds = [
        'books',
    ];

    protected $keysMap = [
        'id'          => 'id',
        'name'        => 'name',
        'isFreelance' => 'is_freelance',
        'birthDate'   => 'birth_date',
    ];

    protected function presentBirthDate($author)
    {
        return $author->birthDate->format('Y-m-d');
    }

    protected function embedBooks($author)
    {
        $bookTransformer = new BookArrayTransformer();
        return $bookTransformer->transformCollection($author->books);
    }
}

在这种情况下,我们想要嵌入作者所属的书籍。定义嵌入的命名法由关键字embed后跟我们想要嵌入的元素名(大驼峰格式)组成。

结果将类似于这样

{
  [
    {
      "id": 1,
      "name": "J.R.R. Tolkien",
      "is_freelance": false,
      "birth_date": "1892-01-03",
      "books": [
        {
          "id": 1,
          "title": "The Hobbit",
          "author": "J.R.R. Tolkien",
          "year": 1937
        },
        {
          "id": 2,
          "title": "The Lord of the Rings",
          "author": "J.R.R. Tolkien",
          "year": 1968
        }
      ]
    },
    {
      "id": 2,
      "name": "Isaac Asimov",
      "is_freelance": false,
      "birth_date": "1920-01-02",
      "books": [
        {
          "id": 3,
          "title": "The Caves of Steel",
          "author": "Isaac Asimov",
          "year": 1954
        }
      ]
    }
  ]
}

测试

composer test

致谢

许可证

MIT许可证(MIT)。请参阅许可证文件以获取更多信息。