1. Go to this page and download the library: Download graste/params library. Choose the download type require.
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
graste / params example snippets
$data = array(
'str' => 'some string',
'first_level' => 'first level',
'nested' => array(
'str' => 'some nested string',
'2nd level' => 'second level',
),
'more' => array(
'str' => 'other nested string',
)
);
// base object of the aliased versions:
$arrayobj = new \Params\ConfigurableArrayObject($array);
// create mutable parameters instance:
$params = new \Params\Parameters($data);
// or initialize an immutable instance that prevents further modifications after construction
$params = new \Params\Immutable\ImmutableParameters($data);
// do you like options instead of parameters in your classes?
$params = new \Params\Options($data);
$params = new \Params\Immutable\ImmutableOptions($data);
// do you like settings instead of parameters in your classes?
$params = new \Params\Settings($data);
$params = new \Params\Immutable\ImmutableSettings($data);
// use it as a recursive object:
$params->has("foo") // returns true now
$params->get("str") // gives "some string"
$params->get("nested") // gives array stored under "nested" key
$params->get("nested")->get('str') // gives "some nested string"
$params->get("non-existant", "default value") // gives "default value" as given key is non existant
$params->get("nested")->set("foo", "bar") // sets key "foo" to value "bar" on the "nested" array
$params->getKeys() // returns all first level keys
$params->toArray() // returns internal array
// or use the mutable methods
$params->set("foo", "bar") // sets key "foo" to value "bar"
$params->add(array|ArrayAccess) // add array or other ArrayAccess implementing object to current instance
$params->clear() // empty internal array
$params->map(function($key, $value) { … }) // modifies each value to the value returned by the callback
// retrieve values using expressions
$params->getValues("foo") // gives "bar"
$params->getValues("nested.str") // gives "some nested string"
$params->getValues('*.str') // gives array("some nested string", "other nested string")
$params->getValues('[str, nested.str]') // gives array("some string", "some nested string")
$params->getValues('nested."2nd level" || first_level') // gives "second level" as that key exists; other expression not evaluated
$params->getValues('first_level || nested."2nd level"') // gives "first level" as that key exists; other expression not evaluated
// use it as an array:
$params["str"] // gives "some string"
$params["nested"] // gives the array under the "nested" key
$params[1] // gives "first level"
// use it as an object with properties
$params->foo = 'bar' // sets key 'foo' to value 'bar'
$params->filter->bool = 'yes' // sets $params['filter']['bool'] to value 'yes'