ghostwriter / collection
Provides a Collection implementation for PHP
Fund package maintenance!
ghostwriter
Installs: 1 347 150
Dependents: 6
Suggesters: 0
Security: 0
Stars: 2
Watchers: 1
Forks: 0
Open Issues: 0
Requires
- php: >=8.3
Requires (Dev)
- ghostwriter/coding-standard: dev-main
- ghostwriter/psalm-plugin: >=0.2
README
Provides a Collection implementation for PHP.
Installation
You can install the package via composer:
composer require ghostwriter/collection
Star ⭐️ this repo if you find it useful
You can also star (🌟) this repo to find it easier later.
Usage
/** @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]
API
/** * @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; }
Testing
composer test
Changelog
Please see CHANGELOG.md for more information what has changed recently.
Security
If you discover any security related issues, please email nathanael.esayeas@protonmail.com
instead of using the issue tracker.
Sponsors
Thank you
Thank you for freely sharing your knowledge, insight and free time with me. I am grateful for your help and support.
Credits
License
The BSD-3-Clause. Please see License File for more information.