2017 © Pedro Peláez
 

library fractal-bundle

A Symfony2 Bundle for Fractal by League. Implements dependency injection into your transformers

image

paymaxi/fractal-bundle

A Symfony2 Bundle for Fractal by League. Implements dependency injection into your transformers

  • Monday, February 19, 2018
  • by dzubchik
  • Repository
  • 1 Watchers
  • 1 Stars
  • 815 Installations
  • PHP
  • 0 Dependents
  • 0 Suggesters
  • 7 Forks
  • 0 Open issues
  • 6 Versions
  • 28 % Grown

The README.md

League Fractal Symfony Bundle

Build Status Quality Score ![Coverage Status][ico-scrutinizer] Latest Version on Packagist ![Total Downloads][ico-downloads] ![Software License][ico-license], (*1)

This bundle provides integration of league/fractal for Symfony. In addition it allows you to use transformers as a services., (*2)

Getting Started

First of all you need to add dependency to composer.json:, (*3)

composer require paymaxi/fractal-bundle

Then register bundle in app/AppKernel.php:, (*4)

public function registerBundles()
{
    return [
        // ...
        new Paymaxi\FractalBundle\PaymaxiFractalBundle(),
    ];
}

Now we can write and use fractal transformers:, (*5)

Using Transformers as Services

There are several cases when you need to pass some dependencies into transformer. The common one is role/scope based results in transformers. For example you need to show email field only for administrators or owner of user profile:, (*6)

class UserTransformer extends TransformerAbstract
{
    private $authorizationCheker;

    public function __construct(AuthorizationChecker $authorizationCheker)
    {
        $this->authorizationCheker = $authorizationCheker;
    }

    public function transform(User $user)
    {
        $data = [
            'id' => $user->id(),
            'name' => $user->name(),
        ];

        if ($this->authorizationChecker->isGranted(UserVoter::SEE_EMAIL, $user)) {
            $data['email'] = $user->email();
        }

        return $data;
    }
}

Then you could just register this class as service, and pass service ID as transformer. This bundle then will try to get it from container:, (*7)

$resource = new Collection($users, 'app.transformer.user');

This works in includes as well:, (*8)

public function includeFriends(User $user)
{    
    return $this->collection($user->friends(), 'app.transformer.user');
}

You could see example of how to use transformers in sample application which is used in test suites., (*9)

License

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

The Versions

19/02 2018

dev-master

9999999-dev

A Symfony2 Bundle for Fractal by League. Implements dependency injection into your transformers

  Sources   Download

MIT

The Requires

 

The Development Requires

19/02 2018

v1.0.4

1.0.4.0

A Symfony2 Bundle for Fractal by League. Implements dependency injection into your transformers

  Sources   Download

MIT

The Requires

 

The Development Requires

08/12 2017

v1.0.3

1.0.3.0

A Symfony2 Bundle for Fractal by League. Implements dependency injection into your transformers

  Sources   Download

MIT

The Requires

 

The Development Requires

28/07 2017

v1.0.2

1.0.2.0

A Symfony2 Bundle for Fractal by League. Implements dependency injection into your transformers

  Sources   Download

MIT

The Requires

 

The Development Requires

30/05 2017

v1.0.1

1.0.1.0

A Symfony2 Bundle for Fractal by League. Implements dependency injection into your transformers

  Sources   Download

MIT

The Requires

 

The Development Requires

30/05 2017

v1.0.0

1.0.0.0

A Symfony2 Bundle for Fractal by League. Implements dependency injection into your transformers

  Sources   Download

MIT

The Requires

 

The Development Requires