PHPackages                             yoshi2889/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. [Validation &amp; Sanitization](/categories/validation)
4. /
5. yoshi2889/collections

ActiveLibrary[Validation &amp; Sanitization](/categories/validation)

yoshi2889/collections
=====================

Simple Collection class allowing storage of specific data.

v0.2(4y ago)4144.5k—4.6%1[3 PRs](https://github.com/NanoSector/collections/pulls)2MITPHPPHP &gt;=7.2.0

Since Jun 28Pushed 2y ago2 watchersCompare

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

READMEChangelog (7)Dependencies (4)Versions (11)Used By (2)

Collections
===========

[](#collections)

[![Build Status](https://camo.githubusercontent.com/b90a67bdddddcbbb666420ef698b8d411486713ad3abdc177b63b2fbeff087cb/68747470733a2f2f7363727574696e697a65722d63692e636f6d2f672f596f736869323838392f636f6c6c656374696f6e732f6261646765732f6275696c642e706e67)](https://scrutinizer-ci.com/g/Yoshi2889/collections/build-status/master)[![Scrutinizer Code Quality](https://camo.githubusercontent.com/ed6ce07f844147f2ac89b48b88ad79d2219b5b000d50b4e8ec5523aa3e56c767/68747470733a2f2f7363727574696e697a65722d63692e636f6d2f672f596f736869323838392f636f6c6c656374696f6e732f6261646765732f7175616c6974792d73636f72652e706e67)](https://scrutinizer-ci.com/g/Yoshi2889/collections/?branch=master)[![Scrutinizer Code Coverage](https://camo.githubusercontent.com/ed6270dd67ff32ceaa6a7111f1074ae23c9ecc31b85c9330124ea5bb9fc6376a/68747470733a2f2f7363727574696e697a65722d63692e636f6d2f672f596f736869323838392f636f6c6c656374696f6e732f6261646765732f636f7665726167652e706e67)](https://scrutinizer-ci.com/g/Yoshi2889/collections/code-structure/master/code-coverage)[![Latest Stable Version](https://camo.githubusercontent.com/7a7edf210bab34196e3ba22ff562497c5f6c643f088f2f15db4d80bf53f7f194/68747470733a2f2f706f7365722e707567782e6f72672f796f736869323838392f636f6c6c656374696f6e732f762f737461626c65)](https://packagist.org/packages/yoshi2889/collections)[![Latest Unstable Version](https://camo.githubusercontent.com/95eb7fbe9dbf872fb155171fdb36c4ab36cb412a0736735c876fe40fefd3607d/68747470733a2f2f706f7365722e707567782e6f72672f796f736869323838392f636f6c6c656374696f6e732f762f756e737461626c65)](https://packagist.org/packages/yoshi2889/collections)[![Total Downloads](https://camo.githubusercontent.com/b002d91a387a7ed320e45accefe84e57966590ce5f624d4181ca07469444815a/68747470733a2f2f706f7365722e707567782e6f72672f796f736869323838392f636f6c6c656374696f6e732f646f776e6c6f616473)](https://packagist.org/packages/yoshi2889/collections)

Simple Collection class allowing storage of specific data, based on PHP's ArrayObject.

Installation
------------

[](#installation)

You can install this class via `composer`:

`composer require yoshi2889/collections`

Usage
-----

[](#usage)

To use a Collection, create a new instance:

```
$validationClosure = function ($value)
{
    return is_string($value);
};

$initialItems = ['This is a test value!', 'This is another test value!'];

$collection = new \Yoshi2889\Collections\Collection($validationClosure, $initialItems);
```

Note that the closure passed as the first parameter to the `Collection` constructur **MUST** return a boolean value. This function is used to validate any added data types. A returned value of true means the given value may be added to the collection.

The second parameter can contain any initial values which should be in the collection. These items will also be validated.

### Validating

[](#validating)

Data is validated using a `Closure` instance, or a callback. The given closure must support exactly 1 untyped(!) parameter and return a boolean value. Values for which the closure returns `false` will be denied with an `\InvalidArgumentException`.

Please note that only the **values** are validated, while keys are not.

Validation can be manually performed on values using the `->validateType($value)` and `->validateArray(array $array)` methods, which check a single value and an array of values respectively.

`->validateType($value)` will return boolean `true` if the value passes validation, and `false` if it does not.

`->validateArray(array $array)` will return boolean `true` if **all** the values pass validation, and `false` if one or more do not.

### Manipulation

[](#manipulation)

A `Collection` instance may be treated like a generic array. This means you can manipulate it like the following example:

```
$validationClosure = function ($value)
{
    return is_string($value);
};

$collection = new \Yoshi2889\Collections\Collection($validationClosure);

$collection['foo'] = 'bar';
```

Please note that any data entered this way will still be validated and may still throw an `\InvalidArgumentException`.

Furthermore, to remove data from the Collection you may use the `->offsetUnset($index)` and `->removeAll($value)` methods.

`->offsetUnset($index)` will remove the value located at key `$index`, which works similarly to using `unset($collection['key'])`.

`->removeAll($value)` will remove every value which is equal to `$value` from the collection. Because values may exist multiple times in the same Collection, it is not possible to reliably remove only a single value from the collection using a value; consider removing by key instead. If you wish to preserve the original values while creating a new instance without certain values, please refer to Filtering below.

### Filtering

[](#filtering)

A Collection can be filtered using the `->filter(\Closure $condition)` method. The given closure **MUST** return a boolean value.

The condition closure is called for every element in the collection. Any elements for which the closure returns `true` will be kept and put in a new `Collection` instance, which will be returned. For example, to filter out all elements which are NOT `foo`:

```
$validationClosure = function ($value)
{
    return is_string($value);
};

$initialItems = ['foo', 'bar', 'baz', 'foo', 'bar', 'baz'];

$collection = new \Yoshi2889\Collections\Collection($validationClosure, $initialItems);

$filterClosure = function ($value)
{
    return $value != 'foo';
};

// Will contain: ['bar', 'baz', 'bar', 'baz']
$newCollection = $collection->filter($filterClosure);
```

### Events

[](#events)

A Collection will emit a `changed` event if data gets added to or removed from it. No arguments are passed. This functionality is borrowed from [Événement](https://github.com/igorw/evenement) by *igorw*.

You can subscribe to the event using the `->on($event, callable $listener)` method, like so:

```
$collection->on('changed', function ()
{
    echo 'The collection changed!';
});
```

Validation Closures
-------------------

[](#validation-closures)

You can find a lot of closures and utilities which can be used by a Collection in the separate [validation-closures project](https://github.com/Yoshi2889/validation-closures).

License
-------

[](#license)

This code is released under the MIT License. Please see `LICENSE` to read it.

###  Health Score

34

—

LowBetter than 77% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity36

Limited adoption so far

Community16

Small or concentrated contributor base

Maturity54

Maturing project, gaining track record

 Bus Factor1

Top contributor holds 53.1% 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 ~264 days

Recently: every ~396 days

Total

7

Last Release

1658d ago

PHP version history (3 changes)v0.1.1PHP &gt;=7.1.0

v0.1.6PHP &gt;=7.0.0

v0.2PHP &gt;=7.2.0

### Community

Maintainers

![](https://www.gravatar.com/avatar/64cbefa2b33116fa8d909bbeb6add16b949cd91864b693cd9c4797430ee7eb6d?d=identicon)[Yoshi2889](/maintainers/Yoshi2889)

---

Top Contributors

[![dependabot[bot]](https://avatars.githubusercontent.com/in/29110?v=4)](https://github.com/dependabot[bot] "dependabot[bot] (52 commits)")[![dependabot-preview[bot]](https://avatars.githubusercontent.com/in/2141?v=4)](https://github.com/dependabot-preview[bot] "dependabot-preview[bot] (23 commits)")[![NanoSector](https://avatars.githubusercontent.com/u/1280380?v=4)](https://github.com/NanoSector "NanoSector (23 commits)")

---

Tags

arrayarrayobjectcollectionsdata-storagephpvalidation

###  Code Quality

TestsPHPUnit

Static AnalysisPHPStan

Code StylePHP\_CodeSniffer

Type Coverage Yes

### Embed Badge

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

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

###  Alternatives

[webmozart/assert

Assertions to validate method input/output with nice error messages.

7.6k894.0M1.2k](/packages/webmozart-assert)[bensampo/laravel-enum

Simple, extensible and powerful enumeration implementation for Laravel.

2.0k15.9M104](/packages/bensampo-laravel-enum)[swaggest/json-schema

High definition PHP structures with JSON-schema based validation

48612.5M73](/packages/swaggest-json-schema)[stevebauman/purify

An HTML Purifier / Sanitizer for Laravel

5325.6M19](/packages/stevebauman-purify)[ashallendesign/laravel-config-validator

A package for validating your Laravel app's config.

217905.3k5](/packages/ashallendesign-laravel-config-validator)[crazybooot/base64-validation

Laravel validators for base64 encoded files

1341.9M8](/packages/crazybooot-base64-validation)

PHPackages © 2026

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