Skip to content

Fluent storage repository with dot notated key support

License

Notifications You must be signed in to change notification settings

kamalkhan/repository

Repository files navigation

Repository

Travis Build Status Codacy Coverage Codacy Grade Packagist Downloads License

Fluent storage repository with dot notated key support.

Install

You may install this package using composer.

$ composer require bhittani/repository

Usage

This package provides a fluent and intuitive storage repository.

<?php

require_once __DIR__ . '/vendor/autoload.php';

use Bhittani\Repository\Repository;

$storage = new Repository;

// See below examples for usage...

Store and retrieve values

// Store and retrieve a value.
$storage->set('foo', 'bar');
echo $storage->get('foo'); // 'bar'

// Store an array and retrieve a value using dot notated key access.
$storage->set('app', ['name' => 'Acme', 'version' => '0.1.0']);
echo $storage->get('app.version'); // '0.1.0'

// Store a value using dot notated keys.
$storage->set('db.sqlite.path', ':memory:');
$storage->set('db.sqlite.prefix', 'acme_');
var_dump($storage->get('db.sqlite')); // ['path' => ':memory:', 'prefix' => 'acme_']

Preset an undefined key

$storage->preset('a', 'b');
echo $storage->get('a'); // 'b'

$storage->set('x', 'y');
$storage->preset('x', 'z');
echo $storage->get('x'); // 'y'

Append a value

$storage->set('append', ['foo']);
$storage->append('append', 'bar');
var_dump($storage->get('append')); // ['foo', 'bar']

Prepend a value

$storage->set('prepend', ['foo']);
$storage->append('prepend', 'bar');
var_dump($storage->get('prepend')); // ['bar', 'foo]

Increment a value

echo $storage->get('incr'); // null

$storage->increment('incr');
echo $storage->get('incr'); // 1

$storage->increment('incr', 5);
echo $storage->get('incr'); // 6

Decrement a value

echo $storage->get('decr'); // null

$storage->decerement('decr');
echo $storage->get('decr'); // -1

$storage->decerement('decr', 5);
echo $storage->get('decr'); // -6

Fallback to a default value

echo $storage->get('bar'); // null

echo $storage->get('bar', 'fallback'); // 'fallback'

Check whether a key is set

var_dump($storage->has('bar')); // (bool) false

Get all items

$storage->add('foo', 'bar');
$storage->add('beep.boop', 'baz');

var_dump($storage->all()); // ['foo' => 'bar', 'beep' => ['boop' => 'baz']]

Changelog

Please see CHANGELOG for more information on what has changed.

Testing

$ git clone https://github.com/kamalkhan/repository
$ cd repository
$ composer install
$ composer test

Contributing

Please see CONTRIBUTING and CONDUCT for details.

Security

If you discover any security related issues, please email [email protected] instead of using the issue tracker.

Credits

License

The MIT License (MIT). Please see the License File for more information.