PHPackages                             da2e/generic-collection - 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. da2e/generic-collection

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

da2e/generic-collection
=======================

PHP-based implementation for generic collection

v1.3.0(6y ago)410.5k↓44.4%MITPHPPHP &gt;=7.1CI failing

Since Nov 22Pushed 6y ago1 watchersCompare

[ Source](https://github.com/dmitrya2e/php-generic-collection)[ Packagist](https://packagist.org/packages/da2e/generic-collection)[ RSS](/packages/da2e-generic-collection/feed)WikiDiscussions master Synced 1w ago

READMEChangelogDependencies (1)Versions (5)Used By (0)

Generic collection
==================

[](#generic-collection)

This small library provides PHP-based implementation for generic collection.

PHP lacks generics (in opposite to Java, C++, etc). The problem is that you can't control and be sure which types do elements implement in an array/collection/hash/...

An example:

```
$a = [1, 2, new \stdClass(), function () { return 'foo'; }];
$r = 0;

foreach ($a as $v) {
    $r += $v; // here for stdClass and a closure you will get an error: Object of class ... could not be converted to int.
}
```

In Java for example you can eliminate that by using the generics:

```
List v = new ArrayList();
```

This library provides an OOP solution to have homogeneous types in a collection/array.

Built-in types
--------------

[](#built-in-types)

- DA2E\\GenericCollection\\Type\\ArrayType (corresponds to PHP built-in is\_array() function).
- DA2E\\GenericCollection\\Type\\BoolType (corresponds to PHP built-in is\_bool() function).
- DA2E\\GenericCollection\\Type\\CallableType (corresponds to PHP built-in is\_callable() function).
- DA2E\\GenericCollection\\Type\\FloatType (corresponds to PHP built-in is\_float() function).
- DA2E\\GenericCollection\\Type\\IntType (corresponds to PHP built-in is\_int() function).
- DA2E\\GenericCollection\\Type\\IterableType (corresponds to PHP built-in is\_iterable() function).
- DA2E\\GenericCollection\\Type\\MixedType (passes any value through).
- DA2E\\GenericCollection\\Type\\NullType (corresponds to PHP built-in is\_null() function).
- DA2E\\GenericCollection\\Type\\NumericType (corresponds to PHP built-in is\_numeric() function).
- DA2E\\GenericCollection\\Type\\ObjectType (corresponds to PHP built-in is\_object() function).
- DA2E\\GenericCollection\\Type\\ResourceType (corresponds to PHP built-in is\_resource() function).
- DA2E\\GenericCollection\\Type\\ScalarType (corresponds to PHP built-in is\_scalar() function).
- DA2E\\GenericCollection\\Type\\StringType (corresponds to PHP built-in is\_string() function).

Special types:

- DA2E\\GenericCollection\\Type\\CustomType: accepts a callback function with one argument to build a custom validator. E.g.:

```
use DA2E\GenericCollection\GCollection;
use DA2E\GenericCollection\Type\CustomType;

$collection = new GCollection(new CustomType(function ($value) {
    return $value === 'foobar';
}));

$collection[] = 'foobar'; // valid
$collection[] = 'bar'; // invalid
```

- DA2E\\GenericCollection\\Type\\GCollectionType: a type for embedded GCollections. E.g.:

```
use DA2E\GenericCollection\Type\GCollectionType;
use DA2E\GenericCollection\Type\StringType;
use DA2E\GenericCollection\GCollection;

$collection = new GCollection(new GCollectionType());
$collection[] = new GCollection(new StringType()); // valid
$collection[] = new StringType(); // invalid
```

### ObjectType

[](#objecttype)

ObjectType optionally accepts a fully-qualified class name to validate that value implements the given class. e.g.:

```
$value = new Some\Class\Here();
$type = new DA2E\GenericCollection\Type\ObjectType('Some\Class\Here');
$type->validate($value);
```

How to use
----------

[](#how-to-use)

1. Create a Generic Collection.
2. Pass a Type for that collection in a constructor.
3. Generic Collection implements \\ArrayAccess interface, so just set/push elements to array.
4. While adding elements to the collection it is immediately validated for the given type.
5. If the type is invalid, a DA2E\\GenericCollection\\Exception\\InvalidTypeException will be thrown.

An example:

```
use DA2E\GenericCollection\GCollection;
use DA2E\GenericCollection\Type\StringType;
use DA2E\GenericCollection\Exception\InvalidTypeException;

try {
    $collection = new GCollection(new StringType()); // You can pass an array as 2nd argument as well.
    $collection[] = 'string'; // this one is fine
    $collection[] = 1; // this one will throw an exception
} catch (InvalidTypeException $e) {
    // handle exception
}
```

6. If you pass the collection to a method/function as an argument, you could demand that all elements should implement an exact type:

```
function foobar(GCollection $collection) {
    try {
        $collection->elementsShouldBeTypeOf(StringType::class); // If something is wrong InvalidTypeException is thrown
    } catch (InvalidTypeException $e) {
        // handle exception
    }

    // ...
}
```

If the collection contains object you could additionally assure that elements are instances of a specific class.

```
function foobar(GCollection $collection) {
    try {
        $collection->elementsShouldBeTypeOf(ObjectType::class); // This is fine and valid but does not give a lot of information which exactly object is there.
        $collection->elementsShouldBeTypeOf(YourAnyClass::class); // This is more verbose and clear. We know that we expect a concrete YourAnyClass objects.
    } catch (InvalidTypeException $e) {
        // handle exception
    }

    // ...
}
```

Additional functions of GCollectionInterface
--------------------------------------------

[](#additional-functions-of-gcollectioninterface)

There many additional functions in GCollectionInterface (e.g. map, filter, slice, shuffle, etc.). Please refer to the interface to see all of the functions.

### map

[](#map)

```
use DA2E\GenericCollection\GCollection;
use DA2E\GenericCollection\Type\StringType;

$collection = new GCollection(new StringType(), ['a' ,'b']);
$collection->map(function ($item) {
    return $item . '2';
}); // Returns [ 'a2', 'b2', 'c2' ]
```

### filter

[](#filter)

```
use DA2E\GenericCollection\GCollection;
use DA2E\GenericCollection\Type\StringType;

$collection = new GCollection(new StringType(), ['a' ,'b']);
$collection->filter(function ($item) {
    return $item === 'b';
}); // Returns [ 'b' ]
```

### sort

[](#sort)

```
use DA2E\GenericCollection\GCollection;
use DA2E\GenericCollection\Type\StringType;

$collection = new GCollection(new StringType(), ['a' ,'b']);
$collection->sort(function (array $items) {
    rsort($items); // Here you can call any sort function you wish.

    return $items;
}); // Returns [ 'b', 'a' ]
```

###  Health Score

32

—

LowBetter than 70% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity28

Limited adoption so far

Community9

Small or concentrated contributor base

Maturity56

Maturing project, gaining track record

 Bus Factor1

Top contributor holds 83.3% 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 ~16 days

Total

4

Last Release

2342d ago

### Community

Maintainers

![](https://avatars.githubusercontent.com/u/959271?v=4)[Dmitry Abrosimov](/maintainers/dmitrya2e)[@dmitrya2e](https://github.com/dmitrya2e)

---

Top Contributors

[![dabrosimov](https://avatars.githubusercontent.com/u/35995035?v=4)](https://github.com/dabrosimov "dabrosimov (5 commits)")[![dmitrya2e](https://avatars.githubusercontent.com/u/959271?v=4)](https://github.com/dmitrya2e "dmitrya2e (1 commits)")

###  Code Quality

TestsPHPUnit

### Embed Badge

![Health badge](/badges/da2e-generic-collection/health.svg)

```
[![Health](https://phpackages.com/badges/da2e-generic-collection/health.svg)](https://phpackages.com/packages/da2e-generic-collection)
```

###  Alternatives

[marineusde/larapex-charts

Package to provide easy api to build apex charts on Laravel

1218.9k](/packages/marineusde-larapex-charts)

PHPackages © 2026

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