ramasdev/simple-collection-transformer
最新稳定版本:1.0.1
Composer 安装命令:
composer require ramasdev/simple-collection-transformer
包简介
Simple data to Laravel collection transformer.
README 文档
README
Simple data to Laravel collection transformer, don't write Collection transformers anymore!
- Lets you utilize filters through callback function.
Install
composer require ramasdev/simple-collection-transformer
Usage
Create DTO/Model class and provide collection class through attributes to which need to transform:
use Ramasdev\SimpleCollectionTransformer\Attributes\CollectionAttribute; use Ramasdev\SimpleCollectionTransformer\Tests\Unit\Data\Collections\ChannelCollection; #[CollectionAttribute(ChannelCollection::class)] class Channel { public function __construct( private string $channelId, private string $channelType, private Carbon $lastRegistration ) { } public function getChannelId(): string { return $this->channelId; } public function getChannelType(): string { return $this->channelType; } public function getLastRegistration(): Carbon { return $this->lastRegistration; } }
Transform to collection with filters:
use Ramasdev\SimpleCollectionTransformer\CollectionTransformer; $actualCollection = $this->collectionTransformer->transform($data, function ($item) { if ($item['channel_id'] === 'b') { return false; } return new Channel($item['channel_id'], $item['channel_type'], new Carbon($item['last_registration'])); }); // Returns ChannelCollection
You can utilize it with you deserializer component also, for example:
use Ramasdev\SimpleCollectionTransformer\CollectionTransformer; $actualCollection = $this->collectionTransformer->transform($data, function ($item) { return $this->decoder->decodeArray((array) $item, Channel::class); }); // Returns ChannelCollection
统计信息
- 总下载量: 3
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 1
- 点击次数: 1
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2022-09-02