Skip to content

Repository files navigation

PHPFluent\ArrayStorage

Build StatusTotal DownloadsLicenseLatest Stable VersionLatest Unstable Version

Non-persistent way to use arrays as database.

Installation

Package is available on Packagist, you can install it using Composer.

composer require phpfluent/arraystorage

Usage

The examples below are using the following use statement at the beginning of the file:

usePHPFluent\ArrayStorage\Storage;

Creating and returning a collection

$storage = newStorage();
$storage->users; // This is a collection

Inserting records to a collection

You can use a single array:

$storage = newStorage();
$storage->users->insert(['name' => 'Henrique Moody']);

But you also can use a Record object:

usePHPFluent\ArrayStorage\Record;
$storage = newStorage();
$record = newRecord();
$record->name = 'Henrique Moody';
$storage->users->insert($record);

You can create a Record object from Storage object:

$storage = newStorage();
$record = $storage->users->record(); // You also can provide default data, like an array or stdClass$record->name = 'Henrique Moody';
$storage->users->insert($record);

An important point to note is that, after you insert the record to the collection object it gives to the record an unique (incremental integer) id property.

Removing records from a collection

$storage = newStorage();
$storage->users->delete(['name' => 'Henrique Moody']);

Removing all records from a collection

$storage = newStorage();
$storage->users->delete();

Finding multiple records into a collection

$storage->users->findAll(['status !=' => false]); // Return an Collection object with the partial result (if any)

Finding single record into a collection

$storage->users->find(['priority >=' => 4]); // Return an Record object with the first matched result (if any) or NULL

Using Criteria object

$criteria = $storage->users->criteria();
$criteria->foo->equalTo(2)
->bar->in([1, 2, 3])
->baz->regex('/^[0-9]{3}$/')
->qux->like('This _s spart%')
->quux->iLike('tHiS _S sPaRt%')
->corge->between(array(1, 42))
->grault->lessThan(1000)
->garply->greaterThan(0)
->waldo->notEqualTo(false)
->fred->greaterThanOrEqualTo(13);
$storage->users->find($criteria);

Transforming data into other formats

Sometimes you want to convert data into other formats, for that cases we have the converters.

The converters accept any object that implements Traversable interface and since Record, Collection and Storage classes implements this interface you are able to convert any of them into other formats.

For the examples below we assume you have the follow use statements:

usePHPFluent\ArrayStorage\Converter;

Arr

Converts data into an array.

$converter = newConverter\Arr();
$converter->convert($storage->collectionName); // Returns an array with the records as array too

If you do not want to convert the children of the object (values that also implement Traversable interface) you just have to define a flag on the constructor of this converter, like:

$converter = newConverter\Arr(false);
$converter->convert($storage->collectionName); // Returns an array of Record objects

Json

Converts data into JSON format.

$converter = newConverter\Json();
$converter->convert($storage->collectionName); // Returns an string with the JSON

You also can define json_encode() options on the constructor of this converter, like:

$converter = newConverter\Json(JSON_NUMERIC_CHECK);
$converter->convert($storage->collectionName); // Returns the JSON but encodes numeric strings as numbers

We use JSON_PRETTY_PRINT as default option.

Xml

Converts data into XML format.

$converter = newConverter\Xml();
$converter->convert($storage->collectionName); // Returns an string with the XML

About

Non-persistent way to use arrays as database

Resources

Contributing

Stars

37 stars

Watchers

6 watching

Forks

Releases

Packages

Used by

Contributors

Languages