Wallogit.com
2017 © Pedro Peláez
Package to easily manage a json setting file
This package aims to make a json setting file usable and easy maintainable., (*1)
The json file is build like this:, (*2)
{
"foo":{
"bar":"foo",
"bars":"foos",
}
}
Where foo is a group and bar a key value pair., (*3)
It is possible to append groups and add new groups., (*4)
Getting a setting is straight forward., (*5)
//gets the value of bar in the foo group
$value = $s->get('foo.bar'); //foo
//gets the value of bars in the foo group
$values = $s->get('foo.bars'); //foos
This can be done by giving $settings as a parameter when creating a new Settings object, (*6)
$settings can either be a json string or an array, (*7)
$settings = array('foo'=>array('bar'=>'foo', 'bars'=>'foos'));
$s = new Settings($settings);
print_r($s->getSettingsAsArray());
/**
* Array
*(
* [foo] => Array
* (
* [bar] => foo
* [bars] => foos
* )
*
*)
*
*/
Or by calling the fill method, (*8)
$settings = array('foo'=>array('bar'=>'foo', 'bars'=>'foos'));
$s = new Settings();
$s->fill($settings);
To add new settings to a existing group goes as follows, (*9)
//we assume the settings file is already filled
$s->appendGroup('foo','cackes');
The group foo has now a new key value pair with the key 'cackes'. The value defaults to ''
If you want to add a default value use a third parameter like this, (*10)
$s->appendGroup('foo','cackes','cheesecake');
Adding a new group to the settings file, and give it some default settings., (*11)
$s->addGroup('files')->appendGroup('files', 'logfile', '/location/to/file.log');
[x] Make it a separate package, (*12)
[x] Add functionality to extend the json settings, (*13)
[x] Make the settings file dynamic, (*14)
[ ] Allow greater settings depth, (*15)
[ ] Allow arrays when adding a new group, (*16)