yucadoo / polymorphic-fractal
Polymorphic Transformer implementation for PHP League's Fractal package.
Requires
- php: ~7.2
- league/fractal: ^0.19.2
- mouf/alias-container: ^2.0
Requires (Dev)
- phpunit/phpunit: >=8.0
- squizlabs/php_codesniffer: ^3.0
Suggests
- yucadoo/singleton-container: Allows caching and reuse of transformers.
This package is auto-updated.
Last update: 2024-12-16 20:23:36 UTC
README
Polymorphic transformer implementation for PHP League's Fractal package. Useful for feeds, e.g. notification feed. The polymorphic transformer can be used as any other transformer and is therefore compatible with packages built on top of Fractal. This package is compliant with PSR-1, PSR-2, PSR-4 and PSR-11. If you notice compliance oversights, please send a patch via pull request.
Install
Via Composer
$ composer require yucadoo/polymorphic-fractal
Usage
use League\Fractal\Manager; use League\Fractal\Resource\Collection; use Mouf\AliasContainer\AliasContainer; use YucaDoo\PolymorphicFractal\Transformer as PolymorphicTransformer; // Get heterogeneous data $objects = array( new Like($liker, $post), new Comment($commentator, $post), ); // Create transformer instance, your framework can probably do this automatically. /** @var Psr\Container\ContainerInterface */ $container; // Wrap framework specific container with aliasing decorator. // This allows us to define mapping from item classes to transformer classes. // We'll call this transformer registry. $registry = new AliasContainer($container); $polymorphicTransformer = new PolymorphicTransformer($registry); // The registry can also be got using the getRegistry() method $registry = $polymorphicTransformer->getRegistry(); // Configure registry, you'll probably do this in a service provider or in a transformer subclass. $registry->alias(Like::class, LikeTransformer::class); $registry->alias(Comment::class, CommentTransformer::class); // Configure manager and use the transformer as usual $manager = new League\Fractal\Manager(); // Optionally set serializer // Optionally parse includes and excludes $resource = new Collection($objects, $polymorphicTransformer); $manager->createData($resource)->toArray();
As shown in the example above the class of the transformation data is used to get the transformer from the transformer registry. This behvaiour can be modified by overriding the getRegistryKey()
method.
use Mouf\AliasContainer\AliasContainer; use YucaDoo\PolymorphicFractal\Transformer as PolymorphicTransformer; class NotificationTransformer extends PolymorphicTransformer { public function __construct(AliasContainer $registry) { parent::__construct($registry); $registry->alias('like', LikeTransformer::class); $registry->alias('comment', CommentTransformer::class); } protected function getRegistryKey($data) { return $data['type']; } } $notifications = array( array( 'type' => 'like' 'liker' => ..., ... ), array( 'type' => 'comment' 'commentator' => ..., ... ), ); $resource = new Collection($notifications, $notificationTransformer);
Pro tip
To prevent repeated instantiation of the same transformer wrap your framework's IoC cointainer with the SingletonContainer
decorator provided by the yucadoo/singleton-container package before passing it into the AliasContainer
.
Change log
Please see CHANGELOG for more information on what has changed recently.
Testing
$ composer test
Contributing
Please see CONTRIBUTING and CODE_OF_CONDUCT for details.
Security
If you discover any security related issues, please email hrcajuka@gmail.com instead of using the issue tracker.
Credits
License
The MIT License (MIT). Please see License File for more information.