PHPackages                             mistralys/application-utils-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. mistralys/application-utils-collections

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

mistralys/application-utils-collections
=======================================

Interfaces, traits and classes for handling static item collections, similar to Enums and with useful getter methods.

1.2.1(9mo ago)037.0k—8.5%5MITPHPPHP &gt;=7.4

Since Oct 16Pushed 9mo ago2 watchersCompare

[ Source](https://github.com/Mistralys/application-utils-collections)[ Packagist](https://packagist.org/packages/mistralys/application-utils-collections)[ RSS](/packages/mistralys-application-utils-collections/feed)WikiDiscussions main Synced 1mo ago

READMEChangelog (10)Dependencies (3)Versions (15)Used By (5)

AppUtils - Collections
======================

[](#apputils---collections)

Interfaces, traits and classes for handling item collections, similar to Enums but with useful getter methods.

> This is part of the [Application Utils](https://github.com/Mistralys/application-utils) ecology.

Feature Overview
----------------

[](#feature-overview)

The collection classes come in several flavors:

- Separate collections for records with string and integer IDs.
- Class loader collections that instantiate classes from one or more folders.
- Basket-like collections that allow freely adding and removing items.

Basic Example
-------------

[](#basic-example)

Let's say you want to create a collection of herbs. A string-based collection is ideal in this case, each herb being an object that can easily be extended with additional properties and methods.

```
$basil = Herbs::getInstance()->getByID(Herbs::BASIL);

echo $basil->getID(); // basil
echo $basil->getName(); // Basil
```

```
foreach(Herbs::getInstance()->getAll() as $herb) {
    echo $herb->getName();
}
```

The Principle
-------------

[](#the-principle)

The basic principle is to have a collection class for a data type, which knows all the possible values for that type, and a record class that represents a single value of that type.

The collections and records are distinguished by the return type of their `getID()` method (`int` or `string`).

Implementation
--------------

[](#implementation)

There are two ways to implement collections:

1. By extending the abstract base classes (e.g. `BaseStringPrimaryCollection`)
2. By implementing the interface and trait (e.g. `StringPrimaryCollectionTrait`)

> The second way is useful when working with classes that already extend another class.

The records have no abstract base class, only an interface that contains the `getID()` method with the relevant return type, e.g. `StringPrimaryRecordInterface`.

Usage
-----

[](#usage)

There are example implementations of the string and integer collections in the unit test classes:

### Items with an integer-based ID

[](#items-with-an-integer-based-id)

- [Integer collection](tests/AppUtilsTestClasses/IntegerPrimaryCollectionImpl.php)
- [Integer record](tests/AppUtilsTestClasses/IntegerPrimaryRecordImpl.php)
- [Non-default-aware collection](tests/AppUtilsTestClasses/IntegerPrimaryCollectionNoDefaultImpl.php)

### Items with a string-based ID

[](#items-with-a-string-based-id)

- [String collection](tests/AppUtilsTestClasses/IntegerPrimaryCollectionImpl.php)
- [String record](tests/AppUtilsTestClasses/IntegerPrimaryRecordImpl.php)
- [Non-default-aware collection](tests/AppUtilsTestClasses/StringPrimaryCollectionNoDefaultImpl.php)

### Basket collections

[](#basket-collections)

These collections allow adding and removing items freely, similar to a basket. They do not contain a fixed set of items, but rather allow dynamically storing and retrieving items.

- [String Basket](/src/Collections/StringPrimaryBasket.php)
- [Integer Basket](/src/Collections/IntegerPrimaryBasket.php)

These classes can be used without extending them, but can be extended to add custom functionality.

```
use AppUtils\Baskets\GenericIntegerPrimaryBasket;

$products = GenericIntegerPrimaryBasket::create();

$products->addItem(new Product(289, 'Product A'));
$products->addItem(new Product(290, 'Product B'));

$products->removeItem(290);
```

### Dynamic class loading

[](#dynamic-class-loading)

This specialized collection loads all classes from a folder that match a given interface or class name, instantiates them, and gives access to the resulting objects.

Fueled by AppUtils' ClassHelper class loading mechanism, it is a powerful, fire-and-forget tool that helps with building dynamic applications.

- [Class folder loading](tests/AppUtilsTestClasses/ClassLoaderCollectionImpl.php)
- [Class folder loading (filtered)](tests/AppUtilsTestClasses/ClassLoaderCollectionInstanceOfImpl.php)
- [Class folder loading (multiple folders)](tests/AppUtilsTestClasses/ClassLoaderCollectionMultiImpl.php)

### Events

[](#events)

Items in a collection are initialized on demand. To be able to react to this, the collections offer the `onItemsInitialized()` method, which can be used to register a listener that is called when the items are initialized.

```
use AppUtils\Collections\Events\ItemsInitializedEvent;use AppUtilsTestClasses\StringPrimaryCollectionImpl;

$collection = new StringPrimaryCollectionImpl();

// Register listeners before accessing the items
$collection->onItemsInitialized(function(ItemsInitializedEvent $event) : void {
    // Do something
});

$collection->getAll(); // This, for example, will trigger the event
```

###  Health Score

40

—

FairBetter than 88% of packages

Maintenance56

Moderate activity, may be stable

Popularity29

Limited adoption so far

Community14

Small or concentrated contributor base

Maturity50

Maturing project, gaining track record

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

Recently: every ~7 days

Total

14

Last Release

295d ago

### Community

Maintainers

![](https://avatars.githubusercontent.com/u/8895528?v=4)[Mistralys](/maintainers/Mistralys)[@Mistralys](https://github.com/Mistralys)

---

Top Contributors

[![Mistralys](https://avatars.githubusercontent.com/u/8895528?v=4)](https://github.com/Mistralys "Mistralys (105 commits)")

---

Tags

collectionsenumslibraryphpphp-library

###  Code Quality

TestsPHPUnit

Static AnalysisPHPStan

Type Coverage Yes

### Embed Badge

![Health badge](/badges/mistralys-application-utils-collections/health.svg)

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

###  Alternatives

[aginev/datagrid

Datagrid Package for Laravel v5+

4828.7k](/packages/aginev-datagrid)[cslant/laravel-like

A package providing 👍 like, 👎 dislike and love ❤️ or unlike ✋features features for Laravel applications.

2422.2k1](/packages/cslant-laravel-like)[xeeeveee/sudoku

PHP sudoku logic library - Generate and solve sudoku puzzles

2814.1k](/packages/xeeeveee-sudoku)[sun/country

Sun Country is the package that helps you to get the country name &amp; dialing code by the country ISO 3166-1 Alpha-2 code.

1016.5k](/packages/sun-country)

PHPackages © 2026

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