subtext/collections
最新稳定版本:v1.0.1
Composer 安装命令:
composer require subtext/collections
包简介
Provides iterable collections of validated objects or scalar values, enabling fluent, type-safe interfaces in PHP.
README 文档
README
Abstract base class that provides a Java-style Collection interface in PHP.
This class brings structured, type-safe, and object-oriented collection handling
to PHP, inspired by Java's Collection framework. It extends ArrayObject
and implements JsonSerializable and Psr\Container\ContainerInterface to offer
modern PHP compatibility along with predictable, validated container behavior.
Subclasses must implement the protected validate($value): void method to define
what values the collection accepts. Rejected values should throw an instance of
InvalidArgumentException.
Key Features
- Enforces validation on all insertions via
::validate(mixed $value) - Supports both sequential (list-style) and associative (map-style) arrays
- Implements
ContainerInterfacefor ease of access and consistency - Implements
JsonSerializablefor structured serialization of contents - Offers utility methods similar to Java/JavaScript/TypeScript:
- getFirst - get the first element in a collection
- getLast - get the last element in a collection
- getNth - get the nth element in a collection, index begins at 1
- getKeys - get the keys for a map style collection
- slice - get a new collection consisting of a slice of the current one
- chunk - create multiple collections of a specific chunk size
- filter - pass a callable to filter the elements of a collection
- map - pass a callable to map the elements of a collection to a new collection
- walk - use a callable to apply transformation to every member of a collection
- reduce - reduce the elements of a collection to a single value
- empty - empty the collection of all values
- absorb - absorb the contents of a collection into another
- renameKey - rename the key for a specified value in the collection
Example
namespace Foo\Bars; use Foo\Bar; use InvalidArgumentException; use Subtext\Collections; class Collection extends Collections\Collection { protected function validate(mixed $value): void { if (!($value instanceof Bar)) { throw new InvalidArgumentException(sprintf( 'Value added to collection must be an instance of %s', Bar::class )); } } }
统计信息
- 总下载量: 1
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 0
- 点击次数: 1
- 依赖项目数: 1
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2025-09-06