PHP code example of jerry58321 / api-transform

1. Go to this page and download the library: Download jerry58321/api-transform 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/ */

    

jerry58321 / api-transform example snippets

markdown
// IndexController.php

...

public function index()
{
    /** @var Models/LoginLog $loginLog */
    $loginLog = LoginLog::with('user')->get();

    return LoginLogTransform::response(compact('loginLog'));
}
markdown
// Transforms/Models/UserTransform.php

...

class UserTransform extends Transform
{
    public function methodOutputKey(): array
    {
        return [
            'user' => false
        ];
    }

    public function __user(Resources $resource)
    {
        return [
            'account' => $resource->account,
            'name'    => $resource->name,
        ];
    }
}
markdown
// Transforms/Models/LoginLogTransform.php

...

class LoginLogTransform extends Transform
{
    public function methodOutputKey(): array
    {
        return [
            'loginLog' => 'login_log'
        ];
    }

    public function __loginLog(Resources $resources)
    {
        $user = UserTransform::quote(['user' => $resources->user]);

        return array_merge($user, [
            'ip'       => $resources->ip,
            'login_at' => $resources->login_at
        ]);
    }
}