Provides a Collection implementation for PHP.
You can install the package via composer:
composer require ghostwriter/collection
You can also star (🌟) this repo to find it easier later.
/** @var \Ghostwriter\Collection\Collection $collection */
$collection = Collection::new([1, 2, 3])
->append([4, 5, 6, 7, 8, 9])
->map(static fn ($v): int => $v * 10)
->filter(static fn ($v): bool => 0 === $v % 20);
$collection->toArray(); // [20, 40, 60, 80]
$collection->drop(1) // [40, 60, 80]
->take(2) // [40, 60]
->slice(1, 1) // [60]
->toArray(); // [60]
/**
* @template TValue
*
* @extends IteratorAggregate<TValue>
*/
interface CollectionInterface extends Countable, IteratorAggregate
{
/**
* @param iterable<TValue> $iterable
*
* @return self<TValue>
*/
public function append(iterable $iterable = []): self;
/**
* @template TContains
*
* @param Closure(TValue):bool|TContains $functionOrValue
*/
public function contains(mixed $functionOrValue): bool;
public function count(): int;
/**
* @param int<0,max> $length
*
* @throws LengthMustBePositiveIntegerException
* @throws OffsetMustBePositiveIntegerException
*
* @return self<TValue>
*
*/
public function drop(int $length): self;
/**
* @param Closure(TValue):void $function
*/
public function each(Closure $function): void;
/**
* @param Closure(TValue):bool $function
*
* @return self<TValue>
*/
public function filter(Closure $function): self;
/**
* @param ?Closure(TValue):bool $function
*
* @throws FirstValueNotFoundException If no value is found
*
* @return ?TValue
*
*/
public function first(Closure $function = null): mixed;
/**
* @return Generator<TValue>
*/
public function getIterator(): Generator;
/**
* @param ?Closure(TValue):bool $function
*
* @return null|TValue
*/
public function last(Closure $function = null): mixed;
/**
* @template TMap
*
* @param Closure(TValue):TMap $function
*
* @return self<TMap>
*/
public function map(Closure $function): self;
/**
* @template TAccumulator
*
* @param ?TAccumulator $accumulator
* @param Closure(null|TAccumulator,TValue):TAccumulator $function
*
* @return ?TAccumulator
*/
public function reduce(Closure $function, mixed $accumulator = null): mixed;
/**
* @param int<0,max> $offset
* @param int<0,max> $length
*
* @throws OffsetMustBePositiveIntegerException
* @throws LengthMustBePositiveIntegerException
*
* @return self<TValue>
*
*/
public function slice(int $offset, int $length = PHP_INT_MAX): self;
/**
* @param int<0,max> $length
*
* @throws OffsetMustBePositiveIntegerException
* @throws LengthMustBePositiveIntegerException
*
* @return self<TValue>
*
*/
public function take(int $length): self;
/**
* @return array<TValue>
*/
public function toArray(): array;
/**
* @param Closure():Generator $generator
*
* @return self<TValue>
*/
public static function from(Closure $generator): self;
/**
* @return self<TValue>
*/
public static function new(iterable $iterable = []): self;
}
composer test
Please see CHANGELOG.md for more information what has changed recently.
If you discover any security related issues, please email nathanael.esayeas@protonmail.com
instead of using the issue tracker.
Thank you for freely sharing your knowledge, insight and free time with me. I am grateful for your help and support.
The BSD-3-Clause. Please see License File for more information.