定制 arete/collection-pipeline 二次开发

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

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

arete/collection-pipeline

Composer 安装命令:

composer require arete/collection-pipeline

包简介

PHP (partial) implementation of a pipeline collection. Work with a collection of objects without making a bunch of callables, loops, & ifs.

README 文档

README

Build Status HHVM Status Author Latest Unstable Version License Codacy Badge

Work with a collection of objects without making a bunch of callables, loops, & ifs.

After reading Martin Fowler on the Collection Pipeline I wanted to use something similar in PHP, thus, this was born. League\Pipeline was used as was Illuminate\Support\Collection (all functions from this Collection are available in the chain.)

Example

This is our example group we will use

use Arete\CollectionPipeline\CollectionPipeline as CP;

class MockEntity {
    public $id;
    public $name;
    public function __construct($id, $name) {
        $this->id = $id;
        $this->name = $name;
    }
    public function getId() {
        return $this->id;
    }
    public function getName() {
        return $this->name;
    }
}

// ids are just random for testing
$array = array(
    new MockEntity(null, "eric"), #0
    new MockEntity(10, "tim"),    #1
    new MockEntity(111, "beau"),  #2
    new MockEntity(11, "ross"),   #3
    new MockEntity(12, "sarah"),  #4
    new MockEntity(13, "taylor"), #5
    new MockEntity(-42, "lea"),   #6
    new MockEntity("eh", "phil"), #7
    new MockEntity(6, "larry"),   #8
    new MockEntity(10, "frank"),  #9
    new MockEntity(["eh"], "joe"),#10

    new MockEntity(99, "kayla"),  #12
    new MockEntity(0, "martin"),  #11
    new MockEntity(1, "brad"),    #13
    new MockEntity(2, "luke"),    #14
    new MockEntity(3, "paul"),    #15
    new MockEntity(4, "ash"),     #16
    new MockEntity(5, "davey"),   #17
    new MockEntity(18,"anthony"), #18
    new MockEntity(19,"tim"),     #19
);

String functions

$result = CP::from($array)->wheres('getId', 'is_string')->all();

# gives: [7 => $array[7]]

! String functions

$result = CP::from($array)->wheres('getId', '!is_string')->all();

# gives: everything in $array except #7

Each

Use ::wheresEach to compare the whole value without using any accessors.

instanceof

$result = CP::from($array)->wheres('instanceof', MockEntity::CLASS)->all();

# gives: everything in $array

! instanceof

$result = CP::from($array)->wheres('!instanceof', MockEntity::CLASS)->all();

# gives: empty array, they all are instances of MockEntity

comparison operators

comparison operator tests

$result = CP::from($array)->wheres('getId', '>', 110)->all();

# gives: [9 => $array[9]]

chaining

$result = CP::from($array)->wheres('getId', '!is_string')->wheres('getId', '>', 10)->wheres('getName', '===', 'tim')->all();

# gives: [19 => $array[19]]

argument order:

The accessor return value (X) as the first argument, and the value you are using in the comparison (Y).

// one does contain joe, but none contain derek
$stringItWouldBeIn = 'joe,derek';
$x = 'getName';
$y = $stringItWouldBeIn;

// containsSubString is from arete\support
$result = CP::from($array)->wheresYX($x, 'containsSubString', $y)->all();

# gives: [10 => $array[10]]

Laravel Illuminate:

Since it extends Illuminate\Support\Collection, you can use their functions, such as:

$result = CP::from($array)->wheres('id', 'is_string', null, 'property')->keys();

# gives: [7]

Types:

// will only check for the method `getId`
$result = CP::from($array)->wheres('getId', '>', 110, 'method')->all();

# gives: [9 => $array[9]]

Reverse order

// compares 110 < $payload->getId()
$result = CP::from($array)->wheres('getId', '<', 110, 'method', 'yx')->all();

# gives: [9 => $array[9]]

callables

$stringItWouldBeIn = 'joe,jonathon';
$result = CP::from($array)->wheresYX('getName', 'containsSubString', $stringItWouldBeIn, 'callable')->all();
$result = CP::from($array)->wheres('getId', function($value) {
    if ($value == 'tim')
        return true
    return false;
})->all();

# gives: [10 => $array[10]]

Value:

Value is an optional parameter, so if you want to check say, a property only, but have no value to compare it to:

// will only check for the property `id`,
// it could be ['property', 'method'] if you wanted to use a method if the property was not there
// or it could be ['property', 'method', 'callable'] (which is default)
$result = CP::from($array)->wheres('id', 'is_string', null, 'property')->all();

# gives: [9 => $array[9]]

Specification

arete/specification

use Arete\Specification\Specification;
use Arete\Specification\SpecificationTrait;

class NameEquals implements Specification {
    use ParameterizedSpecification;
    use SpecificationTrait;

    public function isSatisfiedBy($entity) {
        if ($entity->getName() == $this->value)
            return true;
        return false;
    }
}

$result = CP::from($array)->satisfying(new NameEquals('tim'));

# gives: [10 => $array[10]]

Installation

It can be installed from Packagist using Composer.

In your project root just run:

$ composer require arete/collection-pipeline

Make sure that you’ve set up your project to autoload Composer-installed packages.

Running tests

Run via the command line by going to arete/collection-pipeline directory and running phpunit

@TODO:

  • add ability to get an array with objects method values. Meaning, if I want to just get $objects->getName(); as an array of $objectNames and also maybe set what the key is?
  • option to pass in an array with the '!' if you want it to be not?
  • move ExpressionBuilder to Constructor()
  • optimize the filters so they can be combined and done in one loop when requested as array / all()?
  • pass in multiple string functions & comparison operators, such as 'is_string | is_int & >' be able to do ('methodName', 'strlen >', 5) (could use some Symfony\ExpressionLanguage optionally if alias are required) when this is done, it will really use the pipeline how it ought to
  • similar to the last todo, but with chaining method calls
  • move examples out of readme (except for 1), and into [examples/]
  • add in spaceship comparison operator depending on version (thanks @seldaek)
  • ands using last method?
  • refactor ExendedPipeline so it is less of a God object.
  • array key in Specification
  • array key for matching along with the method, property, and callable
  • abstract argsOrderYX & XY
  • remove null check from ::wheresComparison
  • add ability to reverse arguments in expressions
  • add casting of accessor
  • add ::reduce() similar implementation as ::map()

arete/collection-pipeline 适用场景与选型建议

arete/collection-pipeline 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 25 次下载、GitHub Stars 达 7, 最近一次更新时间为 2015 年 10 月 01 日, 在 PHP 生态内属于活跃度较高的组件。

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

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

围绕 arete/collection-pipeline 我们能提供哪些服务?
定制开发 / 二次开发

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

BUG 修复 & 性能优化

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

项目外包 & 长期维护

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

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

统计信息

  • 总下载量: 25
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 7
  • 点击次数: 17
  • 依赖项目数: 1
  • 推荐数: 0

GitHub 信息

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

其他信息

  • 授权协议: MIT
  • 更新时间: 2015-10-01