aol/transformers 问题修复 & 功能扩展

解决BUG、新增功能、兼容多环境部署,快速响应你的开发需求

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

aol/transformers

最新稳定版本:3.1.4

Composer 安装命令:

composer require aol/transformers

包简介

A package for normalizing property names and types from an external data store.

README 文档

README

Build Status Latest Stable Version Latest Unstable Version Total Downloads Code Climate

What is this?

Aol/Transformers provides a way to quickly handle two-way data transformations. This is useful for normalizing data from external or legacy systems in your application code or even for cleaning up and limiting responses from your external HTTP API.

But why not just fix the data at the source? If you can, do it! Often though, that's not an option, and that's when you need to "fix" the data at the application layer.

There are two terms you should know:

  • app - Short for "application" and this is the format we want to use in our application.
  • ext - Short for "external" and this is the format that is used externally.

Basic usage

In a very basic use case a new Transformer instance can be created and the transformation definitions can be defined on the fly. Take a look at the code and then we'll walk thru what's happening.

<?php

$post = ['Id' => '5', 'Title' => 'Awesome Post!', 'post_tags' => '["awesome"]'];

$transformer = new \Aol\Transformers\Transformer;
$transformer->define('id', 'Id', 'intval', 'strval');
$transformer->define('title', 'Title');
$transformer->define('tags', 'post_tags', 'json_decode', 'json_encode');

$post = $transformer->toApp($post);
// ['id' => 5, 'title' => 'Awesome Post!', 'tags' => ['awesome']];

We have a "Post" array from some external source. It could be MySql, Mongo, an API, the source doesn't really matter. The important part is that we have a post with a few issues. Here's a checklist of things we want to change:

  1. All of the key names should be lowercase snake_case. Oh, and post_tags is kinda silly, we'll make that simply 'tags`.
  2. The post id is always numerical, lets make that an integer.
  3. The post tags are currently a JSON string, lets turn that into a PHP array.

We can make all of these changes by creating a definition for each key. The first argument is the key we want to use in the application, the second argument is the key name that has been forced upon us externally. In many cases (such as title) its enough to just define those two fields.

In cases where we want to actually change the value we can pass in two more arguments. The third argument is a callable that will be applied when we convert to the application context, and the fourth argument is a callable that will be applied when we convert to the external context.

Take a look at the define signature:

public function define($app_name, $ext_name, callable $app_func = null, callable $ext_func, $app_args = [], $ext_args = []);
  • $app_name - This is the key name we want to use in the application.
  • $ext_name - This is the key name that has been forced upon us externally.
  • $app_func - This is an optional callable that will be applied when transforming an array to the application format.
  • $ext_func - This is an optional callable that will be applied when transforming an array to the external format.
  • $app_args - Optional arguments for the application callable
  • $ext_args - Optional arguments for the external callable

Subclass it

Often its useful to reuse a Transformer. In this create a new class that extends the Transformer class and implement the definitions in the constructor.

<?php

class Post extends \Aol\Transformers\Transformer
{
	public function __construct()
	{
		$this->define('id', 'Id', 'intval', 'strval');
		$this->define('title', 'Title');
		$this->define('tags', 'post_tags', 'json_decode', 'json_encode');
	}
}

Leveraging Traits

Many common definitions can be simplified by leveraging the wrapper methods provided by utility traits.

<?php

class Post extends \Aol\Transformers\Transformer
{
	use \Aol\Transformers\Utilities\MysqlTrait,
		\Aol\Transformers\Utilities\UtilityTrait;
		
	public function __construct()
	{
		$this->defineId('id', 'Id');
		$this->define('title', 'Title');
		$this->defineJson('tags', 'post_tags');
	}
}

Installation

$ composer require aol/transformers ^2

Contributing

...

License

This project is licensed under the MIT License - see the LICENSE file for details.

aol/transformers 适用场景与选型建议

aol/transformers 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 12.79k 次下载、GitHub Stars 达 38, 最近一次更新时间为 2014 年 06 月 10 日, 在 PHP 生态内属于活跃度较高的组件。

它主要适用于以下技术方向: 「mysql」 「mongo」 「layer」 「data store」 「transformation」 「normalizing」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。

我们在过去多个企业项目中使用过 aol/transformers 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。

围绕 aol/transformers 我们能提供哪些服务?
定制开发 / 二次开发

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

BUG 修复 & 性能优化

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

项目外包 & 长期维护

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

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

统计信息

  • 总下载量: 12.79k
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 39
  • 点击次数: 25
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 38
  • Watchers: 25
  • Forks: 4
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2014-06-10