This is just a proof-of-concept, don't take it seriously. Please.
- Don't use it in production
- Don't manage large datasets (200+ elements, too deeply nested)
- You need at least
PHP 5.5to make it work - I don't support old things
- Grab it
- Add it to your project
- Include
lib/DotNotation/DotNotation.php - Don't forget about the unit-tests in case you want to modify some code (I used PHPUnit)
Dot notation is so cool. Want to take a look? Let's do this!
$structure = [
'foo' => 'bar',
'secret' => 42 ,
'agent' => [
'code' => '007' ,
'sunglasses' => 'cool',
'info' => [
'first_name' => 'James',
'second_name' => 'Bond',
],
],
];Imagine that you want to get James' second name. What would you do?
Would you just write $secondName = $structure ['agent']['info']['second_name'];?
Honestly, it looks really ugly. What about $secondName = $structure ['agent.info.second_name'];?
This one looks much better, doesn't it?
We can go even further and also use dot notation for removing, declaring and changing values.
// using the structure declared above
$dot = DotNotation::create ($structure);
// equals $dot ['foo']['bar'] = 42;
$dot ['foo.bar'] = 42; // we have just used that sexy dot notation
// everyone should see it!
echo $dot ['foo.bar'];
$dot ['foo.bar'] = 43; // much better
unset ($dot ['foo.bar']); // the meaning of life has been lostInterested now?
Class DotNotation implements ArrayAccess so you can use its instances as PHP arrays.
DotNotation will parse foo.bar , find the desired element in a data storage (which is unique to all DotNotation instances) and give you the result or throw an exception if something go wrong.
As always, magic takes up way too many resources, so you shouldn't use DotNotation in production or/and with large datasets.
First of all, create an instance.
$dot = DotNotation::create ();You can pass an array or another DotNotation object to the create method if needed.
Note that the from method will override all existing data.
If you want to append instead, see Merge datasets.
$dot->from (['foo' => 'bar']); // from an array
$dot->from ( DotNotation::create (['foo' => 'bar']) ); // from a DotNotation instance
$dot->from ('your json', DotNotation::JSON); // from a JSON schema
$dot->from ('your serialized php array', DotNotation::PHP_SERIALIZED); // from a serialize()'d array$dot->merge (['secret' => 42]); // merge $dot dataset with an array
$dot->merge ( DotNotation::create (['secret' => 42]) ); // or with another instance of DotNotation$allData = $dot->root ();
$allData = $dot->toArray (); // the same, but more readable in some cases $allDataInJson = $dot->to (DotNotation::JSON); // returns all data stored in cool JSON format
$allDataSerialized = $dot->to (DotNotation::PHP_SERIALIZED); // same in a serialize()'d array If you want to make $dot unchangeable, you should use readOnly method.
$dot->readOnly (); // returns the current state of $dot
$dot->readOnly (true); // makes $dot unchangeable, passing FALSE gives the opposite resultDotNotation is licensed under the MIT license.
Check the LICENSE file for more information.