2017 © Pedro Peláez
 

library fractal-bundle

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

image

samj/fractal-bundle

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

  • Sunday, December 17, 2017
  • by samjarrett
  • Repository
  • 3 Watchers
  • 16 Stars
  • 35,355 Installations
  • PHP
  • 0 Dependents
  • 0 Suggesters
  • 7 Forks
  • 0 Open issues
  • 9 Versions
  • 26 % Grown

The README.md

League Fractal Symfony Bundle

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

Getting Started

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

composer require samj/fractal-bundle

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

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

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

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:, (*5)

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:, (*6)

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

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

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., (*8)

The Versions

17/12 2017

dev-master

9999999-dev

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

  Sources   Download

MIT

The Requires

 

The Development Requires

17/12 2017

2.1.0

2.1.0.0

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

  Sources   Download

MIT

The Requires

 

The Development Requires

14/10 2017

2.0.0

2.0.0.0

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

  Sources   Download

MIT

The Requires

 

The Development Requires

11/09 2017

2.x-dev

2.9999999.9999999.9999999-dev

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

  Sources   Download

MIT

The Requires

 

The Development Requires

12/03 2017

1.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

18/10 2016

0.3.0

0.3.0.0

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

  Sources   Download

MIT

The Requires

 

The Development Requires

16/08 2015

0.2.1

0.2.1.0

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

  Sources   Download

MIT

The Requires

 

16/08 2015

0.2.0

0.2.0.0

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

  Sources   Download

MIT

The Requires

 

07/09 2014

0.1.0

0.1.0.0

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

  Sources   Download

MIT

The Requires