Skip to content
This repository has been archived by the owner on Sep 3, 2021. It is now read-only.

Latest commit

 

History

History
74 lines (51 loc) · 1.69 KB

bags.md

File metadata and controls

74 lines (51 loc) · 1.69 KB

Bags

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();
The Mutable trait supports dot-notated keys.
When inheriting from AbstractBag, they key-value generics must be defined.

Adding Parameters

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'); // Invalid

The second method is add(), which accepts a map of key-value pairs.

$bag->set(Map {
    'foo' => ['bar'],
    'baz' => ['qux']
});

Retrieving Parameters

To retrieve a single parameter, use the get() method.

$bag->get('foo');
$bag->get('bar.baz'); // Nested

Or to retrieve all parameters, use the all() method.

$bag->all(); // Map<Tk, Tv>

Removing Parameters

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();

Checking Parameters

Use the has() method to check whether a parameter exists by key.

$bag->has('foo');