1. Go to this page and download the library: Download fanqingxuan/presenter 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/ */
fanqingxuan / presenter example snippets
use Json\TransformerAbstract;
use Json\Presenter;
class BookTransformer extends TransformerAbstract
{
public function transform($book)
{
return [
'id' => (int) $book['id'],
'title' => $book['title'],
'year' => (int) $book['yr'],
];
}
}
$presenter = new Presenter();
//模拟源数据
$books =[
'id' => '1',
'title' => 'Hogfather',
'yr' => '1998',
'author_name' => 'Philip K Dick',
'author_email' => '[email protected]',
];
$data = $presenter->transform($books,new BookTransformer(),false);//不包含引用资源,输出单记录结构
//定义transformer
class LinkTransformer extends TransformerAbstract
{
public function transform($book)
{
return [
'rel' => 'self',
'uri' => '/books/'.$book['id'],
];
}
}
class BookTransformer extends TransformerAbstract
{
/**
* List of resources possible to lic function ooks,new BookTransformer(['links']),false);//包含引用记录,输出单记录结构
print_r($data);
//定义transformer
class LinkTransformer extends TransformerAbstract
{
public function transform($book)
{
return [
'rel' => 'self',
'uri' => '/books/'.$book['id'],
];
}
}
class BookTransformer extends TransformerAbstract
{
/**
* List of resources possible to lic function 单记录结构的另一种方式
$bookTransformer = new BookTransformer;
$bookTransformer->setAvailableIncludes('links');
$data = $presenter->transform($books,$bookTransformer,false);//包含引用记录,输出单记录结构
print_r($data);