Skip to content

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

License

Notifications You must be signed in to change notification settings

php-strict/container

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

28 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Container

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

About

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

Topics

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages