承接 e0ipso/shaper 相关项目开发

从需求分析到上线部署,全程专人跟进,保证项目质量与交付效率

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

e0ipso/shaper

Composer 安装命令:

composer require e0ipso/shaper

包简介

Lightweight library to handle in and out transformations in PHP.

README 文档

README

Coverage Status Build Status

Shaper

Shaper is a simple library that enables type safe data transformations. You can either have simple transformations or queued transformations.

One-way Transformations

One way transformations are useful when data only flows in one direction. An example of this is a read-only HTTP API that based on the contents of one property in the output will make a request to an external service to include extra data in the original response.

Simple Transformations

Performs a single operation to transform the data in a type safe manner.

use JsonSchema\Validator;
use Shaper\Transformation\TransformationBase;
use Shaper\Util\Context;
use Shaper\Validator\JsonSchemaValidator;


class NumberToArray extends TransformationBase {
  public function getInputValidator() {
    return new JsonSchemaValidator(['type' => 'number'], new Validator());
  }
  public function getOutputValidator() {
    $schema = ['type' => 'array', 'items' => ['type' => 'number']];
    return new JsonSchemaValidator($schema, new Validator());
  }
  protected function doTransform($data, Context $context) {
    return [$context['keyName'] => $data];
  }
}

$t = new NumberToArray();
$t->transform(42, new Context(['keyName' => 'data'])); // ['data' => 42]
$t->transform(['foo']); // TypeError exception.

Queued Transformations

Performs a set of operations, one after the other in a type safe manner.

use JsonSchema\Validator;
use Shaper\Transformation\TransformationBase;
use Shaper\Util\Context;
use Shaper\Validator\InstanceofValidator;
use Shaper\Validator\JsonSchemaValidator;
use Shaper\Transformation\TransformationsQueue;

class ObjectToNumber extends TransformationBase {
  public function getInputValidator() {
    return new InstanceofValidator(\stdClass::class);
  }
  public function getOutputValidator() {
    $schema = ['type' => 'number'];
    return new JsonSchemaValidator($schema, new Validator());
  }
  protected function doTransform($data, Context $context) {
    return isset($data->value) ? $data->value : 0;
  }
}

$t = new TransformationsQueue();
$t->add(new ObjectToNumber());
$t->add(new NumberToArray());
$input = new \stdClass();
$input->value = 42;
$t->transform($input, new Context(['keyName' => 'data'])); // ['data' => 42]

Two-way Transformations (aka Data Adaptor)

Data adaptors, or reversible transformations, are useful when the data can flow in both directions. An example of this is an HTTP API that exposes an internal storage. However the internal storage format is not the format that the API wants to expose. For that data coming in needs to be transformed into the internal storage format, and data coming out needs to be transformed into the shape we want to expose to the outside world via our API.

use JsonSchema\Validator;
use Shaper\DataAdaptor\DataAdaptorBase;
use Shaper\Util\Context;
use Shaper\Validator\InstanceofValidator;
use Shaper\Validator\JsonSchemaValidator;

class MyDataAdaptor extends DataAdaptorBase {
  protected function doTransform($data, Context $context) {
    return $data->{$context['keyName']};
  }
  protected function doUndoTransform($data, Context $context) {
    return (object) [$context['keyName'] => $data, 'bar' => 'default'];
  }
  public function getInputValidator() {
    // In a real world scenario we would describe this as a JSON Schema.
    return new InstanceofValidator(\stdClass::class);
  }
  public function getInternalValidator() {
    // In a real world this would be your internal data object. Something like cheking that this is
    // an object of class FieldItemInstance.
    return new JsonSchemaValidator(['type' => 'string'], new Validator());
  }
  public function getOutputValidator() {
    // In a real world scenario we would describe this as a JSON Schema. In most cases the output
    // validator is the same as the input validator, so you can return the input validator here.
    return new InstanceofValidator(\stdClass::class);
  }
}

$da = new MyDataAdaptor();
$data = new \stdClass();
$data->lorem = 'caramba!';
$da->transform($data, new Context(['keyName' => 'lorem'])); // 'caramba!'
$da->transform(new NodeObject()); // TypeError exception.
$da->undoTransform('caramba!', new Context(['keyName' => 'lorem'])); // (object) ['lorem' => 'caramba!', 'bar' => 'default']
$da->undoTransform([]); // TypeError exception.

Class Diagram

Class Diagram

e0ipso/shaper 适用场景与选型建议

e0ipso/shaper 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 8.43M 次下载、GitHub Stars 达 15, 最近一次更新时间为 2018 年 02 月 27 日, 在 PHP 生态内属于活跃度较高的组件。

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

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

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

BUG 修复 & 性能优化

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

项目外包 & 长期维护

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

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

统计信息

  • 总下载量: 8.43M
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 16
  • 点击次数: 20
  • 依赖项目数: 3
  • 推荐数: 0

GitHub 信息

  • Stars: 15
  • Watchers: 2
  • Forks: 5
  • 开发语言: PHP

其他信息

  • 授权协议: GPL-2.0
  • 更新时间: 2018-02-27