ytake/hhypermedia
Composer 安装命令:
composer require ytake/hhypermedia
包简介
for HATEOAS REST web services
README 文档
README
Hypertext Application Language for HHVM/Hack
Supported
HAL - Hypertext Application Language
JSON Hypertext Application Language draft-kelly-json-hal-08
vnd.error
Requirements
HHVM 4.0.0 and above.
1.Installation
$ composer require ytake/hhypermedia
2.Usage
Given a Hack Object,
the hal+json transformer will represent the given data following the JSON Hypertext Application Language draft-kelly-json-hal-08 specification draft.
Basic
use type Ytake\Hhypermedia\Serializer\HalJsonSerializer; use type Ytake\Hhypermedia\Link; use type Ytake\Hhypermedia\LinkResource; use type Ytake\Hhypermedia\Serializer; use type Ytake\Hhypermedia\HalResource; use type Ytake\Hhypermedia\ResourceObject; use type Ytake\Hhypermedia\Visitor\JsonSerializationVisitor; $link = new Link('self', vec[new LinkResource('/users')]); $ro = new ResourceObject() |> $$->withLink($link); $resource = new HalResource($ro, dict['id' => 123456789]); $secondRo = new ResourceObject() |> $$->withEmbedded('tests', vec[$resource]); $hal = new HalResource($secondRo); $s = new Serializer( new HalJsonSerializer(), $hal, new JsonSerializationVisitor() ); echo $s->serialize();
Basic - Result
{
"_embedded":{
"tests":[
{
"id":123456789,
"_links":{
"self":{
"href":"\/tests"
}
}
}
]
}
}
Curies
use type Ytake\Hhypermedia\Link; use type Ytake\Hhypermedia\Curie; use type Ytake\Hhypermedia\CurieResource; use type Ytake\Hhypermedia\LinkResource; use type Ytake\Hhypermedia\Serializer; use type Ytake\Hhypermedia\HalResource; use type Ytake\Hhypermedia\ResourceObject; use type Ytake\Hhypermedia\Serializer\HalJsonSerializer; use type Ytake\Hhypermedia\Visitor\JsonSerializationVisitor; $ro = new ResourceObject() |> $$->withLink(new Link('self', vec[new LinkResource('/tests')])) |> $$->withLink(new Curie(vec[ new CurieResource('http://haltalk.herokuapp.com/docs/{rel}', shape('name' => 'heroku')) ])); $s = new Serializer( new HalJsonSerializer(), new HalResource($ro), new JsonSerializationVisitor() ); echo $s->serialize();
Curies - Result
{
"_links":{
"self":{
"href":"\/tests"
},
"curies":[
{
"href":"http:\/\/haltalk.herokuapp.com\/docs\/{rel}",
"templated":true,
"name":"heroku"
}
]
}
}
3.vnd.error
Supported the vnd.error.
use type Ytake\Hhypermedia\Serializer; use type Ytake\Hhypermedia\LinkResource; use type Ytake\Hhypermedia\Error\ErrorLink; use type Ytake\Hhypermedia\Error\MessageResource; use type Ytake\Hhypermedia\ResourceObject; use type Ytake\Hhypermedia\Serializer\VndErrorSerializer; use type Ytake\Hhypermedia\Visitor\JsonSerializationVisitor; $linkVec = vec[new LinkResource('http://...')]; $new = new ResourceObject() |> $$->withLink( new ErrorLink('help', $linkVec)) |> $$->withLink( new ErrorLink('about', $linkVec)) |> $$->withLink( new ErrorLink('describes', $linkVec)); $s = new Serializer( new VndErrorSerializer(), new MessageResource( 'Validation failed', $new, shape('logref' => 42, 'path' => '/username') ), new JsonSerializationVisitor() ); \var_dump($s->toDict());
vnd.error - toDict
dict[ 'message' => 'Validation failed', 'logref' => 42, 'path' => '/username', '_links' => dict[ 'help' => dict[ 'href' => 'http://...' ], 'about' => dict[ 'href' => 'http://...' ], 'describes' => dict[ 'href' => 'http://...' ], ] ]
vnd.error - Result
{
"message": "Validation failed",
"path": "/username",
"logref": 42,
"_links": {
"about": {
"href": "http://..."
},
"describes": {
"href": "http://..."
},
"help": {
"href": "http://..."
}
}
}
ytake/hhypermedia 适用场景与选型建议
ytake/hhypermedia 是一款 基于 Hack 开发的 Composer 扩展包,目前已累计 5.95k 次下载、GitHub Stars 达 1, 最近一次更新时间为 2019 年 04 月 27 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「serialize」 「rest」 「hal」 「Hypertext Application Language」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 ytake/hhypermedia 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 ytake/hhypermedia 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 ytake/hhypermedia 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
A PHP implementation of HAL http://stateless.co/hal_specification.html
LazyPDO is a set of wrappers over PHP's standard PDO and PDOStatement classes. It enables lazy loading, serialization and decoration.
Transform data structures
Lightweight PHP library that allows exchanging binary data with Qt programs (QDataStream)
Build a Hypermedia API response once and return it in multiple formats
HAL+JSON & HAL+XML API transformer outputting valid API responses.
统计信息
- 总下载量: 5.95k
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 1
- 点击次数: 14
- 依赖项目数: 1
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2019-04-27