jacobfennik / json-api-normalizer
处理JSON:API响应的简单包
v0.1-beta.1
2018-12-12 01:10 UTC
Requires
- illuminate/support: ^5.7
Requires (Dev)
- phpunit/phpunit: ^7.5
This package is auto-updated.
Last update: 2024-09-12 13:19:11 UTC
README
用于规范化并构建JSON:API响应数据的实用工具。
安装
$ composer require jacobfennik/json-api-normalizer
示例
使用规范化器构建
<?php use JacobFennik\JsonApiNormalizer\Normalizer; $apiResponse = ' { "data": [{ "type": "articles", "id": "1", "attributes": { "title": "JSON:API paints my bikeshed!" }, "relationships": { "author": { "data": { "type": "people", "id": "9" } }, } }], "included": [{ "type": "people", "id": "9", "attributes": { "firstName": "Dan", "lastName": "Gebhardt", "twitter": "dgeb" }, }] } '; $normalizer = new Normalizer($apiResponse); $built = $normalizer->build();
访问构建后的数据
<?php $normalizer = new Normalizer($apiResponse); $article = $normalizer->build(1); // Build object with id '1' $title = $article->title; $authorFirstName = $article->author->firstName;