rbdwllr/reallysimplecollection

一个简单的PHP集合库。

0.1.0 2018-05-10 10:48 UTC

This package is auto-updated.

Last update: 2024-09-11 20:15:46 UTC


README

Build Status codecov StyleCI

一个非常简单的PHP集合库,帮助您将数组转换为集合。提供了许多有用的方法,如first、map、filter等。

用法

要使用集合,只需将一个数组传递给集合类的new实例。

use ReallySimple\Collection;

$items = ['item1', 'item2', 'item3'];

$collection = new Collection($items);

echo $collection->count(); // Will Output 3.

您还可以静态地创建集合。

use ReallySimple\Collection;

$items = ['item1', 'item2', 'item3'];

$collection = Collection::make($items);

echo $collection->count(); // Will Output 3.

可用方法

以下方法是当前此集合库可用的。

// Return the number of items in the collection
$collection->count(): int;

// Return the current item the collection pointer is on
$collection->current();

// Filter the collection based on the values and return a new collection
$collection->filter(callable $callback): Collection;

// Filter the collection based on the keys and return a new collection
$collection->filterKeys(callable $callback): Collection;

// Filter the collection based on the keys and values and return a new collection
$collection->filterKeysValues(callable $callback): Collection;

// Return the first item from the collection
$collection->first();

// Return the current key the collection pointer is on
$collection->key();

// Map the collection and return a new collection
$collection->map(callable $callback): Collection;

// Move the collection point forward by one and return value
$collection->next();

// Return the collection pointer to the start of the collection
$collection->rewind();

// Reduce the collection and return a new collection
$collection->reduce(callable $callback, $initial = null): Collection;

// Return the collection as an array
$collection->toArray(): array;

// Check the collection key is valid
$collection->valid(): bool;

作者