The Titon\Common\Bag interface provides an easy object oriented approach for managing a set of parameters. An abstract class exists at Titon\Common\Bag\AbstractBag that can be inherited for immediate functionality. This class inherits most of its functionality from the Titon\Common\Mutable trait.
class ExampleBag extends Titon\Common\Bag\AbstractBag<string, array<string>> {
// ...
}
$bag = new ExampleBag();Mutable trait supports dot-notated keys.
AbstractBag, they key-value generics must be defined.
There are 2 methods for adding parameters to a bag, the first with set(), which accepts a key and value. Using the example above, our key must be a string and our value must be an array<string>.
$bag->set('foo', ['bar']);
$bag->set('baz', 'qux'); // InvalidThe second method is add(), which accepts a map of key-value pairs.
$bag->set(Map {
'foo' => ['bar'],
'baz' => ['qux']
});To retrieve a single parameter, use the get() method.
$bag->get('foo');
$bag->get('bar.baz'); // NestedOr to retrieve all parameters, use the all() method.
$bag->all(); // Map<Tk, Tv>The remove() method can be used for deleting a parameter defined by key.
$bag->remove('foo');And the flush() method will delete all parameters.
$bag->flush();Use the has() method to check whether a parameter exists by key.
$bag->has('foo');