wandu/restifier

This package is abandoned and no longer maintained. No replacement package was suggested.

Restify Data to the RESTFul API Output.

v4.0.0-beta2 2018-02-10 07:53 UTC

This package is not auto-updated.

Last update: 2021-04-16 19:35:32 UTC


README

Latest Stable Version Latest Unstable Version Total Downloads License

Transform Data to the RESTFul API Output.

Installation

composer require wandu/restifier

Documentation

Example

$restifier = new Restifier();
$restifier->addTransformer(SampleUser::class, new SampleUserTransformer());
$restifier->addTransformer(SampleCustomer::class, new SampleCustomerTransformer());

$user = new SampleUser([
    'username' => 'wan2land',
    'customer' => new SampleCustomer([
        'address' => 'seoul blabla',
        'paymentmethods' => [], // critical data
    ]),
]);

static::assertEquals([
    "username" => "wan2land",
    'customer' => [
        'address' => 'seoul blabla',
    ],
], $restifier->restify($user));

Restifier

class Restifier {
    public function addTransformer(string $classNameOrInterfaceName, callable $transformer);
    
    public function restify($resource, array $includes = [], callable $transformer = null): array|null
    
    public function restifyMany($resource, array $includes = [], callable $transformer = null): array
} 

Transformer

Transformer is callable. It is recommended to use the callable class that contain __invoke method.

Example

<?php
namespace Wandu\Restifier\Sample;

use Wandu\Restifier\Contracts\Restifiable;

class SampleUserTransformer
{
    public function __invoke(SampleUser $user, Restifiable $restifier, array $includes = [])
    {
        return [
            'username' => $user->username,
            'customer' => $restifier->restify($user->customer),
        ];
    }

    public function customer(SampleUser $user, Restifiable $restifier, array $includes = [])
    {
        return [
            'customer' => $restifier->restify($user->customer, $includes),
        ];
    }

    public function profile(SampleUser $user, Restifiable $restifier, array $includes = [])
    {
        return [
            'profile' => $user->profile,
        ];
    }
}