1. Go to this page and download the library: Download nilportugues/haljson 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/ */
nilportugues / haljson example snippets
$post = new Post(
new PostId(9),
'Hello World',
'Your first post',
new User(
new UserId(1),
'Post Author'
),
[
new Comment(
new CommentId(1000),
'Have no fear, sers, your king is safe.',
new User(new UserId(2), 'Barristan Selmy'),
[
'created_at' => (new DateTime('2015/07/18 12:13:00'))->format('c'),
'accepted_at' => (new DateTime('2015/07/19 00:00:00'))->format('c'),
]
),
]
);
// PostMapping.php
class PostMapping implements \NilPortugues\Api\Mappings\HalMapping
{
public function getClass()
{
return Post::class;
}
public function getAlias()
{
return 'Message';
}
public function getAliasedProperties()
{
return [
'author' => 'author',
'title' => 'headline',
'content' => 'body',
];
}
public function getHideProperties()
{
return [];
}
public function getIdProperties()
{
return ['postId'];
}
public function getUrls()
{
return [
'self' => 'http://example.com/posts/{postId}', // Mandatory
'comments' => 'http://example.com/posts/{postId}/comments' // Optional
];
}
public function getCuries()
{
return [
'name' => 'example',
'href' => "http://example.com/docs/rels/{rel}",
];
}
}
// UserMapping.php
class UserMapping implements \NilPortugues\Api\Mappings\HalMapping
{
public function getClass()
{
return User::class;
}
public function getAlias()
{
return '';
}
public function getAliasedProperties()
{
return [];
}
public function getHideProperties()
{
return [];
}
public function getIdProperties()
{
return ['postId'];
}
public function getUrls()
{
return [
'self' => 'http://example.com/users/{userId}'
];
}
public function getCuries()
{
return [
'name' => 'example',
'href' => "http://example.com/docs/rels/{rel}",
];
}
}
// CommentMapping.php
class CommentMapping implements \NilPortugues\Api\Mappings\HalMapping
{
public function getClass()
{
return Comment::class;
}
public function getAlias()
{
return '';
}
public function getAliasedProperties()
{
return [];
}
public function getHideProperties()
{
return [];
}
public function getIdProperties()
{
return ['commentId'];
}
public function getUrls()
{
return [
'self' => 'http://example.com/comments/{commentId}',
];
}
public function getCuries()
{
return [
'name' => 'example',
'href' => "http://example.com/docs/rels/{rel}",
];
}
}
$mappings = [
PostMapping::class,
UserMapping::class,
CommentMapping::class,
];
$mapper = new Mapper($mappings);
use NilPortugues\Api\Hal\JsonTransformer;
use NilPortugues\Api\Hal\HalSerializer;
use NilPortugues\Api\Hal\Http\Message\Response;
$transformer = new JsonTransformer($mapper);
//For XML: $transformer = new XmlTransformer($mapper);
//Output transformation
$serializer = new HalSerializer($transformer);
$output = $serializer->serialize($post);
//PSR7 Response with headers and content.
$response = new Response($output);
header(
sprintf(
'HTTP/%s %s %s',
$response->getProtocolVersion(),
$response->getStatusCode(),
$response->getReasonPhrase()
)
);
foreach($response->getHeaders() as $header => $values) {
header(sprintf("%s:%s\n", $header, implode(', ', $values)));
}
echo $response->getBody();
use NilPortugues\Api\Hal\HalPagination;
use NilPortugues\Api\Hal\HalSerializer;
use NilPortugues\Api\Hal\JsonTransformer;
// ...
//$objects is an array of objects, such as Post::class.
// ...
$page = new HalPagination();
//set the amounts
$page->setTotal(20);
$page->setCount(10);
//set the objects
$page->setEmbedded($objects);
//set up the pagination links
$page->setSelf('/post?page=1');
$page->setPrev('/post?page=1');
$page->setFirst('/post?page=1');
$page->setLast('/post?page=1');
$output = $serializer->serialize($page);
Loading please wait ...
Before you can download the PHP files, the dependencies should be resolved. This can take some minutes. Please be patient.