PHP code example of fanqingxuan / presenter

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);//不包含引用资源,输出单记录结构

  //模拟源数据
  $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);
    

   
  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();
  
  //原始数据
  $userlist = [
  	[
  		'user_id'   => '1',
  		'name'      => 'Json',
  		'email'     => '[email protected]',
  		'age'       => 32,
          'list'      =>  [
              [
                  'id'    =>  1,
                  'title' =>  '文章1',
                  'text'  =>  '文章1内容',
                  'status'=>  1
              ],
              [
                  'id'    =>  2,
                  'title' =>  '文章2',
                  'text'  =>  '文章2内容',
                  'status'=>  0
              ]
          ]
  	],
  	[
  		'user_id'   => '2',
  		'name'      => '范先生',
  		'email'     => '[email protected]',
  		'age'       => 29,
          'list'      =>  [
              [
                  'id'    =>  3,
                  'title' =>  '文章3',
                  'text'  =>  '文章3内容',
                  'status'=>  1
              ],
              [
                  'id'    =>  4,
                  'title' =>  '文章4',
                  'text'  =>  '文章4内容',
                  'status'=>  1
              ]
          ]
  	]
  ];
  
  class PostTransformer extends TransformerAbstract
  {
                      
      public function transform(array $post)
      {
          return [
              'post_id'   =>  $post['id'],
               'title'    =>  $post['title']
          ];
      }
      
  }
  
  class UserTransformer extends TransformerAbstract
  {
      protected $availableIncludes = ['posts'];
                      
      public function transform(array $userInfo)
      {
          return [
              'id'            => (int) $userInfo['user_id'],
              'username'      => $userInfo['name'],
              'email'         => $userInfo['email'],
          ];
      }
      
      public function 

  //方式1
  $data = $presenter->transform($userlist,new UserTransformer(['posts']));//包含引用记录,输出集合结构
  print_r($data);
  
  //方式2
  //包含引用记录,输出集合结构的另一种方式
  $userTransformer = new UserTransformer;
  $userTransformer->setAvailableIncludes('posts');
  $data = $presenter->transform($userlist,$userTransformer);//包含引用记录,输出集合结构
  print_r($data);