PHP code example of yucadoo / polymorphic-fractal

1. Go to this page and download the library: Download yucadoo/polymorphic-fractal library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

yucadoo / polymorphic-fractal example snippets

 php
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);