承接 subtext/collections 相关项目开发

从需求分析到上线部署,全程专人跟进,保证项目质量与交付效率

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

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

Run Unit Tests

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 ContainerInterface for ease of access and consistency
  • Implements JsonSerializable for 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

GitHub 信息

  • Stars: 0
  • Watchers: 0
  • Forks: 0
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2025-09-06

承接程序开发

PHP开发

VUE

Vue开发

前端开发

小程序开发

公众号开发

系统定制

数据库设计

云部署

网站建设

安全加固