PHPackages                             yucadoo/polymorphic-fractal - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [API Development](/categories/api)
4. /
5. yucadoo/polymorphic-fractal

ActiveLibrary[API Development](/categories/api)

yucadoo/polymorphic-fractal
===========================

Polymorphic Transformer implementation for PHP League's Fractal package.

1.0.0(6y ago)43MITPHPPHP ~7.2

Since Mar 14Pushed 6y ago1 watchersCompare

[ Source](https://github.com/yucadoo/polymorphic-fractal)[ Packagist](https://packagist.org/packages/yucadoo/polymorphic-fractal)[ Docs](https://github.com/yucadoo/polymorphic-fractal)[ RSS](/packages/yucadoo-polymorphic-fractal/feed)WikiDiscussions master Synced today

READMEChangelog (1)Dependencies (4)Versions (3)Used By (0)

Polymorphic Fractal Transformer
===============================

[](#polymorphic-fractal-transformer)

[![Latest Version on Packagist](https://camo.githubusercontent.com/6d76e13ea421c8ee7f4fca9ab7431385da687c1ee0caf17c5d163d1422af0063/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f762f79756361646f6f2f706f6c796d6f72706869632d6672616374616c2e7376673f7374796c653d666c61742d737175617265)](https://packagist.org/packages/yucadoo/polymorphic-fractal)[![Software License](https://camo.githubusercontent.com/55c0218c8f8009f06ad4ddae837ddd05301481fcf0dff8e0ed9dadda8780713e/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f6c6963656e73652d4d49542d627269676874677265656e2e7376673f7374796c653d666c61742d737175617265)](LICENSE.md)[![Build Status](https://camo.githubusercontent.com/bd99e7c193cd3348ccb2dd5492056533c37a7fa1a5d4f5a4274d631808b6712b/68747470733a2f2f696d672e736869656c64732e696f2f7472617669732f79756361646f6f2f706f6c796d6f72706869632d6672616374616c2f6d61737465722e7376673f7374796c653d666c61742d737175617265)](https://travis-ci.org/yucadoo/polymorphic-fractal)[![Coverage Status](https://camo.githubusercontent.com/ba82f95c9e77dfaaae9c2139d47290a069c3d963ba72559bfa02a2a6130f4141/68747470733a2f2f696d672e736869656c64732e696f2f7363727574696e697a65722f636f7665726167652f672f79756361646f6f2f706f6c796d6f72706869632d6672616374616c2e7376673f7374796c653d666c61742d737175617265)](https://scrutinizer-ci.com/g/yucadoo/polymorphic-fractal/code-structure)[![Quality Score](https://camo.githubusercontent.com/37f85c6e72886c3a80d9dca4ae20e26b2f9affc755be67f027f172d1a17448b2/68747470733a2f2f696d672e736869656c64732e696f2f7363727574696e697a65722f672f79756361646f6f2f706f6c796d6f72706869632d6672616374616c2e7376673f7374796c653d666c61742d737175617265)](https://scrutinizer-ci.com/g/yucadoo/polymorphic-fractal)[![Total Downloads](https://camo.githubusercontent.com/7f16f136d0f3bb8a5ccbc71ceed1fec09ee54959ac81199758031853ae4fdaac/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f64742f79756361646f6f2f706f6c796d6f72706869632d6672616374616c2e7376673f7374796c653d666c61742d737175617265)](https://packagist.org/packages/yucadoo/polymorphic-fractal)

Polymorphic transformer implementation for PHP League's [Fractal package](https://github.com/thephpleague/fractal). 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](https://github.com/php-fig/fig-standards/blob/master/accepted/PSR-1-basic-coding-standard.md), [PSR-2](https://github.com/php-fig/fig-standards/blob/master/accepted/PSR-2-coding-style-guide.md), [PSR-4](https://github.com/php-fig/fig-standards/blob/master/accepted/PSR-4-autoloader.md) and [PSR-11](https://github.com/php-fig/fig-standards/blob/master/accepted/PSR-11-container.md). If you notice compliance oversights, please send a patch via pull request.

Install
-------

[](#install)

Via Composer

```
$ composer require yucadoo/polymorphic-fractal
```

Usage
-----

[](#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

[](#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](link-singleton-container) before passing it into the `AliasContainer`.

Change log
----------

[](#change-log)

Please see [CHANGELOG](CHANGELOG.md) for more information on what has changed recently.

Testing
-------

[](#testing)

```
$ composer test
```

Contributing
------------

[](#contributing)

Please see [CONTRIBUTING](CONTRIBUTING.md) and [CODE\_OF\_CONDUCT](CODE_OF_CONDUCT.md) for details.

Security
--------

[](#security)

If you discover any security related issues, please email  instead of using the issue tracker.

Credits
-------

[](#credits)

- [Hrvoje Jukic](https://github.com/yucadoo)
- [All Contributors](../../contributors)

License
-------

[](#license)

The MIT License (MIT). Please see [License File](LICENSE.md) for more information.

###  Health Score

24

—

LowBetter than 32% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity7

Limited adoption so far

Community7

Small or concentrated contributor base

Maturity52

Maturing project, gaining track record

 Bus Factor1

Top contributor holds 100% of commits — single point of failure

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

###  Release Activity

Cadence

Unknown

Total

1

Last Release

2215d ago

### Community

Maintainers

![](https://www.gravatar.com/avatar/9c6e704cc57891bf1868f1afa8fda7068d53ba7fafdaabc630fec179c517665c?d=identicon)[Firtzberg](/maintainers/Firtzberg)

---

Top Contributors

[![Firtzberg](https://avatars.githubusercontent.com/u/8490119?v=4)](https://github.com/Firtzberg "Firtzberg (7 commits)")

---

Tags

apitransformfractalyucapolymorphic

###  Code Quality

TestsPHPUnit

Code StylePHP\_CodeSniffer

### Embed Badge

![Health badge](/badges/yucadoo-polymorphic-fractal/health.svg)

```
[![Health](https://phpackages.com/badges/yucadoo-polymorphic-fractal/health.svg)](https://phpackages.com/packages/yucadoo-polymorphic-fractal)
```

###  Alternatives

[spatie/laravel-fractal

An easy to use Fractal integration for Laravel applications

1.9k15.1M98](/packages/spatie-laravel-fractal)[spatie/fractalistic

A developer friendly wrapper around Fractal

38715.3M8](/packages/spatie-fractalistic)[flugger/laravel-responder

A Laravel Fractal package for building API responses, giving you the power of Fractal and the elegancy of Laravel.

8901.5M4](/packages/flugger-laravel-responder)[yajra/laravel-datatables-fractal

Laravel DataTables Fractal Plugin.

966.9M29](/packages/yajra-laravel-datatables-fractal)[ellipsesynergie/api-response

Simple package to handle response properly in your API

3751.4M20](/packages/ellipsesynergie-api-response)[cyvelnet/laravel5-fractal

A simple fractal service provider and transformer generator with model attributes for laravel &gt;=5.

79187.2k1](/packages/cyvelnet-laravel5-fractal)

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
