bfg/transformer
Composer 安装命令:
composer require bfg/transformer
包简介
README 文档
README
Install
composer require bfg/transformer
Description
The package is designed to transform data for the model (on the example obtained by the data from the API). And data from the model (for example, when you need to send data back on the API).
Usage
Create new transformer
php artisan make:transformer UserTransformer -m User
Describe all the fields that you will fill in the new class App\Transformers\UserTransformer in the $toModel variable:
use Bfg\Transformer\Transformer; ... class UserTransformer extends Transformer { /** * If your data that you received from * a third-party source have an Identifier, * then you need to specify this field. * @var string|null */ protected ?string $remoteId = "ID"; /** * An alternative source indicate the model * if it is not sent to the transformer. * @var string|null */ protected ?string $modelClass = User::class; /** * Mapping to send data generation into the model. * @var string[] */ protected array $toModel = [ 'FullName' => 'name', 'Email' => 'email', // or if you identical key names: 'name', 'email', // for related iteration ContactsTransformer::class // The transformer of contacts => 'contacts', // The relation name in the model ]; /** * Mapping for the direction of data generation * from the model, back to third-party service. * @var string[] */ protected array $fromModel = [ 'name' => ['FirstName', 'LastName'], 'address' => 'Address', 'email' => 'Email', 'phone' => 'Phone', ]; /** * To implement the data unloading mechanism * on third-party service. * @return void */ public function upload() { } }
Mutators
For fully transformation, it is often necessary to process a little more accurately, for this there are mutators in different directions:
use Bfg\Transformer\Transformer; ... class UserTransformer extends Transformer { ... protected array $toModel = [ 'FullName' => 'name', 'Email' => 'email', ]; protected array $fromModel = [ 'name' => ['FirstName', 'LastName'], 'email' => 'Email', ]; protected function toNameAttribute($dataValue) { $this->data; // All data is available on this property. $this->model; // The model is available on this property. return $dataValue; } protected function fromNameAttribute($modelValue) { return $modelValue; } protected function forFirstNameDataAttribute($modelValue) { return $modelValue; } protected function forLastNameDataAttribute($modelValue) { return $modelValue; } protected function forEmailDataAttribute($modelValue) { return $modelValue; } }
Casting
All transformation casting rules are completely copied from the
casting of Laravel Attributes
and their functionality is absolutely identical (except for the set of custom casts):
... protected $casts = [ 'views' => 'int' ]; ...
Applies to data, not to the model.
Model catch
In order to catch a model definition for a transformer (based on data), you can use the getModel method:
use Bfg\Transformer\Transformer; use App\Models\User; ... class UserTransformer extends Transformer { ... protected function getModel() { return User::where('remote_id', $this->data['ID'])->first() ?: parent::getModel(); } }
Data catch
For a transformer, you can determine the value for processing directly inside,
this mechanism allows you to generate built-in dependent nesting.
According to the rules, if a collection Illuminate\Support\Collection is returned,
then the selection of a transformer instance will be created for each entry.
use Bfg\Transformer\Transformer; use App\Models\User; ... /** * @property-read ApiService $api For example some "ApiService" class */ class UserTransformer extends Transformer { ... protected function getData() { return $this->api->findUser($this->model->remote_id); } }
Data to model
use App\Transformers\UserTransformer; ... $data = [ 'userName' => 'Thomas', 'userEmail' => 'thomas@example.com', ]; // for example, any data $model = UserTransformer::make() ->withData($data) ->toModel(); // Instance of User model // Or from any model $model = UserTransformer::make() ->withData($data) ->withModel(User::find(1)) ->toModel(); // With my instance of User model $model->save();
Data from model
use App\Models\User; use App\Transformers\UserTransformer; ... $model = User::find(1); $data = UserTransformer::make()->withModel($model)->toData()->data; // => ['userName' => 'Thomas','userEmail' => 'thomas@example.com'] // Or with you data filling $fillData = (object) ['userName' => null, 'userEmail' => null, 'otherData' => 'test'] UserTransformer::make()->withModel($model)->withData($fillData)->toData()->upload();
Next, the collection perceives all the methods of the model to the entire collection:
use App\Transformers\TransformerCollection; ... /** @var TransformerCollection $collection */ $collection->save();
To start the entire chain in the transaction:
use App\Transformers\TransformerCollection; ... /** @var TransformerCollection $collection */ $collection->transaction()->save(); // For additional updating $collection->transaction()->save()->update([ 'api_updated_at' => now() ]);
bfg/transformer 适用场景与选型建议
bfg/transformer 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 62.65k 次下载、GitHub Stars 达 0, 最近一次更新时间为 2022 年 03 月 24 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「extension」 「laravel」 「transformer」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 bfg/transformer 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 bfg/transformer 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 bfg/transformer 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
Serializer transformers outputting valid API responses in JSON, JSON API and HAL+JSON API formats.
Symfony 2 & 3 JSON API Transformer Package
A custom URL rule class for Yii 2 which allows to create translated URL rules
A simple library that allows transform any kind of data to native php data or whatever
The Yii2 extension uses jQuery jquery.carousel-1.1.min.js and makes image carousel from php array of structure defined.
HAL+JSON & HAL+XML API transformer outputting valid API responses.
统计信息
- 总下载量: 62.65k
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 0
- 点击次数: 28
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2022-03-24