jjware / collections
Library for working with collections in PHP
Installs: 2 674
Dependents: 0
Suggesters: 0
Security: 0
Stars: 0
Watchers: 1
Forks: 0
Open Issues: 0
Requires
- php: >=5.5.0
Requires (Dev)
- phpunit/phpunit: ^5.0
This package is not auto-updated.
Last update: 2023-06-28 15:46:39 UTC
README
A library of collection types in PHP
Getting Started
composer require jjware/collections
Usage
Collections can be instantiated from existing arrays or without any arguments. A simple example of using a collection would be:
use Collections\Collection; $collection = new Collection([1, 2, 3, 4, 5]); $firstOverThree = $collection->where(function ($item) { return $item > 3; })->firstOrDefault(null); // $firstOverThree === 4
Collections implement the IteratorAggregate
, ArrayAccess
, Countable
, Serializable
, and JsonSerializable
interfaces.
There are also specialized collection classes for functionality such as queuing:
use Collections\Queue; $queue = new Queue([1, 2, 3, 4, 5]); $next = $queue->dequeue(); // $next === 1
Or for stacking:
use Collections\Stack; $stack = new Stack([1, 2, 3, 4, 5]); $next = $stack->pop(); // $next === 5
The Collections\EnumerableExtensions
trait can be used on any Traversable
to give it a lot of collection-like functionality.
EnumerableExtensions::all(callable $predicate): Enumerable EnumerableExtensions::any(callable $predicate): Enumerable EnumerableExtensions::contains(mixed $object): bool EnumerableExtensions::count(): int EnumerableExtensions::countWhere(callable $predicate): int EnumerableExtensions::distinct(): Enumerable EnumerableExtensions::distinctWhere(EqualityComparerInterface $comparer): Enumerable EnumerableExtensions::each(callable $callback): void EnumerableExtensions::elementAt(int $index): mixed EnumerableExtensions::elementAtOrDefault(int $index, mixed $default): mixed EnumerableExtensions::except(array $ignore): Enumerable EnumerableExtensions::exceptWhere(array $ignore, EqualityComparerInterface $comparer): Enumerable EnumerableExtensions::first(): mixed EnumerableExtensions::firstOrDefault(mixed $default): mixed EnumerableExtensions::last(): mixed EnumerableExtensions::lastOrDefault(mixed $default): mixed EnumerableExtensions::ofType(string $type): mixed EnumerableExtensions::skip(int $number): Enumerable EnumerableExtensions::toArray(): array EnumerableExtensions::where(callable $predicate): Enumerable
Full documentation to come in the future...