Skip to content

mmamedov/array-property

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

19 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Build Status Latest Stable Version License

ArrayProperty

Syntax sugar for working with arrays in PHP. Read and write to arrays in an object-oriented style. Keys are treated as properties.

ArrayProperty can be used on any PHP array variable.

Install using composer (recommended):

composer require mmamedov/array-property

Or manually add to your composer.json file:

"require": {
    "mmamedov/array-property": "^1.1"
}

Usage

Consider the following sample PHP array:

$sample =
    array(
        'app' => array(
            'log_dir' => '/log/path',
            'debug' => false,
            'log' => true,
            'version' => '2.3',
            'deep' => array(
                'inner' => 'some value',
                'level' => '2'
            )
        ),
        'my node' => 'some value'
    );

This is how easy to work with it using ArrayProperty:

$a = new ArrayProperty($sample);
echo $a->app->log_dir;      //outputs /log/path
echo $a->app->deep->inner;  //outputs 'some value'

//convert to array
$deep = $a->app->deep->toArray();
print_r($deep);             //outputs deep as array;

//check if value exists:
$a->app->exist('log_dir')   //returns true

Load new array and acces its values inline:

$prop = new ArrayProperty(array());
$new = array(1 => 'apple', 2 => 'orange', 3 => 'olive', 4 => 'grapes', 'multi'=>array('key'=>'value'));
echo $prop->loadArray($new)->{1}; //outputs "apple"
echo $prop->multi->key;           //outputs "value"

Now let's add new element to the $prop ArrayProperty object we created above:

$prop->myNewNode = "myValue";   //you can assign arrays as well.
echo $prop->myNewNode;          //outputs 'myValue';
//overwrite existing elements
$prop->{1} = "banana"; 
echo $prop->{1};                //now outputs "banana" instead of "apple"

Please note that multidimensional write is not supported, like in $prop->firstIndex->second = "value";

You can switch modes between OBJECT_MODE and MIXED_MODE(default). Mode is set as second parameter in the constructor, or by calling setMode() method. In MIXED_MODE array values are returned in their original type, whereas in OBJECT_MODE returned values are always ArrayProperty object:

$prop->setMode(ArrayProperty::OBJECT_MODE);
var_dump($prop->myNewNode);     //returns ArrayProperty object

$prop->setMode(ArrayProperty::MIXED_MODE);
var_dump($prop->myNewNode);     //returns "myValue" which was set before

For more examples see code inside ArrayProperty examples directory.

Enjoy!

About

PHP array "sweetener". Work with arrays in an easy and object-oriented way. Array keys are treated as properties.

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages