PHP code example of nilportugues / laravel5-haljson
1. Go to this page and download the library: Download nilportugues/laravel5-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/ */
use Acme\Domain\Dummy\Post;
use Acme\Domain\Dummy\ValueObject\PostId;
use Acme\Domain\Dummy\User;
use Acme\Domain\Dummy\ValueObject\UserId;
use Acme\Domain\Dummy\Comment;
use Acme\Domain\Dummy\ValueObject\CommentId;
//$postId = 9;
//PostRepository::findById($postId);
$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'),
]
),
]
);
class PostMapping implements \NilPortugues\Api\Mappings\HalMapping {
public function getClass()
{
return 'Acme\Domain\Dummy\Post';
}
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' => ['name' => 'get_post'], //named route
'comments' => ['name' => 'get_post_comments'],//named route
];
}
public function getCuries()
{
return [
'name' => 'example',
'href' => "http://example.com/docs/rels/{rel}",
];
}
}
class PostIdMapping implements \NilPortugues\Api\Mappings\HalMapping{
public function getClass()
{
return 'Acme\Domain\Dummy\ValueObject\PostId';
}
public function getAlias()
{
return '';
}
public function getAliasedProperties()
{
return [];
}
public function getHideProperties()
{
return [];
}
public function getIdProperties()
{
return ['postId'];
}
public function getUrls()
{
return [
'self' => ['name' => 'get_post'],//named route
];
}
public function getCuries()
{
return [
'name' => 'example',
'href' => "http://example.com/docs/rels/{rel}",
];
}
}
class UserMapping implements \NilPortugues\Api\Mappings\HalMapping{
public function getClass()
{
return 'Acme\Domain\Dummy\User';
}
public function getAlias()
{
return '';
}
public function getAliasedProperties()
{
return [];
}
public function getHideProperties()
{
return [];
}
public function getIdProperties()
{
return ['userId'];
}
public function getUrls()
{
return [
'self' => ['name' => 'get_user'],//named route
'friends' => ['name' => 'get_user_friends'],//named route
'comments' => ['name' => 'get_user_comments'],//named route
];
}
public function getCuries()
{
return [
'name' => 'example',
'href' => "http://example.com/docs/rels/{rel}",
];
}
}
class UserIdMapping implements \NilPortugues\Api\Mappings\HalMapping{
public function getClass()
{
return 'Acme\Domain\Dummy\ValueObject\UserId';
}
public function getAlias()
{
return '';
}
public function getAliasedProperties()
{
return [];
}
public function getHideProperties()
{
return [];
}
public function getIdProperties()
{
return ['userId'];
}
public function getUrls()
{
return [
'self' => ['name' => 'get_user'],//named route
'friends' => ['name' => 'get_user_friends'],//named route
'comments' => ['name' => 'get_user_comments'],//named route
];
}
public function getCuries()
{
return [
'name' => 'example',
'href' => "http://example.com/docs/rels/{rel}",
];
}
}
class CommentMapping implements \NilPortugues\Api\Mappings\HalMapping{
public function getClass()
{
return 'Acme\Domain\Dummy\Comment';
}
public function getAlias()
{
return '';
}
public function getAliasedProperties()
{
return [];
}
public function getHideProperties()
{
return [];
}
public function getIdProperties()
{
return ['commentId'];
}
public function getUrls()
{
return [
'self' => ['name' => 'get_comment'],//named route
];
}
public function getCuries()
{
return [
'name' => 'example',
'href' => "http://example.com/docs/rels/{rel}",
];
}
}
class CommentIdMapping implements \NilPortugues\Api\Mappings\HalMapping{
public function getClass()
{
return 'Acme\Domain\Dummy\ValueObject\CommentId';
}
public function getAlias()
{
return '';
}
public function getAliasedProperties()
{
return [];
}
public function getHideProperties()
{
return [];
}
public function getIdProperties()
{
return ['commentId'];
}
public function getUrls()
{
return [
'self' => ['name' => 'get_comment'],//named route
];
}
public function getCuries()
{
return [
'name' => 'example',
'href' => "http://example.com/docs/rels/{rel}",
];
}
}
namespace App\Http\Controllers;
use Acme\Domain\Dummy\PostRepository;
use NilPortugues\Laravel5\HalJson\HalJsonResponseTrait;
class PostController extends \App\Http\Controllers\Controller
{
use HalJsonResponseTrait;
/**
* @var PostRepository
*/
protected $postRepository;
/**
* @var HalJson
*/
protected $serializer;
/**
* @param PostRepository $postRepository
* @param HalJson $HalJson
*/
public function __construct(PostRepository $postRepository, HalJson $HalJson)
{
$this->postRepository = $postRepository;
$this->serializer = $HalJson;
}
/**
* @param int $postId
*
* @return \Symfony\Component\HttpFoundation\Response
*/
public function getPostAction($postId)
{
$post = $this->postRepository->findById($postId);
return $this->response($this->serializer->serialize($post));
}
}
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);
protected function errorResponse($json);
protected function resourceCreatedResponse($json);
protected function resourceDeletedResponse($json);
protected function resourceNotFoundResponse($json);
protected function resourcePatchErrorResponse($json);
protected function resourcePostErrorResponse($json);
protected function resourceProcessingResponse($json);
protected function resourceUpdatedResponse($json);
protected function response($json);
protected function unsupportedActionResponse($json);
Loading please wait ...
Before you can download the PHP files, the dependencies should be resolved. This can take some minutes. Please be patient.