freddiegar/json-api-mapper

PHP中的json-api响应映射器

v1.0.0 2018-05-05 15:53 UTC

This package is auto-updated.

Last update: 2024-09-22 08:20:21 UTC


README

这是一个从jsonapi.org响应中映射到PHP的映射器。

此库从响应json-api创建一个对象。轻松访问响应中的元素

分支状态

master: Build Status develop: Build Status v1.0.0: Build Status

要求

  • php >= 7.1.3

安装

composer require freddiegar/json-api-mapper

使用方法

创建Mapper实例,请参见 $jsonApiResponse 这里

use FreddieGar\JsonApiMapper\JsonApiMapper;

$jsonApi = new JsonApiMapper($jsonApiResponse);

$data = $jsonApi->getData(0);
$included = $jsonApi->getIncluded();

示例,获取数据资源

echo $data->getType(); // articles
echo $data->getId(); // 1

echo print_r($data->getAttributes(), true); // ['title' => 'JSON API paints my bikeshed!', 'body' => '...']
echo $data->getAttribute('created'); // 2015-05-22T14:56:29.000Z
echo $data->getAttribute('description'); // If not exist, return: null

echo print_r($data->getRelationships(), true); // ['author' => ['id' => '1', 'type' => 'people']]
echo get_class($data->getRelationship('author')); // return DataMapperInterface
echo $data->getRelationship('author')->getType(); // people
echo $data->getRelationship('author')->getId(); // 1

示例,获取包含的内容

echo get_class($included->getIncluded(0)); // return DataMapperInterface
echo $included->getIncluded(0)->getType(); // people
echo $included->getIncluded(0)->getId(); // 42
echo $included->getIncluded(0)->getName(); // John
echo $included->getIncluded(1); // null, it is not defined in response

示例,获取错误,请参见 $jsonApiResponse 这里

$jsonApi = new JsonApiMapper($jsonApiResponse);

echo get_class($jsonApi->getErrors()); // Return ErrorsMapperInterface

$firstError = $jsonApi->getErrors(0); // Get first error

echo $firstError->getStatus(); // 422
echo print_r($firstError->getSource(), true); // ['pointer' => '/data/attributes/first-name']
echo $firstError->getTitle(); // Invalid Attribute
echo $firstError->getDetail(); // First name must contain at least three characters.

$secondError = $jsonApi->getErrors(1); // null, it is not defined in response

查找

获取 id = 2 的数据

$dataWithIdTwo = $data->find(2); // Return DataMapperInterface if exist else null

通过 type = people 获取包含的内容

$dataPeople = $included->find('people'); // Return DataMapperInterface if exist else null

通过 type = people 和 id = 3 获取包含的内容

$dataPeopleWithIdThree = $included->find('people', 3); // Return DataMapperInterface if exist else null
// OR
$peopleWithIdThree = $dataPeople->find(3); // Return DataMapperInterface if exist else null

JsonApiResponse类中的别名

您可以使用任何选项来访问该响应中的数据

性能

您应该优先使用 get* (getData(), getErrors()) 访问器方法,它们是直接调用,其他方式都是重载(__call__get),这些 较慢的

示例中使用的响应

您可以在 这里 找到所有示例

此示例中使用的 json-api 资源

{
  "data": [{
    "type": "articles",
    "id": "1",
    "attributes": {
      "title": "JSON API paints my bikeshed!",
      "body": "The shortest article. Ever.",
      "created": "2015-05-22T14:56:29.000Z",
      "updated": "2015-05-22T14:56:28.000Z"
    },
    "relationships": {
      "author": {
        "data": {"id": "42", "type": "people"}
      }
    }
  }],
  "included": [
    {
      "type": "people",
      "id": "42",
      "attributes": {
        "name": "John",
        "age": 80,
        "gender": "male"
      }
    }
  ]
}

此示例中使用的 json-api 错误

{
  "errors": [
      {
        "status": "422",
        "source": { "pointer": "/data/attributes/first-name" },
        "title":  "Invalid Attribute",
        "detail": "First name must contain at least three characters."
      }
    ]
}

许可

MIT