定制 selective/transformer 二次开发

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

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

selective/transformer

Composer 安装命令:

composer require selective/transformer

包简介

A strictly typed array transformer with dot-access, fluent interface and filters.

README 文档

README

A strictly typed array transformer with dot access and fluent interface. The mapped result can be used for JSON responses and many other things.

Latest Version on Packagist Software License Build Status Total Downloads

Table of Contents

Requirements

  • PHP 8.2 - 8.5

Installation

composer require selective/transformer

Introduction

This Transformer component provides functionality to map, cast and loop array values from an array or object to another array.

Converting complex data with simple PHP works by using a lot of type casting, if conditions and looping through the data with foreach(). This leads to very high cyclomatic complexity and nesting depth, and thus poor "code rating".

Before: Conditions: 9, Paths: 256, CRAP Score: 9

Click to expand!

After: Conditions: 1, Paths: 1, CRAP Score: 1

Click to expand!

Use Cases

When building an API it is common for people to just grab stuff from the database and pass it to json_encode(). This might be passable for “trivial” APIs but if they are in use by the public, or used by mobile applications then this will quickly lead to inconsistent output. The Transformer is able to create a “barrier” between source data and output, so schema changes do not affect users.

The Transformer works also very well to put any kind of database resultset (e.g. from PDO) into a new data structure.

The uses cases are not limited.

Dot access

You can copy any data from the source array to any sub-element of the destination array using the dot-syntax.

<?php
use Selective\Transformer\ArrayTransformer;

$transformer = new ArrayTransformer();

$transformer->map('firstName', 'address.first_name')
    ->map('lastName', 'address.last_name')
    ->map('invoice.items', 'root.sub1.sub2.items');

// ...

Object access

It's possible to access the properties of an object using the dot notation.

use Selective\Transformer\ArrayTransformer;

$transformer = new ArrayTransformer();

$transformer->map('bar1', 'foo.bar', 'string')
    ->map('bar2', 'foo.bar2.0', 'string')
    ->map('sub.sub2.sub3', 'foo.bar2.1', 'string');

$user = new stdClass();
$user->foo = new stdClass();
$user->foo->bar = 'Hello Bar';
$user->foo->bar2 = [
    0 => 'Test 0',
    1 => 'Test 1',
];

$result = $transformer->toArray((array)$user);

The result:

[
    'bar1' => 'Hello Bar',
    'bar2' => 'Test 0',
    'sub' => [
        'sub2' => [
            'sub3' => 'Test 1',
        ],
    ],
];

Transforming

Transforming arrays

For the sake of simplicity, this example has been put together as though it was one file. In reality, you would spread the manager initiation, data collection and JSON conversion into separate parts of your application.

Sample data:

$data = [
    'first_name' => 'Sally',
    'last_name' => '',
    'email' => 'sally@example.com',
];
<?php

use Selective\Transformer\ArrayTransformer;

$transformer = new ArrayTransformer();

$transformer->map('firstName', 'first_name')
    ->map('lastName', 'last_name')
    ->map('email', 'email');
    
$result = $transformer->toArray($data);

The result:

[
    'firstName' => 'Sally',
    'email' => 'sally@example.com',
];

Transforming list of arrays

The method toArrays is able to transform a list of arrays.

This can be useful if you want to transform a resultset from a database query, or a response payload from an API.

Example:

$transformer = new ArrayTransformer();

$transformer->map('id', 'id', $transformer->rule()->integer())
    ->map('first_name', 'first_name', $transformer->rule()->string())
    ->map('last_name', 'last_name', $transformer->rule()->string())
    ->map('phone', 'phone', $transformer->rule()->string())
    ->map('enabled', 'enabled', $transformer->rule()->boolean());

$rows = [];
$rows[] = [
    'id' => '100',
    'first_name' => 'Sally',
    'last_name' => '',
    'phone' => null,
    'enabled' => '1',
];

$rows[] = [
    'id' => '101',
    'first_name' => 'Max',
    'last_name' => 'Doe',
    'phone' => '+123456789',
    'enabled' => '0',
];

$result = $transformer->toArrays($rows);

The result:

[
    [
        'id' => 100,
        'first_name' => 'Sally',
        'enabled' => true,
    ],
    [
        'id' => 101,
        'first_name' => 'Max',
        'last_name' => 'Doe',
        'phone' => '+123456789',
        'enabled' => false,
    ],
]

Mapping Rules

Simple mapping rules

Using strings, separated by |, to define a filter chain:

<?php

use Selective\Transformer\ArrayTransformer;

$transformer = new ArrayTransformer();

$transformer->map('firstName', 'first_name', 'string|required')
    ->map('lastName', 'last_name', 'string|required')
    ->map('email', 'email', 'string|required');

$data = [
    'first_name' => 'Sally',
    'last_name' => '',
    'email' => 'sally@example.com',
];
    
$result = $transformer->toArray($data);

Because lastName is blank but required the result looks like this:

[
    'firstName' => 'Sally',
    'lastName' => '',
    'email' => 'sally@example.com',
];

Complex mapping rules

