PHP code example of brandonlamb / php-hal

1. Go to this page and download the library: Download brandonlamb/php-hal library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

brandonlamb / php-hal example snippets


use PhpHal\Link;
use PhpHal\Resource;
use PhpHal\Format\Writer\Hal;

$resource = new Resource();
$resource->setURI('/foo');
$resource->setLink('foo', new Link('/bar'));
$resource->setData([
    'foo' => 'bar',
    'baz' => 'qux'
]);

$writer = new Hal\JsonWriter(true);
echo $writer->execute($resource);

use PhpHal\Link;
use PhpHal\Resource;
use PhpHal\Format\Writer\Siren;

$resource = new Resource();
$resource->setRepositoryKey('index');
$resource->setURI('/index?page=2');
$resource->setLink('prev', new Link('/index?page=1'));
$resource->setLink('next', new Link('/index?page=3'));
$resource->addData('count', 5);

$subresource = [];
foreach (range(1, 5) as $value) {
    $subresource = new Resource();
    $subresource->addData('value', $value);

    $subresources[] = $subresource;
}

$resource->addResources('subresources', $subresources);

$writer = new Siren\JsonWriter(true);
echo $writer->execute($resource);

use PhpHal\Link;
use PhpHal\Resource;
use PhpHal\Format\Writer\Hal;

$author = new Resource();
$author->setURI('/john-doe');
$author->setTitle('John Doe');

$article = new Resource();
$article->setURI('/lorem-ipsum');
$article->addData('description', 'Lorem ipsum dolor sit amet ...');
$article->linkResource('author', $author);

$writer = new Hal\XmlWriter(true);
echo $writer->execute($article);

use PhpHal\Format\Reader\Hal;

$json = '{"foo":"bar","baz":"qux","_links":{"self":{"href":"/foo"},"foo":{"href":"/bar"}}}';

$reader = new Hal\JsonReader();
$resource = $reader->execute($json);
print_r($resource);

PhpHal\Resource Object
(
    [uri:protected] => /foo
    [links:protected] => Array
        (
            [foo] => PhpHal\Link Object
                (
                    [href:protected] => /bar
                )

        )

    [data:protected] => Array
        (
            [foo] => bar
            [baz] => qux
        )

)
JSON
{
    "randonlamb/php-hal": "dev-master"
    }
}