For a site I was working on the admin should be able to switch on or off a form that's displayed on the homepage. Question: where's the best place to store the value of that switch? Creating a table and column for it in the database seems overkill. Putting it in a never expiring cache does not feel right. I think the best place to store such value is just write it to a simple file.

In the example above only one value needed to be stored, but for other projects there sometimes were two or three of them. Over the years I found myself writing the same code over and over again to store and work with such values.

Our intern Jolita and I whipped up a valuestore package. It only provides one class: Valuestore. The values will get written as JSON in a given file. The API mostly reflects Laravel's caching API. Here's how you can work with it:

$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

As you see it's quite a simple class, but I'm sure it'll come in handy in the future. The package can be found on GitHub.