定制 yucadoo/polymorphic-fractal 二次开发

按需修改功能、优化性能、对接业务系统,提供一站式技术支持

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

yucadoo/polymorphic-fractal

Composer 安装命令:

composer require yucadoo/polymorphic-fractal

包简介

Polymorphic Transformer implementation for PHP League's Fractal package.

README 文档

README

Latest Version on Packagist Software License Build Status Coverage Status Quality Score Total Downloads

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.

统计信息

  • 总下载量: 3
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 4
  • 点击次数: 1
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 4
  • Watchers: 1
  • Forks: 0
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2020-04-16

承接程序开发

PHP开发

VUE

Vue开发

前端开发

小程序开发

公众号开发

系统定制

数据库设计

云部署

网站建设

安全加固