2017 © Pedro Peláez
 

library fractal-views

A view abstraction for fractal

image

lumenated/fractal-views

A view abstraction for fractal

  • Friday, April 7, 2017
  • by TFarla
  • Repository
  • 1 Watchers
  • 1 Stars
  • 5 Installations
  • PHP
  • 0 Dependents
  • 0 Suggesters
  • 0 Forks
  • 0 Open issues
  • 3 Versions
  • 0 % Grown

The README.md

Fractal views

A view abstraction for Fractal making it more simple to integrate inside a framework., (*1)

fractal views Build Status Coverage Status Packagist Packagist, (*2)

Installation

composer require lumenated/fractal-views

Configuration

In order to use Fractal views for a resource we need to implement a transformer to map the resource to a serializable array and a view to present our resource to the consumer. A View extends the Lumenated\FractalViews\Views class which exposes two methods: - renderOne which renders a single resource to an array - renderMany which renders multiple objects to an array with pagination support, (*3)

class BookView extends Lumenated\FractalViews\View 
{
  // The fractal transformer that has to be used for this view
  protected $transformerClass = BookTransformer::class;
}

Next up we need to implement the BookTransformer. Below is an example from the fractal documentation:, (*4)

<?php
namespace Acme\Transformer;

use Acme\Model\Book;
use League\Fractal;

class BookTransformer extends Fractal\TransformerAbstract
{
    public function transform(Book $book)
    {
        return [
            'id'      => (int) $book->id,
            'title'   => $book->title,
            'year'    => (int) $book->yr,
            'links'   => [
                [
                    'rel' => 'self',
                    'uri' => '/books/'.$book->id,
                ]
            ],
        ];
    }
}

Usage

After configuring our view they can be used inside our project. Below is an example how to use them in a Lumen controller:, (*5)

namespace \App\Http\Controllers;

class BookController extends Controller 
{
  private $view;

  public function __construct(BookView $view) 
  {
    $this->view = $view;

  }

  public function get($id) 
  {
    $book = Book::findOrFail($id);

    return response()->json($this->view->render($book));
  }

  public function getAll() 
  {
    $books = Book::all();

    return response()->json($this->view->render($books));
  }
}

Running tests

after installing the dependencies with:, (*6)

composer install

Execute the following command to execute the tests:, (*7)

vendor/bin/phpunit

The Versions

07/04 2017

dev-master

9999999-dev

A view abstraction for fractal

  Sources   Download

The Requires

 

The Development Requires

07/04 2017

1.1.0

1.1.0.0

A view abstraction for fractal

  Sources   Download

The Requires

 

The Development Requires

24/03 2017

1.0.0

1.0.0.0

A view abstraction for fractal

  Sources   Download

The Requires

 

The Development Requires