colindecarlo/collection

Collection是一个专注于快速访问和迭代其成员的集合库。

0.1.0 2015-08-11 20:40 UTC

This package is not auto-updated.

Last update: 2024-09-14 17:53:35 UTC


README

Build Status

集合

Collection是一个面向快速直观接口的库,用于处理一组相关元素。

定义集合

可以通过传递以下方式以多种方式构造集合:

  • 一个整数给构造函数,表示其容量
  • 一个包含集合元素的数组或SplFixedArray

定义一个空集合

通过将一个整数传递给Collection构造函数来创建空集合,以指示其容量。集合的所有索引都被初始化为null,集合的大小报告为0

$imEmpty = new Collection(10);

count($imEmpty);
// 0

定义一个由arraySplFixedArray派生的集合

可以通过将数组(或SplFixedArray)传递给Collection构造函数来定义集合。集合的大小由找到数组的最后一个非空索引来决定。

$daysOfTheWeek = ['Sunday', 'Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday'];
$fromArray = new Collection($daysOfTheWeek);

count($fromArray);
// 7
$occupations = new \SplFixedArray(10);
$occupations[0] = 'Botanist';

$occupations = new Collection($occupations);

count($occupations);
// 1

使用集合

map($func)

使用map创建一个新实例的Collection,其中包含映射集合的每个元素的投影。投影通过将包含在$func中的函数应用于原始集合的每个元素来创建。

参数

`$func`
应用于集合中每个元素的函数。`$func`可以是任何[可调用](callable)函数。

示例

// using a function name
$words = new Collection(['Lorem', 'ipsum', 'dolor', 'sit' 'amet']);
$shouty = $words->map('strtoupper');
// ['LOREM', 'IPSUM', 'DOLOR', 'SIT' 'AMET'];

// using a callable array
$classesToMock = new Collection(['SomeClass', 'SomeOtherClass', 'YetAnotherClass']);
$mocks = $classesToMack->map(['Mockery', 'mock']);
//[object(Mockery\Mock), object(Mockery\Mock), object(Mockery\Mock)]

// using an anonymous function
$stringyDates = new Collection(['2007-06-08', '2009-05-11', '2014-02-19']);
$dates = $stringyDates->map(function ($date) {
  return DateTime::createFromFormat('Y-m-d', $date);
});
// [object(DateTime), object(DateTime), object(DateTime)]

each($func)

$func应用于集合中的每个元素。`each`返回原始集合,因此可以在此之上链式调用其他方法。

参数

`$func`
应用于集合中每个元素的函数。`$func`可以是任何[可调用](callable)函数。

示例

$queueEmail = function ($address) use ($message, $emailQueue) {
    $emailQueue->publish(['to' => $address, 'message' => $message]);
};

$adminEmails->each($queueEmail);

reduce($func, $intial)

通过将$func应用于集合中的每个元素来生成一个单一聚合值。

参数

`$func`
还原函数,该函数接受两个参数,`$carry`和`$elem`(按此顺序),其中`$carry`是当前还原值,`$elem`是正在还原的集合中的当前元素。`$func`可以是任何[可调用](callable)函数。
`$initial`
用于还原的初始值

示例

$workSchedule = new Collection([
     ['date' => '2015/04/20', 'start' => '08:00', 'end' => '12:00'],
     ['date' => '2015/04/21', 'start' => '12:00', 'end' => '17:00'],
     ['date' => '2015/04/23', 'start' => '08:00', 'end' => '17:00'],
     ['date' => '2015/04/24', 'start' => '10:00', 'end' => '15:00']
]);

$totalHours = $workSchedule->reduce(function($total, $schedule) {
    $start = DateTime::createFromFormat('Y/m/d H:i', $schedule['date'] . ' ' . $schedule['start']);
    $end = DateTime::createFromFormat('Y/m/d H:i', $schedule['date'] . ' ' . $schedule['end']);
    $hours = $end->diff($start)->h;
    return $total + $hours;
});
// 25

filter($func = null)

返回一个新集合,其中包含返回`true`的元素。如果未将`$func`传递给`filter`,则结果中仅包含原始集合中的真值。

参数

`$func`
传递给集合中每个元素的过滤器函数。`$func`返回`true`则保留元素,返回`false`则丢弃。

示例

$workSchedule = new Collection([
     ['date' => '2015/04/20', 'start' => '08:00', 'end' => '12:00'],
     ['date' => '2015/04/21', 'start' => '12:00', 'end' => '17:00'],
     ['date' => '2015/04/23', 'start' => '08:00', 'end' => '17:00'],
     ['date' => '2015/04/24', 'start' => '10:00', 'end' => '15:00']
]);

$packALunch = $workSchedule->filter(function($schedule) {
    $start = DateTime::createFromFormat('Y/m/d H:i', $schedule['date'] . ' ' . $schedule['start']);
    $end = DateTime::createFromFormat('Y/m/d H:i', $schedule['date'] . ' ' . $schedule['end']);
    $hours = $end->diff($start)->h;
    return $hours > 4;
});
// object(Collection)(
     ['date' => '2015/04/21', 'start' => '12:00', 'end' => '17:00'],
     ['date' => '2015/04/23', 'start' => '08:00', 'end' => '17:00'],
     ['date' => '2015/04/24', 'start' => '10:00', 'end' => '15:00']
)

slice($offset, $length = null)

flatten($flattenWith = null)

将多维集合转换为一维集合。`$flattenWith`的职责是接受集合中的一个元素,并返回包含该元素中元素的数组(或实现CountableArrayAccess)的对象。如果没有提供`flattenWith`函数给`flatten`,则方法将尝试使用通用的`flattenWith`函数来扁平化集合,这对于扁平化二维集合很有用。

参数

`$func`
此函数用于将单个元素扁平化为单维数组。

示例

contains($value)

first($matching = null)

last($matching = null)

reverse()

groupBy($getGroupKey)

prepend($elem)

append($elem)

push($elem)

pop()

toArray()

count()

作者

Colin DeCarlo, colin@thedecarlos.ca

许可证

收集数据遵循MIT许可证 - 详细信息请查看LICENSE文件