项目作者: ytake

项目描述 :
Hypertext Application Language for HHVM/Hack
高级语言: Hack
项目地址: git://github.com/ytake/hhypermedia.git
创建时间: 2018-02-10T02:01:25Z
项目社区:https://github.com/ytake/hhypermedia

开源协议:MIT License

下载


Hhypermedia

Hypertext Application Language for HHVM/Hack

Travis (.org) branch(https://travis-ci.org/ytake/hhypermedia.svg?branch=master)
Packagist
Packagist Version
Packagist

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
  2. Usage
  3. vnd.error

1.Installation

  1. $ 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

  1. use type Ytake\Hhypermedia\Serializer\HalJsonSerializer;
  2. use type Ytake\Hhypermedia\Link;
  3. use type Ytake\Hhypermedia\LinkResource;
  4. use type Ytake\Hhypermedia\Serializer;
  5. use type Ytake\Hhypermedia\HalResource;
  6. use type Ytake\Hhypermedia\ResourceObject;
  7. use type Ytake\Hhypermedia\Visitor\JsonSerializationVisitor;
  8. $link = new Link('self', vec[new LinkResource('/users')]);
  9. $ro = new ResourceObject()
  10. |> $$->withLink($link);
  11. $resource = new HalResource($ro, dict['id' => 123456789]);
  12. $secondRo = new ResourceObject()
  13. |> $$->withEmbedded('tests', vec[$resource]);
  14. $hal = new HalResource($secondRo);
  15. $s = new Serializer(
  16. new HalJsonSerializer(),
  17. $hal,
  18. new JsonSerializationVisitor()
  19. );
  20. echo $s->serialize();

Basic - Result

  1. {
  2. "_embedded":{
  3. "tests":[
  4. {
  5. "id":123456789,
  6. "_links":{
  7. "self":{
  8. "href":"\/tests"
  9. }
  10. }
  11. }
  12. ]
  13. }
  14. }

Curies

  1. use type Ytake\Hhypermedia\Link;
  2. use type Ytake\Hhypermedia\Curie;
  3. use type Ytake\Hhypermedia\CurieResource;
  4. use type Ytake\Hhypermedia\LinkResource;
  5. use type Ytake\Hhypermedia\Serializer;
  6. use type Ytake\Hhypermedia\HalResource;
  7. use type Ytake\Hhypermedia\ResourceObject;
  8. use type Ytake\Hhypermedia\Serializer\HalJsonSerializer;
  9. use type Ytake\Hhypermedia\Visitor\JsonSerializationVisitor;
  10. $ro = new ResourceObject()
  11. |> $$->withLink(new Link('self', vec[new LinkResource('/tests')]))
  12. |> $$->withLink(new Curie(vec[
  13. new CurieResource('http://haltalk.herokuapp.com/docs/{rel}', shape('name' => 'heroku'))
  14. ]));
  15. $s = new Serializer(
  16. new HalJsonSerializer(),
  17. new HalResource($ro),
  18. new JsonSerializationVisitor()
  19. );
  20. echo $s->serialize();

Curies - Result

  1. {
  2. "_links":{
  3. "self":{
  4. "href":"\/tests"
  5. },
  6. "curies":[
  7. {
  8. "href":"http:\/\/haltalk.herokuapp.com\/docs\/{rel}",
  9. "templated":true,
  10. "name":"heroku"
  11. }
  12. ]
  13. }
  14. }

3.vnd.error

Supported the vnd.error.

  1. use type Ytake\Hhypermedia\Serializer;
  2. use type Ytake\Hhypermedia\LinkResource;
  3. use type Ytake\Hhypermedia\Error\ErrorLink;
  4. use type Ytake\Hhypermedia\Error\MessageResource;
  5. use type Ytake\Hhypermedia\ResourceObject;
  6. use type Ytake\Hhypermedia\Serializer\VndErrorSerializer;
  7. use type Ytake\Hhypermedia\Visitor\JsonSerializationVisitor;
  8. $linkVec = vec[new LinkResource('http://...')];
  9. $new = new ResourceObject()
  10. |> $$->withLink( new ErrorLink('help', $linkVec))
  11. |> $$->withLink( new ErrorLink('about', $linkVec))
  12. |> $$->withLink( new ErrorLink('describes', $linkVec));
  13. $s = new Serializer(
  14. new VndErrorSerializer(),
  15. new MessageResource(
  16. 'Validation failed',
  17. $new,
  18. shape('logref' => 42, 'path' => '/username')
  19. ),
  20. new JsonSerializationVisitor()
  21. );
  22. \var_dump($s->toDict());

vnd.error - toDict

  1. dict[
  2. 'message' => 'Validation failed',
  3. 'logref' => 42,
  4. 'path' => '/username',
  5. '_links' => dict[
  6. 'help' => dict[
  7. 'href' => 'http://...'
  8. ],
  9. 'about' => dict[
  10. 'href' => 'http://...'
  11. ],
  12. 'describes' => dict[
  13. 'href' => 'http://...'
  14. ],
  15. ]
  16. ]

vnd.error - Result

  1. {
  2. "message": "Validation failed",
  3. "path": "/username",
  4. "logref": 42,
  5. "_links": {
  6. "about": {
  7. "href": "http://..."
  8. },
  9. "describes": {
  10. "href": "http://..."
  11. },
  12. "help": {
  13. "href": "http://..."
  14. }
  15. }
  16. }