PHPackages                             subtext/collections - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Utility &amp; Helpers](/categories/utility)
4. /
5. subtext/collections

ActivePackage[Utility &amp; Helpers](/categories/utility)

subtext/collections
===================

Provides iterable collections of validated objects or scalar values, enabling fluent, type-safe interfaces in PHP.

v1.0.1(12mo ago)011MITPHPPHP &gt;=8.1CI failing

Since Sep 20Pushed 11mo agoCompare

[ Source](https://github.com/subtext/collections)[ Packagist](https://packagist.org/packages/subtext/collections)[ RSS](/packages/subtext-collections/feed)WikiDiscussions master Synced 1mo ago

READMEChangelog (2)Dependencies (3)Versions (6)Used By (1)

Subtext\\Collections
====================

[](#subtextcollections)

[![Run Unit Tests](https://github.com/subtext/collections/actions/workflows/tests-unit.yml/badge.svg)](https://github.com/subtext/collections/actions/workflows/tests-unit.yml/badge.svg)

Abstract base class that provides a Java-style Collection interface in PHP.
---------------------------------------------------------------------------

[](#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

[](#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

[](#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
            ));
        }
    }
}
```

###  Health Score

34

—

LowBetter than 77% of packages

Maintenance50

Moderate activity, may be stable

Popularity1

Limited adoption so far

Community8

Small or concentrated contributor base

Maturity67

Established project with proven stability

 Bus Factor1

Top contributor holds 100% of commits — single point of failure

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

###  Release Activity

Cadence

Every ~4 days

Total

2

Last Release

360d ago

### Community

Maintainers

![](https://www.gravatar.com/avatar/5564be50e16a7f11dd5eecc28adf83cd3256638ea5bd0e103d1d0190c74b6b78?d=identicon)[subtext](/maintainers/subtext)

---

Top Contributors

[![subtext](https://avatars.githubusercontent.com/u/1268165?v=4)](https://github.com/subtext "subtext (13 commits)")

###  Code Quality

TestsPHPUnit

### Embed Badge

![Health badge](/badges/subtext-collections/health.svg)

```
[![Health](https://phpackages.com/badges/subtext-collections/health.svg)](https://phpackages.com/packages/subtext-collections)
```

###  Alternatives

[symfony/dependency-injection

Allows you to standardize and centralize the way objects are constructed in your application

4.2k431.1M7.5k](/packages/symfony-dependency-injection)[illuminate/contracts

The Illuminate Contracts package.

705122.9M10.1k](/packages/illuminate-contracts)[illuminate/container

The Illuminate Container package.

31178.1M2.0k](/packages/illuminate-container)[ecotone/ecotone

Supporting you in building DDD, CQRS, Event Sourcing applications with ease.

558549.8k17](/packages/ecotone-ecotone)[civicrm/civicrm-core

Open source constituent relationship management for non-profits, NGOs and advocacy organizations.

728272.9k20](/packages/civicrm-civicrm-core)[internal/dload

Downloads binaries.

98142.7k10](/packages/internal-dload)

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
