jojo1981 / typed-collection
类型化集合
5.0.0
2023-02-06 09:27 UTC
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
作者:Joost Nijhuis <jnijhuis81@gmail.com>
类型化集合是一个有序可变序列。
它实际上是一个围绕索引数组包装的数据结构。
创建新集合时,必须指定集合的 type
。集合具有特定的 type
,并保证集合中所有元素都具有相同的 type
。
type 可以是一个 原始 type
或为集合设置的 类/接口 type
。
可用类型包括
- int(整数别名)
- float(别名real、double或number)
- string(别名text)
- array
- object
- callable(别名callback)
- iterable
- class(类或接口名称)
\Jojo1981\TypedCollection\Collection
类是可计数的,并且是可遍历的(可迭代的)。集合有以下便捷实例方法
- 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
\Jojo1981\TypedCollection\Collection
有一个静态方法 createFromCollections
。
可以将相同 type
的多个集合合并为一个集合。
安装
库
git clone https://github.com/jojo1981/typed-collection.git
Composer
composer require jojo1981/typed-collection
基本用法
<?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