phpextra/collection

此软件包已被废弃,不再维护。未建议替代软件包。

PHP的集合类型

dev-master / 1.0.x-dev 2015-05-19 22:56 UTC

This package is not auto-updated.

Last update: 2022-02-01 12:38:27 UTC


README

#Collection (PHPExtra\Type\Collection)

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

##使用方法

通过实现以下接口,集合解决了以下问题: \Countable\ArrayAccess\Iterator\SortableInterface。这使得您可以使用 count() 计算集合,使用 foreach 在它上面进行迭代,像数组一样访问它(例如 $a[1]),以及对其内容进行排序($a->sort($sorter))。除了常规集合之外,还有 LazyCollection,它允许您指定一个闭包,仅在需要时初始化集合内容。

创建您的第一个集合

$collection = new Collection();

$collection->add('item1');
$collection->add('item2');
$collection->add('item3');

使用它

echo count($collection); // returns 3
echo $collection[0]; // returns "item1"
echo $collection->slice(1, 2); // returns Collection with a length of 2 containing item2 and item3.
echo $collection->filter(function($element, $offset){ return $offset % 2 == 0; }); // returns sub-collection with all elements with even offset number
$collection->sort(SorterInterface $sorter); // sorts collection

懒集合示例

$lazy = new LazyCollection(function(){
    return new Collection(array(1, 2, 3));
});

echo $lazy[2]; // initializes the closure and returns "3"

安装(Composer)

{
    "require": {
        "phpextra/collection":"~1.0"
    }
}

##变更日志

No releases yet

##贡献

所有代码贡献都必须通过 pull request 进行。Fork 项目,创建一个功能分支,然后发送给我一个 pull request。为了确保代码库的一致性,您应确保代码遵循 编码规范。如果您想帮忙,请查看 问题列表

##要求

See composer.json for a full list of dependencies.

##作者

Jacek Kobus - <kobus.jacek@gmail.com>

许可信息

See the file LICENSE.txt for copying permission.on.