avris

This is a lite version. See full website.


Avris Bag • Nicer arrays for PHP

Avris Bag

Avris Bag is a set of helpers that make working with arrays in PHP way easier.

Instalation

composer require avris/bag

Bag

Bag class represents a key-value store. It can be used both as an array and as an object, leading to a shorter and nicer code:

$array = [
    'foo' => 'bar',
    'lorem' => [
        'ipsum' => ['dolor', 'sit', 'amet']
    ],
];

$bag = new Bag($array);

// all return 'bar'
var_dump(isset($array['foo']) ? $array['foo'] : null);
var_dump($bag->get('foo'));
var_dump($bag['foo']);
var_dump($bag('foo'));

// all return null
var_dump(isset($array['nonexistent']) ? $array['nonexistent'] : null);
var_dump($bag->get('nonexistent'));
var_dump($bag['nonexistent']);
var_dump($bag('nonexistent'));

// all return 'default'
var_dump(isset($array['nonexistent']) ? $array['nonexistent'] : 'default');
var_dump($bag->get('nonexistent', 'default'));
var_dump($bag['nonexistent'] ?: 'default');
var_dump($bag('nonexistent', 'default'));

// all are equivalent
$array['x'] => 'y';
$bag->set('x', 'y');
$bag['x'] = 'y';

// all are equivalent
var_dump(count($array));
var_dump($bag->count());
var_dump(count($bag));

// all are equivalent
var_dump(count($array) === 0);
var_dump($bag->isEmpty());
var_dump(count($bag) === 0);

// all are equivalent
var_dump(array_keys($array));
var_dump($bag->keys());
var_dump(array_keys($bag));

// all are equivalent
var_dump(isset($array['foo']));
var_dump($bag->has('foo'));
var_dump(isset($bag['foo']));

// all are equivalent
unset($array['foo']);
$bag->delete('foo');
unset($bag['foo']);

// all are equivalent
$array = [];
$bag->clear();

Just like with a simple array, you can also iterate over a Bag and json_encode it.

Additional features include:

Set

Set is a similar structure, but it doesn’t care about the keys and it makes sure that all the values are unique. Sometimes it’s helpful to instantiate it with a callback:

$set = new Set(['post', 'get', 'GET'], 'strtoupper');
// internal values are: 'POST' and 'GET'

var_dump($set->has('post')); // true
var_dump($set->has('POST')); // true
var_dump($set->has('DELETE')); // false

Methods:

BagHelper

This class provides four static methods:

Nested

This class provides two static methods for a nested access to arrays and objects:

For example:

Nested::get($container, ['github', 'webhooks', 'foo']);

could return a value of:

$container->get('github')->getWebhooks()['foo'];

unless any of the chain elements doesn’t exist – then $default is returned.

Similarly:

Nested::set($array, ['foo', 'bar', 'baz'], 8);

is in equivalent to:

$array['foo']['bar']['baz'] = 8;

but it creates all the arrays on the way, if they don’t exist yet.

QueueBag

This class works like a normal queue, but lets you enqueue and dequeue a key-value pair and gives you standard Bag features.

$queue = new QueueBag();
$queue->enqueue('key', 'value')
$queue->enqueue('foo', 'bar')
list($key, $value) = $queue->dequeue()

StackBag

This class works like a normal stack, but lets you push and pop a key-value pair and gives you standard Bag features.

$stack = new StackBag();
$stack->push('key', 'value')
$stack->push('foo', 'bar')
list($key, $value) = $stack->pop()

tags: