PHP code example of brianbrian666 / tp-transformer

1. Go to this page and download the library: Download brianbrian666/tp-transformer 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/ */

    

brianbrian666 / tp-transformer example snippets




namespace app\transformer;

use League\Fractal\TransformerAbstract;

class UserTransformer extends TransformerAbstract
{
    public function transform($user)
    {
        return [
            'id' => $user['id'],
            'name' => $user['name'],
        ];
    }
}

use brianbrian666\TpTransformer\Response\Factory;

class UserController
{
    public function show($id)
    {
        $user = model('User')->find($id);

        return (new Factory())->item($user, \app\transformer\UserTransformer::class);
    }

    public function index()
    {
        $users = model('User')->select();

        return (new Factory())->collection($users, \app\transformer\UserTransformer::class);
    }

    public function page()
    {
        $users = model('User')->paginate(15);

        return (new Factory())->paginator($users, \app\transformer\UserTransformer::class);
    }
}

$factory->item($user, new UserTransformer());