rkulik/fractal

Convenience wrapper for Fractal

1.0.0 2019-02-10 11:38 UTC

This package is auto-updated.

Last update: 2024-04-10 22:54:52 UTC


README

Convenience wrapper for Fractal.

Requirements

This package requires PHP 7.2 or higher.

Install

Via composer:

$ composer require rkulik/fractal

Usage

As this package wraps Fractal, the general usage is pretty much the same. The examples listed below demonstrate the basic workflow. For further information please refer to the Fractal documentation.

Item examples

In the following examples an item gets transformed and returned as an array. The transformation is done using either a callback or a class.

Callback for transformer

<?php

require 'vendor/autoload.php';

$fractal = new \Rkulik\Fractal\Fractal(new \League\Fractal\Manager());

$product = [
    'id' => '123',
    'name' => 'T-shirt',
    'price' => '1290',
    'brand_name' => 'Nike',
    'gender' => 'm',
];

$transformer = function (array $product): array {
    return [
        'id' => (int)$product['id'],
        'name' => $product['name'],
        'price' => (int)$product['price'],
        'brand' => $product['brand_name'],
        'gender' => $product['gender'] === 'm' ? 'male' : 'female',
    ];
};

$item = $fractal->item($product, $transformer)->toArray();

Class for transformer

Using classes for transformation is the recommended way to do so, as those transformers are easily reusable.

<?php

require 'vendor/autoload.php';

class Transformer extends \League\Fractal\TransformerAbstract {
    public function transform(array $product): array
    {
        return [
            'id' => (int)$product['id'],
            'name' => $product['name'],
            'price' => (int)$product['price'],
            'brand' => $product['brand_name'],
            'gender' => $product['gender'] === 'm' ? 'male' : 'female',
        ];
    }
}

$fractal = new \Rkulik\Fractal\Fractal(new \League\Fractal\Manager());

$product = [
    'id' => '123',
    'name' => 'T-shirt',
    'price' => '1290',
    'brand_name' => 'Nike',
    'gender' => 'm',
];

$item = $fractal->item($product, new Transformer())->toArray();

Collection example

Transforming and paginating a collection using a cursor can be achieved as follows:

<?php

require 'vendor/autoload.php';

$fractal = new \Rkulik\Fractal\Fractal(new \League\Fractal\Manager());

$products = [
    [
        'id' => '123',
        'name' => 'T-shirt',
        'price' => '1290',
        'brand_name' => 'Nike',
        'gender' => 'm',
    ],
    [
        'id' => '456',
        'name' => 'Jacket',
        'price' => '19900',
        'brand_name' => 'Carhartt',
        'gender' => 'f',
    ],
    [
        'id' => '789',
        'name' => 'Trousers',
        'price' => '3990',
        'brand_name' => 'Only & Sons',
        'gender' => 'f',
    ],
];

$transformer = function (array $product): array {
    return [
        'id' => (int)$product['id'],
        'name' => $product['name'],
        'price' => (int)$product['price'],
        'brand' => $product['brand_name'],
        'gender' => $product['gender'] === 'm' ? 'male' : 'female',
    ];
};

$cursor = new \League\Fractal\Pagination\Cursor(null, null, 2, 3);

$collection = $fractal->collection([
    $products[0],
    $products[1],
], $transformer)->setCursor($cursor)->toArray();

Testing

$ composer test

Changelog

Please see CHANGELOG for more information what has changed recently.

Contributing

Please see CONTRIBUTING for details.

Security

If you discover any security-related issues, please email rene@kulik.io instead of using the issue tracker.

Credits

License

The MIT License (MIT). Please see License File for more information.