raphhh/trex-collection

集合操作助手

1.1.0 2016-06-08 16:18 UTC

This package is auto-updated.

Last update: 2024-08-25 22:06:03 UTC


README

Latest Stable Version Build Status Scrutinizer Quality Score Code Coverage Total Downloads Reference Status License

安装

$ composer require raphhh/trex-collection

用法

集合

Collection 只是一个实现了额外方法的 ArrayObject

因为 Collection 只是一个外观,您可以使用 ArrayObject(或任何可转换为数组的对象)继续编码,并自行实现一些 TRex 特性

use TRex\Collection;

class MyCollection extends \ArrayObject
{
    use CollectionValueAccessorTrait;
    use CollectionKeyAccessorTrait;
    use CollectionFilterTrait;
    use CollectionComparatorTrait;
    use CollectionSorterTrait;
    ...
}

过滤器

提供过滤集合的方法。

使用回调过滤值:filter
use TRex\Collection\Collection;

$collection = new Collection([
    't-rex',
    'dog',
    'cat',
]);

$result = $collection->filter(function($value){
    return $value === 't-rex';
});

(array)$result; //['t-rex']
对值应用回调:each
use TRex\Collection\Collection;

$collection = new Collection([
    't-rex',
    'dog',
    'cat',
]);

$result = $collection->each(function($value){
    return strtoupper($value);
});

(array)$result; //['T-REX', 'DOG', 'CAT']
提取列表的一部分:extract
use TRex\Collection\Collection;

$collection = new Collection([
    't-rex',
    'dog',
    'cat',
]);

$result = $collection->extract(1);

(array)$result; //[1 => 'dog', 2 => 'cat']

比较器

合并任何集合:merge、mergeA
$coll1 = new Collection(['t-rex']);
$coll2 = new Collection(['dog']);
$coll3 = new Collection(['cat']);

$result = $coll1->merge($coll2, $coll3);

(array)$result; //['t-rex', 'dog', 'cat']
查找任何集合之间的差异:diff、diffA、diffK
$coll1 = new Collection(['t-rex', 'dog', 'cat']);
$coll2 = new Collection(['dog']);
$coll3 = new Collection(['cat']);

$result = $coll1->diff($coll2, $coll3);

(array)$result; //['t-rex']
查找任何集合之间的交集:intersect、intersectA、intersectK
$coll1 = new Collection(['t-rex', 'dog', 'cat']);
$coll2 = new Collection(['t-rex', 'dog']);
$coll3 = new Collection(['t-rex', 'cat']);

$result = $coll1->intersect($coll2, $coll3);

(array)$result; //['t-rex']

排序器

重新索引集合:reindex
$collection = new Collection(['a' => 't-rex', 'b' => 'dog', 'c' => 'cat']);
$result = $collection->reindex(); //[0 => ''t-rex', 1 => 'dog', 2 => 'cat']
排序集合:sort
$collection = new Collection(['t-rex', 'dog', 'cat']);
$result = $collection->sort($callback); //indexes nex collection
按特定值分组:groupBy
class Bar
{
    public $foo;

    public function __construct($foo)
    {
        $this->foo = $foo;
    }
}


$collection = new Collection([
    new Bar('t-rex'),
    new Bar('dog'),
    new Bar('cat'),
]);

//we will split the collection in 2: dinosaurs vs pets
$results = $collection->groupBy(function(Bar $bar){
    if($bar->foo === 't-rex'){
        return 'dinosaur';
    }
    return 'pet';
});

count($result['dinosaur']); // a Collection with 1 Bar with 't-rex'
count($result['pet']); // a Collection with 2 Bar with 'dog' and 'cat'

检索第一个值:first
$collection = new Collection(['t-rex', 'dog', 'cat']);
$collection->first(); //'t-rex'
检索最后一个值:last
$collection = new Collection(['t-rex', 'dog', 'cat']);
$collection->last(); //'cat'
检查是否有值:has
$collection = new Collection(['t-rex', 'dog', 'cat']);
$collection->has('t-rex'); //true

搜索键:search
$collection = new Collection(['t-rex', 'dog', 'cat']);
$collection->search('t-rex'); //[0]
提取键:keys
$collection = new Collection(['t-rex', 'dog', 'cat']);
$collection->keys(); //[0, 1, 2]

排序器

按属性或方法结果对集合进行排序。

您需要扩展类 TRex\Collection\AbstractSorter。在方法 invoke 中,您必须为集合内容中的每个对象返回排序值。

注意,TRex 排序器实现了鸭子类型。您应该通过键创建排序器以访问数据,而不是通过集合内容的类型。例如,如果您想按属性 '$foo' 对对象集合进行排序,您应该调用您的排序器 'FooSorter',不管集合内容中的对象。

use TRex\Collection\AbstractSorter;

class Bar
{
    public $foo;

    public function __construct($foo)
    {
        $this->foo = $foo;
    }
}

// we name the sorter with the name of the property $foo, and not the name of the class Bar
class FooSorter extends AbstractSorter
{
    /**
     * calls the property/method/key to extract the value to sort.
     *
     * @param mixed $object
     * @return mixed
     */
    protected function invoke($object)
    {
        //here, we want to sort the collection by the property $foo
        return $object->foo;
    }
}

$collection = [
    new Bar('a'),
    new Bar('c'),
    new Bar('b'),
];

uasort($collection, new FooSorter()); //will sort 'a', 'b', 'c'

过滤器

您需要扩展类 TRex\Collection\AbstractFilter。在方法 invoke 中,您必须为集合内容中的每个对象返回比较值。

注意,TRex 过滤器实现了鸭子类型。您应该通过键创建过滤器以访问数据,而不是通过集合内容的类型。例如,如果您想按属性 '$foo' 对对象集合进行排序,您应该调用您的排序器 'FooSorter',不管集合内容中的对象。

use TRex\Collection\AbstractFilter;

class Bar
{
    public $foo;

    public function __construct($foo)
    {
        $this->foo = $foo;
    }
}

// we name the filter with the name of the property $foo, and not the name of the class Bar
class FooFilter extends AbstractFilter
{
    /**
     * @param mixed $object
     * @return mixed
     */
    protected function invoke($object)
    {
        return $object->foo;
    }
}


$collection = [
    new Bar('a'),
    new Bar('b'),
    new Bar('b'),
];

array_filter($collection, new FooFilter('b')); //wil keep only the 'b' ones