jojo1981 / typed-collection
Typed collection
Installs: 10 766
Dependents: 3
Suggesters: 0
Security: 0
Stars: 2
Watchers: 2
Forks: 0
Open Issues: 0
Requires
- php: ^8.0
- jojo1981/php-types: ^4.0
Requires (Dev)
- dg/bypass-finals: ^1.3
- php-coveralls/php-coveralls: ^2.2
- phpspec/prophecy-phpunit: ^2.0
- phpunit/phpunit: ^9.5
Suggests
- jms/serializer: Library for (de-)serializing data of any complexity; supports XML, JSON, and YAML.
- jojo1981/jms-serializer-handlers: Add support to work with the jms/serializer package
README
Author: Joost Nijhuis <jnijhuis81@gmail.com>
The typed collection is an ordered mutable sequence.
It is a data structure which is in fact a wrapper around an indexed array.
When a new collection is created the type
for the collection MUST be given.
The collection is of a certain type
and will guarantee all elements in the collection are of the same type
.
The type can be a primitive type
or a class/interface type
set for the collection.
Available types are:
- int (alias integer),
- float (aliases real, double or number)
- string (alias text)
- array
- object
- callable (alias callback)
- iterable
- class (class or interface name)
The \Jojo1981\TypedCollection\Collection
class is countable and is traversable (iterable).
The collection has the following convenient instance methods:
- getType(): string
- isEqualType(Collection $otherCollection): bool
- isEmpty(): bool
- isNonEmpty(): bool
- unshiftElement($element): void
- pushElement($element): void
- shiftElement()
- popElement()
- hasElement($element): bool
- pushElements(array $elements): void
- unshiftElements(array $elements): void
- setElements(array $elements): void
- removeElement($element): void
- indexOfElement($element): ?int
- getElementByIndex(int $index)
- getFirstElement()
- getLastElement()
- getFirstElementAsCollection(): Collection
- toArray(): array
- sortBy(callable $comparator): Collection
- map(string $type, callable $mapper): Collection
- flatMap(string $type, callable $mapper): Collection
- merge(Collection $otherCollection, Collection ...$otherCollections): void
- all(callable $predicate): bool
- some(callable $predicate): bool
- none(callable $predicate): bool
- forEach(callable $callback): void
- foldLeft(callable $callback, $initial = null)
- foldRight(callable $callback, $initial = null)
- filter(callable $predicate): Collection
- find(callable $predicate)
- group(callable $predicate, callable ...$predicates): Collection
- partition(callable $predicate): Collection
- slice(int $offset, ?int $length = null): Collection
- clear(): void
- count(): int
- getIterator(): CollectionIterator
- isEqualCollection(Collection $otherCollection, ?callable $predicate = null, bool $strict = false): bool
The \Jojo1981\TypedCollection\Collection
has a static method createFromCollections
.
Multiple collection of the same type
can be merged together into one collection.
Installation
Library
git clone https://github.com/jojo1981/typed-collection.git
Composer
composer require jojo1981/typed-collection
Basic usage
<?php require 'vendor/autoload.php'; // Create an empty collection of type `string` $collection1 = new \Jojo1981\TypedCollection\Collection('string'); // Create a collection of type `integer` with some elements $collection2 = new \Jojo1981\TypedCollection\Collection('int', [1, 2, 3]); // Will throw an exception, because an invalid type has been given try { $collection = new \Jojo1981\TypedCollection\Collection('invalid'); } catch (\Jojo1981\TypedCollection\Exception\CollectionException $exception) { echo $exception->getMessage() . PHP_EOL; } // Create a collection of class type \stdClass $collection3 = new \Jojo1981\TypedCollection\Collection(\stdClass::class); $collection3->pushElement(new \stdClass()); try { $collection3->pushElement('element'); } catch (\Jojo1981\TypedCollection\Exception\CollectionException $exception) { echo $exception->getMessage() . PHP_EOL; } echo 'Collection count: ' . $collection3->count() . PHP_EOL; // Will be 1