Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/ytake/hhypermedia
Hypertext Application Language for HHVM/Hack
https://github.com/ytake/hhypermedia
hack hacklang hal hhvm hypertext-application-language
Last synced: about 1 month ago
JSON representation
Hypertext Application Language for HHVM/Hack
- Host: GitHub
- URL: https://github.com/ytake/hhypermedia
- Owner: ytake
- License: mit
- Created: 2018-02-10T02:01:25.000Z (almost 7 years ago)
- Default Branch: master
- Last Pushed: 2020-07-01T12:33:03.000Z (over 4 years ago)
- Last Synced: 2024-10-05T22:03:03.586Z (4 months ago)
- Topics: hack, hacklang, hal, hhvm, hypertext-application-language
- Language: Hack
- Size: 57.6 KB
- Stars: 1
- Watchers: 3
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# Hhypermedia
Hypertext Application Language for HHVM/Hack
![Travis (.org) branch(https://travis-ci.org/ytake/hhypermedia.svg?branch=master)](https://img.shields.io/travis/ytake/hhypermedia/master.svg?style=flat-square)
[![Packagist](https://img.shields.io/packagist/dt/ytake/hhypermedia.svg?style=flat-square)](https://packagist.org/packages/ytake/hhypermedia)
[![Packagist Version](https://img.shields.io/packagist/v/ytake/hhypermedia.svg?color=orange&style=flat-square)](https://packagist.org/packages/ytake/hhypermedia)
[![Packagist](https://img.shields.io/packagist/l/ytake/hhypermedia.svg?style=flat-square)](https://packagist.org/packages/ytake/hhypermedia)## Supported
[HAL - Hypertext Application Language](http://stateless.co/hal_specification.html)
[JSON Hypertext Application Language draft-kelly-json-hal-08](https://tools.ietf.org/html/draft-kelly-json-hal-08)
[vnd.error](https://github.com/blongden/vnd.error)## Requirements
HHVM 4.0.0 and above.
1. [Installation](#1-installation)
2. [Usage](#2-usage)
3. [vnd.error](#3-vnd-error)## 1.Installation
```bash
$ 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`](https://tools.ietf.org/html/draft-kelly-json-hal-08) specification draft.### Basic
```hack
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
```json
{
"_embedded":{
"tests":[
{
"id":123456789,
"_links":{
"self":{
"href":"\/tests"
}
}
}
]
}
}
```### Curies
```hack
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
```json
{
"_links":{
"self":{
"href":"\/tests"
},
"curies":[
{
"href":"http:\/\/haltalk.herokuapp.com\/docs\/{rel}",
"templated":true,
"name":"heroku"
}
]
}
}
```## 3.vnd.error
Supported the [vnd.error](https://github.com/blongden/vnd.error).
```hack
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
```hack
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
```json
{
"message": "Validation failed",
"path": "/username",
"logref": 42,
"_links": {
"about": {
"href": "http://..."
},
"describes": {
"href": "http://..."
},
"help": {
"href": "http://..."
}
}
}
```