This package makes it easy to store and retrieve some values. Stores values are saved as a json in a file.
It can be used like this:
$valuestore = Valuestore::make($pathToFile);
$valuestore->put('key', 'value');
$valuestore->get('key'); // Returns 'value'
$valuestore->has('key'); // Returns true
// Specify a default value for when the specified key does not exist
$valuestore->get('non existing key', 'default') // Returns 'default'
$valuestore->put('anotherKey', 'anotherValue');
// Put multiple items in one go
$valuestore->put(['ringo' => 'drums', 'paul' => 'bass']);
$valuestore->all(); // Returns an array with all items
$valuestore->forget('key'); // Removes the item
$valuestore->flush(); // Empty the entire valuestore
$valuestore->flushStartingWith('somekey'); // remove all items who's keys start with "somekey"
$valuestore->increment('number'); // $valuestore->get('key') will return 1
$valuestore->increment('number'); // $valuestore->get('key') will return 2
$valuestore->increment('number', 3); // $valuestore->get('key') will return 5
// Valuestore implements ArrayAccess
$valuestore['key'] = 'value';
$valuestore['key']; // Returns 'value'
isset($valuestore['key']); // Return true
unset($valuestore['key']); // Equivalent to removing the value
// Valuestore impements Countable
count($valuestore); // Returns 0
$valuestore->put('key', 'value');
count($valuestore); // Returns 1
Read the usage section of this readme to learn the other methods.
Spatie is a webdesign agency based in Antwerp, Belgium. You'll find an overview of all our open source projects on our website.
You're free to use this package (it's MIT-licensed), but if it makes it to your production environment you are required to send us a postcard from your hometown, mentioning which of our package(s) you are using.
Our address is: Spatie, Samberstraat 69D, 2060 Antwerp, Belgium.
The best postcards will get published on the open source page on our website.
You can install the package via composer:
composer require spatie/valuestore
To create a Valuestore use the make
-method.
$valuestore = Valuestore::make($pathToFile);
All values will be saved as json in the given file.
When there are no values stored, the file will be deleted.
You can call the following methods on the Valuestore
/**
* Put a value in the store.
*
* @param string|array $name
* @param string|int|null $value
*
* @return $this
*/
public function put($name, $value = null)
/**
* Get a value from the store.
*
* @param string $name
*
* @return null|string
*/
public function get(string $name)
/*
* Determine if the store has a value for the given name.
*/
public function has(string $name) : bool
/**
* Get all values from the store.
*
* @return array
*/
public function all() : array
/**
* Get all values from the store which keys start with the given string.
*
* @param string $startingWith
*
* @return array
*/
public function allStartingWith(string $startingWith = '') : array
/**
* Forget a value from the store.
*
* @param string $key
*
* @return $this
*/
public function forget(string $key)
/**
* Flush all values from the store.
*
* @return $this
*/
public function flush()
/**
* Flush all values from the store which keys start with the specified value.
*
* @param string $startingWith
*
* @return $this
*/
public function flushStartingWith(string $startingWith)
/**
* Get and forget a value from the store.
*
* @param string $name
*
* @return null|string
*/
public function pull(string $name)
/**
* Increment a value from the store.
*
* @param string $name
* @param int $by
*
* @return int|null|string
*/
public function increment(string $name, int $by = 1)
/**
* Decrement a value from the store.
*
* @param string $name
* @param int $by
*
* @return int|null|string
*/
public function decrement(string $name, int $by = 1)
Please see CHANGELOG for more information what has changed recently.
$ composer test
Please see CONTRIBUTING for details.
If you discover any security related issues, please email [email protected] instead of using the issue tracker.
Spatie is a webdesign agency based in Antwerp, Belgium. You'll find an overview of all our open source projects on our website.
The MIT License (MIT). Please see License File for more information.