originphp / value-store
OriginPHP ValueStore (KVS)
Requires
- php: >=7.3.0
Requires (Dev)
- originphp/xml: ^2.0
- originphp/yaml: ^2.0
- phpstan/phpstan: ^0.12.64
- phpunit/phpunit: ^9.0
Suggests
- originphp/xml: Install this if you want to work with XML files
- originphp/yaml: Install this if you want to work with Yaml files
README
ValueStore is a Key-Value Store (KVS) which provides a consistent interface for working with various types of stores, including JSON, Yaml, XML and PHP files.
Installation
To install this package
$ composer require originphp/value-store
Usage
The type of store that is used is detected by the file extension (json
, yml
, xml
, or php
), if it cannot detect or there is no extension then json
type will be used. This can also be overridden in the constructor.
You work with this like an object or array, and if the file exists it will load the existing data. To save data call the save
method.
use Origin\ValueStore\ValueStore; $settings = new ValueStore(storage_path('settings.json')); $settings->email = 'demo@example.com' $settings->incomingServer = [ 'host' => 'mail.example.com', 'port' => 993, 'encryption' => 'ssl' ]; $settings->active = true; $settings->save();
You can also use isset
, unset
and count
unset($settings->key); $hasKey = isset($settings->key); $keys = count($settings);
You can iterate through the settings
foreach($settings as $key => $value){ ... }
To increment or decrement values in the store
$settings->increment('count'); $settings->decrement('count');
You can also pass a second argument with the amount you want to increase or decrease by.
$settings->increment('count', 4); $settings->decrement('count', 3);
You can also set/get/check values using functions.
$settings->set('foo','bar'); $settings->set(['foo'=>'bar','key'=>'value']); // Set multiple values. $foo = $settings->get('foo'); $keyExists = $settings->has('foo'); $setting->unset('foo'); $count = $settings->count();
You can also access as an array
$value = $settings['foo']; $settings['foo'] = 'bar' unset($settings['foo']); $has = isset($settings['foo']);
To clear all data in the ValueStore
(remember to call save if needed).
$settings->clear(); // clears all values
You can convert the ValueStore
object to any type on the fly
$settings->toArray(); $settings->toJson(); $settings->toPhp(); $settings->toXml(); // requires originphp/xml $settings->toYaml(); // requires originphp/yaml
Dependencies
To use Xml
you will need to install the following composer package
$ composer require originphp/xml
To use Yaml
you will need to install the following composer package
$ composer require originphp/yaml