dev-master
9999999-devA simple parameter bag to make working with arrays easier
MIT
The Requires
- php >=5.6
The Development Requires
by Adam Nicholson
A simple parameter bag to make working with arrays easier
Bag is a simple parameter bag class to make working with arrays easier., (*1)
Boilerplate isset
calls whenever working with arrays can be a pain., (*3)
function foo (array $data) { if (!isset($data['status']) || !isset($data['data']['foo'])) || $data['status'] !== 'success') { return false; } return $data['data']['foo']; } // vs function bar (array $data) { $data = new Bag($data); if ($bag->get('status') !== 'success') { return false; } return $bag->get('data.foo'); }
Note:
isset
boilerplate will hopefully be much less of a chore in PHP7 with the isset ternary operator, (*4)
$array = [ 'foo' => 'bar', 'hello' => 'world', 'items' => [ 'first' => 'fizz', 'second' => 'buzz' ] ]; $bag = new Adam\Bag\Bag($array); // Get a value by key $bag->get('foo'); // string "bar" // Set some value by key $bag->set('fizz', 'buzz'); // Return a default value if the key is not set $bag->get('ziff'); // null $bag->get('ziff', 'my_default_value'); // string "my_default_value" // Use dot notation for multidimensional arrays $bag->get('items.first'); // string "fizz" $bag->set('items.third', 'foovalue'); // Get the value of an item and then remove it $bag->pluck('foo'); // string "bar" $bag->get('foo'); // null // Get the raw data $bag->all(); // array // Empty the data $bag->flush(); // Or just use it like an array $bag['foo'] = 'bar'; $bag['foo']; // string "bar" $bag->get('foo'); // string "bar"
We welcome any contributions to this project. They can be made via GitHub issues or pull requests., (*5)
This project is licensed under the MIT License - see the LICENSE.txt
file for details, (*6)
Adam Nicholson - adamnicholson10@gmail.com, (*7)
A simple parameter bag to make working with arrays easier
MIT