承接 freddiegar/json-api-mapper 相关项目开发

从需求分析到上线部署,全程专人跟进,保证项目质量与交付效率

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

freddiegar/json-api-mapper

Composer 安装命令:

composer require freddiegar/json-api-mapper

包简介

Mapper response json-api in PHP

README 文档

README

It is a mapper in PHP from response jsonapi.org.

This library create a object from response json-api. Access to elements in response easily

Status Branch

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

Requisites

  • php >= 7.1.3

Install

composer require freddiegar/json-api-mapper

Usage

Creating instance of Mapper, see $jsonApiResponse here

use FreddieGar\JsonApiMapper\JsonApiMapper;

$jsonApi = new JsonApiMapper($jsonApiResponse);

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

By example, get data resource

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

By example, get included

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

By example, get errors, see $jsonApiResponse here

$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

Find

Get data with id = 2

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

Get included by type = people

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

Get included with type = people and id = 3

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

Alias in JsonApiResponse class

You can use any option to access to data in that response

Method* Alias Property Description
getData() data() data Return object DataMapper if exists in response, else null
getErrors() errors() errors Return object ErrorsMapper if exists in response, else null
getMeta() meta() meta Return object MetaMapper if exists in response, else null
getJsonApi() jsonapi() jsonapi Return object JsonApiMapper if exists in response, else null
getIncluded() included() included Return object IncludedMapper if exists in response, else null
getLinks() links() links Return object LinksMapper if exists in response, else null

Performance

You will prefer to use get* (getData(), getErrors()) methods accessors, they are direct call, any other ways are overloading (__call and __get), this are slower

Response Used In Example

You can see all example here

Response json-api Resource used in this example

{
  "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"
      }
    }
  ]
}

Response json-api Errors used in this example

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

License

MIT

统计信息

  • 总下载量: 201
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 3
  • 点击次数: 11
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 2
  • Watchers: 1
  • Forks: 1
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2018-04-12

承接程序开发

PHP开发

VUE

Vue开发

前端开发

小程序开发

公众号开发

系统定制

数据库设计

云部署

网站建设

安全加固