saad/fractal
Composer 安装命令:
composer require saad/fractal
包简介
Wraper for spatie fractal package
README 文档
README
Install
You can pull in the package via composer:
composer require saad/fractal
The package will automatically register itself.
Laravel Version
this package is compatible with laravel versions >= 5.5
Changelog
V 1.2.0
- Add Strict Mode, so for null resources instead of returning empty array it will return null, By Default strict mode is Enabled
use
Exactly as spatie except that this package is automatically parse includes or excludes from parameters first if defined, otherwise it will look for query string includes and excludes
Default serializer is ArraySerializer
Console Generator
you can generate a new transformer class using the following command
the following command will create "App\Transformers\UserTransformer.php"
php artisan make:transformer 'App\User'
to create in nested folders "App\Transformers\Sub1\Sub2\UserTransformer.php"
php artisan make:transformer 'App\User' --nest='Sub1\Sub2' # Nest Name could be: # Sub1/Sub2 # /Sub1/Sub2/ # Sub1\\Sub2
Request Includes
will include defined includes from
availableIncludesarray
// assume that Request Url = /countries?include=name,code,iso $couintries = Country::all(); $transformed_data = \Saad\Fractal\Fractal::create($countries, new CountryTransformer()); // CountryTransformer will include name, code and iso
Force Includes
we could also pass includes to create method as the
4th argumentwhich will have the heighest periority than request include will include defined includes fromavailableIncludesarray
// assume that Request Url = /countries?include=name,code,iso $couintries = Country::all(); $transformed_data = \Saad\Fractal\Fractal::create($countries, new CountryTransformer(), null, 'name,iso'); // CountryTransformer will include only name and iso
Request Excludes
will exclude defined includes from
defaultIncludesarray
// assume that Request Url = /countries?exclude=name,code $couintries = Country::all(); $transformed_data = \Saad\Fractal\Fractal::create($countries, new CountryTransformer()); // CountryTransformer will exclude name and code from default includes
Force Excludes
we could also pass excludes to create method as the
5th argumentwhich will have the heighest periority than request include will include defined includes fromdefaultIncludesarray
// assume that Request Url = /countries?exclude=name,code,iso $couintries = Country::all(); $transformed_data = \Saad\Fractal\Fractal::create($countries, new CountryTransformer(), null, null, 'name'); // CountryTransformer will exclude only name from defaultIncludes
Transformer Abstract Class
this package has a base abstract Transformer Class Saad\Fractal\Transformers\TransformerAbstract you could use as the base class of your transformers, this class is based on extends League\Fractal\TransformerAbstract and adds the following features:
TransformerAbstract::strictMode(bool)
This mode added since V 1.2.0
null resources is returning empty array, when enable strict mode it will return NULL instead. By default strict mode is enabled
To control strict mode you could call one of these methods in one of the service providers boot method:
Enable Strict Mode (Enabled By Default):
TransformerAbstract::strictMode(true)
TransformerAbstract::enableStrictMode()
Disable Strict Mode:
TransformerAbstract::strictMode(false)
TransformerAbstract::disableStrictMode()
// Assume we have this transformer class CountryTransformer extends TransformerAbstract { ... includeRegions(Country $country) { // assume there are no regions return $this->null(); } } $transformer = new CountryTransformer(); $output = Fractal::create($country, $transformer); // output when strict mode is enabled (default status) [ ... 'regions' => null, ] // Disable Strict Mode TransformerAbstract::disableStrictMode(); $output = Fractal::create($country, $transformer); // output when strict mode is disabled [ ... 'regions' => [], ]
transform() replaced by transforWithDefault()
you should use
transforWithDefault()methodcinstead oftransform()method this is because the newaddEexternalfeature
addExternal($key, $value)
you can add external value to output
$transformer = new CountryTransformer(); $output = Fractal::create($country, $transformer); // assume this output of country is [ 'name' => 'Egypt', 'iso' => 'EG' ] // If we want to add another key to output $transformer->addExternal('new_key', 'Iam New Value'); $output = Fractal::create($country, $transformer); // Then output of country will be [ 'name' => 'Egypt', 'iso' => 'EG', 'new_key' => 'Iam New Value', ] // We can also add external which have a calculated value depends on transformed object // assume we want to add new key to output named 'name_iso' which its value is the concatenation of both 'name' and 'iso' properies $transformer->addExternal('name_iso', function ($country_object) { return $country_object->name . '_' . $country_object->iso; }); $output = Fractal::create($country, $transformer); // Then output of country will be [ 'name' => 'Egypt', 'iso' => 'EG', 'name_iso' => 'Egypt_EG', ]
addDefaultInclude(string|array $defaults_to_add)
will add provided keys to defaultIncludes array
// assume that defaultIncludes are ['id', 'name'] $transformer = new CountryTransformer(); $transformer->addDefaultInclude(['iso']); $output = Fractal::create($country, $transformer); // assume this output of country is [ 'id' => 1, 'name' => 'Egypt', 'iso' => 'EG' // added to defaultIncludes ]
Fractal Request Parser Singletone
this package also contains a singletone Saad\Fractal\FractalRequestParser
which is a helper class that provides usefull methods about Request includes and excludes
with the following methods :
Saad\Fractal\FractalRequestParser::includesHas($key_path)
Saad\Fractal\FractalRequestParser::excludesHas($key_path)
assume we have the following request URI
?include=name,sub.name:lang(ar),sub.country
$parser = Saad\Fractal\FractalRequestParser::getInstance(); // we can check the following $parser->includesHas('name'); // true $parser->includesHas('sub'); // true $parser->includesHas('sub.name'); // true $parser->includesHas('sub.country'); // true $parser->includesHas('iso'); // false $parser->includesHas('sub.country.name'); // false
the same for excludesHas()
saad/fractal 适用场景与选型建议
saad/fractal 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 8.88k 次下载、GitHub Stars 达 2, 最近一次更新时间为 2017 年 08 月 31 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「api」 「transform」 「laravel」 「fractal」 「spatie」 「lumen」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 saad/fractal 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 saad/fractal 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 saad/fractal 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
Behat package to transform human readable labels into selectors or page paths.
Transform data structures
Transform Craft Element and field data structures
Data sanitizer forked from Waavi/Sanitaize with support for Laravel 5 to 8. .
Transform Middleware for Relay
A PSR-7 compatible library for making CRUD API endpoints
统计信息
- 总下载量: 8.88k
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 2
- 点击次数: 7
- 依赖项目数: 2
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2017-08-31