For complexer mapping rules there is a fluent interface available. To create a new rule use the rule method and pass it as 3rd. parameter to the map method.

<?php

use Selective\Transformer\ArrayTransformer;

$transformer = new ArrayTransformer();

$transformer->map('firstName', 'first_name', $transformer->rule()->string()->required())
    ->map('lastName', 'last_name', $transformer->rule()->string()->required())
    ->map('email', 'email', $transformer->rule()->string()->required());

$data = [
    'first_name' => 'Sally',
    'last_name' => '',
    'email' => 'sally@example.com',
];

$result = $transformer->toArray($data);

Because lastName is blank but required the result looks like this:

[
    'firstName' => 'Sally',
    'lastName' => '',
    'email' => 'sally@example.com',
];

Filter

Most filters are directly available as method.

// Cast value to string, convert blank to null
$transformer->rule()->string();

// Cast value to string, allow blank string ''
$transformer->rule()->string(true);

// Cast value to int
$transformer->rule()->integer();

// Cast value to float
$transformer->rule()->float();

// Cast value to bool
$transformer->rule()->boolean();

// Cast value to datetime string, default: Y-m-d H:i:s
$transformer->rule()->date();

// Cast value to date string
$transformer->rule()->date('Y-m-d');

// Format value to number using the number_format function
$transformer->rule()->number();

// Format value to number with a custom number format
$transformer->rule()->number(2, '.', ',');

// Cast value to array
$transformer->rule()->array();

// Cast value using a custom callback function
$transformer->rule()->callback(
    function ($value) {
        return 'My custom value: ' . $value;
    }
);

// Set fixed value
$transformer->set('bar.0.item', 'default-value');

// Apply transformation to array item
$transformer->rule()->transform(
    function (ArrayTransformer $transformer) {
        $transformer
            ->map('id', 'id', 'integer')
            ->map('first_name', 'first_name', 'string');
    }
);

// Apply transformation to a list of arrays
$transformer->rule()->transformList(
    function (ArrayTransformer $transformer) {
        $transformer
            ->map('id', 'id', 'integer')
            ->map('first_name', 'first_name', 'string');
       }
);

Custom Filter

You can also add your own custom filter:

$transformer = new ArrayTransformer();

// Add a trim filter using the native trim function
$transformer->registerFilter('trim', 'trim');

// Usage
$transformer->map('destination', 'source', 'trim');

// or

$transformer->map('destination', 'source', $transformer->rule()->filter('trim'));

Add a custom filter using a callback:

$transformer = new ArrayTransformer();

$transformer->registerFilter(
    'custom1',
    function ($value) {
        return 'Custom value: ' . $value;
    }
);

// Usage
$transformer->map('destination', 'source', 'custom1');

// or

$transformer->map('destination', 'source', $transformer->rule()->filter('custom1'));

// It is possible to chain multiple filters
$transformer->map(
    'destination',
    'source',
    $transformer->rule()->filter('custom1')->filter('trim')->required()->default('example')
);

Define a custom filter using a mapping specific callback:

$transformer = new ArrayTransformer();

$transformer->map(
    'destination',
    'source',
    $transformer->rule()->callback(
        function ($value) {
            return 'Callback value: ' . $value;
        }
    )
);

There are even more filter classes available that can be registered manually:

use Selective\Transformer\Filter\SprintfFilter;

$transformer = new ArrayTransformer();

// Convert the value using the sprintf function
$transformer->registerFilter('sprintf', new SprintfFilter());

// Usage
$transformer->map('destination', 'source', $transformer->rule()->filter('sprintf', 'Count: %d'));

You can also implement and register your own filter classes as well.

Examples

JSON conversion

You can use your own json component or just the native json_encode function.

// Turn all of that into a JSON string
$json = (string)json_encode($transformer->toArray($data));

Writing JSON to a PSR-7 response object:

// Set correct header
$response = $response->withHeader('Content-Type', 'application/json');

// Write json string to the response body
$response->getBody()->write($json);

return $response;

PDO resultset conversion

use Selective\Transformer\ArrayTransformer;

$pdo = new PDO($dsn, $username, $password, $options);

$statement = $pdo->query('SELECT id, username, first_name FROM users');
$rows = $statement->fetchAll();

$transformer = new ArrayTransformer();

$transformer->map('id', 'id', 'integer')
    ->map('username', 'username', 'string|required')
    ->map('first_name', 'first_name', 'string|required');
    
$result = $transformer->toArrays($rows);

The result:

[
    [
        'id' => 1,
        'username' => 'sally',
        'first_name' => 'Sally',
    ]  
    // ... 
];

Similar packages

License

The MIT License (MIT). Please see License File for more information.

selective/transformer 适用场景与选型建议

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

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

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

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

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

BUG 修复 & 性能优化

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

项目外包 & 长期维护

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

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

统计信息

  • 总下载量: 19.81k
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 38
  • 点击次数: 27
  • 依赖项目数: 3
  • 推荐数: 0

GitHub 信息

  • Stars: 38
  • Watchers: 2
  • Forks: 3
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2021-01-26