php-strict/container

Interface and implementation of common container.

v1.0.0 2019-05-06 07:33 UTC

This package is auto-updated.

Last update: 2024-04-16 16:51:56 UTC


README

Software License Build Status codecov Codacy Badge

Interface and implementation of common container.

Strict version of PSR-11 with additional methods to set/get references (instead of values) and get all container entries as array.

Requirements

  • PHP >= 7.1

Install

Install with Composer:

composer require php-strict/container

Usage

Classic usage:

use PhpStrict\Container\Container

$container = new Container();
$container->set('key1', 1);

if ($container->has('key1')) {
    $var1 = $container->get('key1');
}

Usage to set/get references:

use PhpStrict\Container\Container

$myObject = new stdClass();

$container = new Container();
$container->setByRef('key2', $myObject);

$anotherObject =& $container->getRef('key2');

Set container values through constructor:

use PhpStrict\Container\Container

$container = new Container([
    'key1' => 1,
    'key2' => 'value 2',
    'key3' => true,
]);

if ($container->has('key2')) {
    $var2 = $container->get('key2');
}

Unpacking container through callback:

use PhpStrict\Container\Container

class MyClass
{
    protected $field1;
    protected $field2;
    protected $field3;
    
    public function unpacker(array $entries): void
    {
        foreach ($entries as $key => $value) {
            $this->$key = $value;
        }
    }
}

$container = new Container([
    'field1' => 1,
    'field2' => 'value 2',
    'field3' => true,
]);

$myClassObject = new MyClass();

$container->unpackWith([$myClassObject, 'unpacker']);

Tests

To execute the test suite, you'll need Codeception.

vendor\bin\codecept run