定制 quibble/transformer 二次开发

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

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

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, collection is simply a convenient front to array_map(Transformer::resource). In all other respects they work identically. The rest of this README will therefore only use resource.

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 我们能提供哪些服务?
定制开发 / 二次开发

基于 quibble/transformer 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。

BUG 修复 & 性能优化

线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。

项目外包 & 长期维护

承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。

yvsm@zunyunkeji.com QQ:316430983 微信:yvsm316 西安尊云信息科技 · 专注 PHP / Go / 分布式系统研发

统计信息

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

GitHub 信息

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

其他信息

  • 授权协议: MIT
  • 更新时间: 2016-10-02