tomphp / siren
此包已被弃用且不再维护。没有建议的替代包。
用于在PHP中创建和解析Siren API的库
v0.1.0
2016-12-05 22:04 UTC
Requires
- php: ^7.0
- beberlei/assert: ^2.6
- psr/link: ^1.0
- tomphp/exception-constructor-tools: ^1.0
Requires (Dev)
- friendsofphp/php-cs-fixer: 2.0.0-RC
- phpunit/phpunit: ^5.6
This package is not auto-updated.
Last update: 2023-11-11 15:45:20 UTC
README
Siren API的序列化和解析器。
Siren
Siren 是一个使用JSON的HATEOAS API的架构。
当前状态
此项目发布严格遵守SemVer。目前,此项目处于零点状态。虽然其功能健全且运行良好,但新版本可能会出现大的向下不兼容性中断。
安装
composer require tomphp/siren:dev-master
序列化
使用构建器创建实体,通过调用 TomPHP\Siren\Entity::builder()
来创建。
use TomPHP\Siren\{Entity, Action}; $editAction = Action::builder() ->setName('edit') ->setTitle('Edit User') ->setHref('http://example.com/api/v1/users/ea019642-9c53-415f-88b6-e191dea184f9') ->setMethod('PUT') ->setType('application/vnd.siren+json') ->addField('email', ['email-class'], 'email', 'test@example.com', 'Email Address') ->build(); $user = Entity::builder() ->addLink('self', 'http://example.com/api/v1/users/ea019642-9c53-415f-88b6-e191dea184f9') ->addProperty('full_name', 'Tom Oram') ->addProperty('email', 'tom@example.com') ->addClass('item') ->addAction($editAction) ->build(); print(json_encode($user->toArray());
解析
可以使用 fromArray()
构造函数从JSON解码的数组中创建实体。
// Assuming the JSON from the serialising example. $user = Entity::fromArray(json_decode($json, true)); echo 'Name: ' . $user->getProperty('full_name') . PHP_EOL; echo 'Email: ' . $user->getProperty('email') . PHP_EOL; $editAction = $user->getAction('edit'); echo 'Edit Action ' . $editAction->getMehod() . ' ' . $editAction->getHref() . PHP_EOL;
贡献
我希望尽快使此项目稳定,因此任何形式的帮助都将非常感激。如果您认为您可以帮助,请提交一个Pull Request。