quibble/transformer
Composer 安装命令:
composer require quibble/transformer
包简介
Transformers for query result( set)s
README 文档
README
Transformers for query result( sets)
Easily cast and transform a single result set or a collection of results from a database query.
Installation
$ cd /path/to/your/project
$ composer require quibble/transformer
Usage
Instantiate a transformer and either call the collection or resource method.
The first parameter is the query result, the second a callable used to determine
how to transform the result(s):
<?php use Quibble\Transformer\Transformer; $transformer = new Transformer; $results = $statement->fetchAll(); $results = $transformer->collection($results, function () {}); $result = $statement->fetch(); $result = $transformer->resource($result, function () {});
Internally,
collectionis simply a convenient front toarray_map(Transformer::resource). In all other respects they work identically. The rest of this README will therefore only useresource.
The default behaviour is to remove all numeric keys from resources as if one had
fetched it using PDO::FETCH_ASSOC. To override this you may instantiate the
transformer with a false parameter.
You can only transform results returned as an array. The assumption is that if you fetch "into" objects (models) these would do their own massaging. You will however see later on how we can transform into objects as well.
Defining a transformation
The key to specifying the transformation is to type hint the named parameters you want to have transformed. By default PDO returns most things as strings. We can simply specify a few fields we want to be cast:
<?php $result = $transformer->resource($result, function (int $id, bool $active) {});
We can also type hint any object. The assumption is that such an object will be instantiated with the original value as its first and only argument. E.g.:
<?php use Carbon\Carbon; $result = $transformer->resource($result, function (Carbon $datecreated) {});
Any fields not specifically transformed are passed verbatim, and any fields
mentioned in the transformed not present in the result are simply ignored. The
idea is that you specify transformers with all transformable fields for a
table, but your query doesn't necessarily do "SELECT *". This way you can
easily reuse transformers.
Modifying the return type
PHP7+ allows us to type-hint a return type. If a transformer does this, an
object of that type (or an array of them, in the case of collection) is
returned instead:
<?php $result = $transformer->resource($result, function () : stdClass {}); $result typeof stdClass; // true
Specifying multiple transformers
More than one transformer may be given; just add additional arguments. They are
applied in order and if more than one specifies a return type, only the last is
used. This allows you to quickly specify overrides for certain query results.
If a field would be transformed twice using this method, it is first cast back
to a string before being passed to the next transformer. If you instead would
like to pass the transformed value (because your decorator uses type hinting
in its constructor itself) specify it with a default null value.
Centralizing your code (DRY)
Instead of manually passing lambdas around, a much cleaner pattern would be to specify your transformers as class methods. You might do this on a model, or in a central interface. Remember, we're not actually calling these methods, we just want to inspect them!
<?php use Carbon\Carbon; interface MySchema { public function foo(int $id, Carbon $datecreated) : MyAwesomeModel; // A return type of array is actually the default. public function bar(int $id, bool $valid) : array; } $fooResult = $transformer->resource($fooResult, [MySchema::class, 'foo']); $barResult = $transformer->resource($barResult, [MySchema::class, 'bar']);
FAQ
Do I need any other Quibble module to use this?
Nope. You don't even need PDO as long as you're dealing with arrays.
quibble/transformer 适用场景与选型建议
quibble/transformer 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 227 次下载、GitHub Stars 达 0, 最近一次更新时间为 2016 年 10 月 02 日, 在 PHP 生态内属于活跃度较高的组件。
我们在过去多个企业项目中使用过 quibble/transformer 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 quibble/transformer 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
统计信息
- 总下载量: 227
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 0
- 点击次数: 4
- 依赖项目数: 2
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2016-10-02