Atomik offers a global store where anything can be saved for the time of a request. This global store acts like an associative array with key/value pairs. It's mainly use to store the configuration.
Accessors are methods provided by the Atomik class that allow you to
access the global store. They are five of them: get,
set, add,
has and delete.
The get method allows you to retreive the value
associated to the key passed as first argument. If a second argument is
specified it will be use as a default value in the case where the key is
not found.
Example 4.1. Using the get method
echo Atomik::get('key');
echo Atomik::get('keyThatDoesntExist', 'defaultValue');
The set method allows you to define a key and its
associated value. It will overwrite an existing value.
This accessor can also take an array as argument to set multiple key/value pairs at once. This array will be merged with the store.
Example 4.2. Using the set method
Setting one key
Atomik::set('key', 'value');
Setting multiple keys
Atomik::set(array( 'key1' => 'value1', 'key2' => 'value2' ));
The add method works like the set
method but rather than replacing values when they already exists, adds them. For
example if the key points to an array, the value will be added to this array as a new
item. If the key points to a value which is not an array, it will be transformed to
one.
Example 4.3. Using the add method
Atomik::set('key1', array('item1'));
Atomik::add('key1', 'item2');
$array = Atomik::get('key1'); // array('item1', 'item2')
The has and delete
methods only take a key as argument. The first one checks if the key exists
and the second delete the key and its value.
Paths can be used to access nested arrays. Each key in the path has to point to a nested array unless it's the last one. Keys are separated by a slash.
Example 4.5. Using paths
Atomik::set('users', array(
'paul' => array(
'id' => 1,
'age' => 20
),
'peter' => array(
'id' => 2,
'age' => 33
)
));
$paul = Atomik::get('users/paul'); // returns an array
$paulAge = Atomik::get('users/paul/age'); // returns 20
$peterId = Atomik::get('users/peter/id'); // returns 2
Atomik::set('users/sofia', array(
'id' => 3,
'age' => 25
));
$sofiaAge = Atomik::get('users/sofia/age');
You can also use paths in sub arrays when setting some values.
Example 4.6. Using paths when setting values
Atomik::set(array(
'users' => array(
'paul/age' => 22,
'paul/friends' => array(
'peter/age' => 20
)
)
));
echo Atomik::get('users/paul/age'); // 22
var_export(Atomik::get('users/paul/friends'));
array(
'peter' => array(
'age' => 20
)
)
Atomik::_dimensionizeArray() can be used to “dimensionize” any array.
Accessors can be used with any array. You need to pass as argument an array (the position of the argument depends on the method). See the API guide for more information. Still, here's an example:
Example 4.7. Using accessors with any array
$array = array();
Atomik::set('key', 'value', $array);
echo Atomik::get('key', null, $array);