Container

A container holds a list of elements and does not take care of keys.

Create a container

use Dustin\Encapsulation\Container;

$container = new Container();

A container can optionally be initialized with a list of elements:

$container = new Container([$foo, $bar]);

Add elements

$container = new Container();

$container->add($foo, $bar);

You can pass as many elements as you want to the add-method.

Access Elements

You can get a single element of a container at a specific position:

$productNumbers = new Container(['My1003', 'My1004', 'My1005']);

$number = $productNumbers->getAt(1); // Returns 'My1004'

You also can get all elements of a container as array:

$container = new Container([$foo, $bar]);

$array = $container->toArray();

Count elements of a container

$container = new Container([$foo, $bar]);

$container->count();
// or
count($container); 
// Returns 2

You can also check if a container has elements:

$container->isEmpty();

Iterate over all elements

You can also use a container in a loop and iterate over all elements:

$container = new Container([$foo, $bar]);

foreach($container as $element) {
    // do something
}

Delete elements

You can clear a container which unsets all of it's elements:

$container->clear();

Copy a container

$container = new Container([$foo, $bar]);

$newContainer = $container->copy();

Copying a container does not clone object elements.

Merging serveral containers together

$container = Container::merge(
    new Container([$foo, $bar]), 
    new Container([$alice, $bob]),
    new Container([$hello, $world])
);

$container will now be a new container object holding the values of $foo, $bar, $alice, $bob, $hello and $world.

You can pass as many container objects to the merge-function as you want.

Sorting elements

You can sort the elements of a container ascending, descending or by a callback. Internally the array functions usort, sort and rsort will be used so you optionally can pass their flags.

Sort elements by callback

$container->sort(function($a, $b) {
    // return sort result
});

Sort elements ascending

$container->sort();
// or
$container->sort(null, Container::ASCENDING, $optionalFlags);

Sort elements descending

$container->sort(null, Container::DESCENDING, $optionalFlags);

Array functions

The container class brings a bunch of wrapper methods according to some of PHP's array functions. Each function takes the same parameters as the default PHP function except the input array.

Most methods return the container itself or a new instance which makes it easy to concat method calls to get the wanted container object:

$newContainer = $container->map($myCallback)->filter()->unique();

$newContainer = $container->map(function($element) {
    // do and return something
});

map will create and return a new container instance.

$value = $container->reduce(
    function($carry, $element) {
        // do and return something
    }, 
    $optionalInitValue
);

$newContainer = $container->filter($optionalCallbackFunction);

filter will create and return a new container instance.

$newContainer = $container->slice($offset, $optionalLength);

slice will create and return a new container instance.

$container->splice($offset, $optionalLength, $replacement);

$newContainer = $container->unique($optionalFlags);

unique will create and return a new container instance.

$value = $container->shift();

$container->unshift(...$elements);

$value = $container->pop();

$newContainer = $container->replace(... $replacements);

replace will create and return a new container instance.

$container->walk(
    function($element) { 
        // do something
    },
    $optionalArg
);

$newContainer = $container->reverse();

reverse will create and return a new container instance.

$index = $container->search($needle, $strict);

$result = $container->has($element);

$arrayOfNewContainers = $container->chunk($size);

Last updated