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

ActiveLibrary

star/collection
===============

Collection utility lib

1.3.0(11y ago)7563.5k—0%[2 PRs](https://github.com/yvoyer/collection/pulls)1MITPHP

Since Mar 12Pushed 8y ago1 watchersCompare

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

READMEChangelogDependencies (3)Versions (11)Used By (1)

collection
==========

[](#collection)

Master: [![Build Status](https://camo.githubusercontent.com/8a0b47116db9e33e046e1f254b2fc8d20e87ee6094706124cd8331735dce7c9e/68747470733a2f2f7472617669732d63692e6f72672f79766f7965722f636f6c6c656374696f6e2e7376673f6272616e63683d6d6173746572)](https://travis-ci.org/yvoyer/collection)

Develop: [![Build Status](https://camo.githubusercontent.com/a2f88f46dbac18ee763a64eb01fc98823ac91123ea208b190d70abd0999f6274/68747470733a2f2f7472617669732d63692e6f72672f79766f7965722f636f6c6c656374696f6e2e7376673f6272616e63683d646576656c6f70)](https://travis-ci.org/yvoyer/collection)

Library that offer multiple implementations of collection.

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

[](#installation)

To install the package using \[composer\] (), you just need to add the following lines to your `composer.json` file.

```
...
"require": {
    "star/collection": "~1.2"
}
...

```

Supported collection
--------------------

[](#supported-collection)

### Enumeration

[](#enumeration)

Wrap an immutable array of values in an object. Ensuring that any value passed to the enumeration is supported by the instance.

```
$enumeration = new Enumeration(array(1,2,3));
$enumeration->getSelected(); // returns null
$enumeration->select(2);
$enumeration->getSelected(); // returns 2
$enumeration->select('invalid'); // Throw Exception

```

### Typed Collection

[](#typed-collection)

Wraps a collection of a specific kind of object (class or interface). If a value not supported by the collection is given, the collection throws exceptions.

#### Basic usage

[](#basic-usage)

```
$collection = new TypedCollection('\stdClass');
$collection->add(2); // Throw exception
$collection->add(new \stdClass()); // works

$collection = new TypedCollection('\Countable');
$collection->add(2); // Throw exception
$collection->add(new ClassThatImplementsCountable()); // works

```

#### Advanced usage

[](#advanced-usage)

##### Using composition

[](#using-composition)

Lets say that you want a `Car` collection, you could just define it using the basic usage, but it would lead to code duplication. So a good practice would be to define a new class named `CarCollection`, and use composition instead of inheritance, and declare it like this:

```
class CarCollection
{
    private $collection;

    public function __construct()
    {
        $this->collection = new TypedCollection('tests\Star\Component\Collection\Example\Car');
    }
}

```

Declaring your collection like this will enable you to encapsulate logic relevant to the car collection at one place, instead of risking to expose the inner implementation to the outside world. That way, you can control what methods are available and avoid duplication.

Using this example, adding a `Car` to the collection would be easy by implementing the `addCar` method.

```
class CarCollection
{
    ...
    public function addCar(Car $car)
    {
        $this->collection->add($car);
    }
    ...
}

```

And, if you want to filter all the cars based on their color, you can internally use it like this:

```
class CarCollection
{
    ...
    public function findAllCarWithColor(Color $color)
    {
        $closure = function(Car $car) use ($color) {
            return $car->getColor() == $color;
        };

        return $this->collection->filter($closure)->toArray();
    }
    ...
}

```

The same could also be done for finding cars based on their name:

```
class CarCollection
{
    ...
    public function findAllWithName($name)
    {
        $expression = Criteria::expr()->eq('name', $name);
        $criteria = new Criteria($expression);

        return $this->collection->matching($criteria)->toArray();
    }
    ...
}

```

From now on, your collection is re-usable, and testable at one place, while avoiding the pitfalls of inheritance.

#### Using Inheritance

[](#using-inheritance)

```
class PassengerCollection extends TypedCollection
{
    ...
    public function findAllWithName($name)
    {
        $expression = Criteria::expr()->eq('name', $name);
        $criteria = new Criteria($expression);

        return $this->matching($criteria)->toArray();
    }
    ...
}

```

#### Using Doctrine

[](#using-doctrine)

This class can be used in conjunction with [doctrine/dbal](https://github.com/doctrine/dbal) as an added functionality, but you need to make sure that the following steps are respected.

- The custom collection should inherit from the `TypedCollection` which implement the `Doctrine\Common\Collections\Collection`.
- The collection attribute will be replaced by a `Doctrine\ORM\PersistentCollection` when hydrated, instead of the CustomCollection, event thought the collection is instanciated in the construct. To bypass this feature, any method that sould return a CustomCollection should be implemented like this:

    class Entity { // CustomCollection that can be hydrated with a PersistentCollection by Doctrine private $myElements;

    ```
      public function __construct()
      {
          $this->myElements = new CustomCollection('stdClass');
      }

      public function getElements()
      {
          // At this point $this->myElements could be a Persistent collection
          return new CustomCollection($this->myElements->toArray());
      }

    ```

    }

###  Health Score

38

—

LowBetter than 85% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity38

Limited adoption so far

Community9

Small or concentrated contributor base

Maturity69

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 ~69 days

Total

5

Last Release

4173d ago

### Community

Maintainers

![](https://www.gravatar.com/avatar/36116f724f1bd3ed808e54e2b2de6c17209c5c2c1954685fba18958ec8d055ee?d=identicon)[yvoyer](/maintainers/yvoyer)

---

Top Contributors

[![yvoyer](https://avatars.githubusercontent.com/u/1745744?v=4)](https://github.com/yvoyer "yvoyer (37 commits)")

###  Code Quality

TestsPHPUnit

### Embed Badge

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

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

###  Alternatives

[sylius/sylius

E-Commerce platform for PHP, based on Symfony framework.

8.4k5.6M651](/packages/sylius-sylius)[sulu/sulu

Core framework that implements the functionality of the Sulu content management system

1.3k1.3M152](/packages/sulu-sulu)[liuggio/fastest

Simple parallel testing execution... with some goodies for functional tests.

4825.6M26](/packages/liuggio-fastest)[sonata-project/entity-audit-bundle

Audit for Doctrine Entities

644989.8k1](/packages/sonata-project-entity-audit-bundle)[doctrine/phpcr-odm

PHP Doctrine Content Repository Object Document Mapper (ODM) provides transparent persistence for PHP objects.

1811.5M97](/packages/doctrine-phpcr-odm)[contao/core-bundle

Contao Open Source CMS

1231.6M2.4k](/packages/contao-core-bundle)

PHPackages © 2026

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