nolikein / container
Associative and indexed containers, or create your own container type. A PSR-11 implementation.
Requires
- php: ^8.4
- psr/container: ^2.0
Requires (Dev)
- friendsofphp/php-cs-fixer: ^3.95
- pestphp/pest: ^4
- phpstan/phpstan: ^2.3
Suggests
None
Provides
None
Conflicts
None
Replaces
None
This package is auto-updated.
Last update: 2026-11-14 10:09:16 UTC
README
Description
Allow to use an associative container and an indexed container. This library is based on PSR-11.
Install
Requires PHP 8.4 or greater and psr/container ^2.0.
Composer from bash
composer require nolikein/container ^2.0
Composer from Docker:
docker run --rm --interactive --tty -v $PWD:/app composer require nolikein/container ^2.0
Usage
Create instances
$iC = new IndexedContainer();
$aC = new AssociativeContainer();
Available methods
For all containers
# Get - Get a data by id
$container->get('id');
$container->get(1);
# Has - Check if has a data by id
$container->has('id');
$container->has(1);
# All - Get all element in the container
$container->all();
# Note: has() is true for an id holding null, but get() throws a ContainNullException
# (PSR-11 only guarantees that has() false means get() throws a NotFoundExceptionInterface).
# Delete - Delete a data by id
$container->delete('id');
$container->delete(1);
# DeleteAll - Delete all element in the container
$container->deleteAll();
For indexed container
# Unshift - Set a data onto the start of the container
$iC->unshift('value');
# Push - Set a data onto the end of the container
$iC->push('value');
# Shift - Drop and get a data from the start of the container
$iC->shift();
# Pop - Drop and get a data from the end of the container
$iC->pop();
For associative container
# Set - Set a mixed value from an int or a string name
# Any other kind of name (float, bool, null, array, object) throws an InvalidArgumentException
$aC->set('name', 'value');
Exceptions
Every exception of the library implements Nolikein\Container\Exception\ContainerLibraryExceptionInterface: catch it to handle any of them. They also implement the PSR-11 interfaces (NotContainException is a NotFoundExceptionInterface, ContainNullException a ContainerExceptionInterface).
use Nolikein\Container\Exception\ContainerLibraryExceptionInterface;
try {
$container->get('name');
} catch (ContainerLibraryExceptionInterface $exception) {
// NotContainException or ContainNullException
}
Add a new container type
Extend the BaseContainer;
use Nolikein\Container\BaseContainer;
class MyContainer extends BaseContainer {}
Development
Every command runs in the Docker php service (PHP 8.4 by default):
docker compose build
docker compose run --rm php composer install
docker compose run --rm php composer qa # style, static analysis, tests
docker compose run --rm php composer test:coverage # tests with coverage
PHP_VERSION=8.5 docker compose run --rm php composer qa # run on PHP 8.5
Links
- Security policy: how to report a vulnerability.
Licence
The project is under licence MIT.