Giter Site home page Giter Site logo

hhypermedia's Introduction

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

$ 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://..."
    }
  }
}

hhypermedia's People

Contributors

ytake avatar

Stargazers

 avatar

Watchers

 avatar  avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.