Skip to content

sneakybush/dotnotation

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

35 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Dot Notation

This is just a proof-of-concept, don't take it seriously. Please.

Remember

  • Don't use it in production
  • Don't manage large datasets (200+ elements, too deeply nested)
  • You need at least PHP 5.5 to make it work - I don't support old things

Installation

  • 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)

Demo

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 lost

Interested now?

Magic Explained

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.

Usage

First of all, create an instance.

$dot = DotNotation::create ();

You can pass an array or another DotNotation object to the create method if needed.

Load data

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

Merge datasets

$dot->merge (['secret' => 42]); // merge $dot dataset with an array
$dot->merge ( DotNotation::create (['secret' => 42]) ); // or with another instance of DotNotation

Get dataset as array

$allData = $dot->root ();
$allData = $dot->toArray (); // the same, but more readable in some cases 

Get dataset in other formats

$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 

Switch to read-only mode

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 result

License

DotNotation is licensed under the MIT license.

Check the LICENSE file for more information.

About

Proof-Of-Concept #1: Simple PHP library that helps you manage arrays via sexy dot notation

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages