Coder Social home page Coder Social logo

open-source-contributions / arraystorage Goto Github PK

View Code? Open in Web Editor NEW

This project forked from phpfluent/arraystorage

0.0 1.0 0.0 62 KB

Non-persistent way to use arrays as database

Home Page: http://documentup.com/PHPFluent/ArrayStorage

License: MIT License

Makefile 1.30% PHP 98.70%

arraystorage's Introduction

PHPFluent\ArrayStorage

Build Status Total Downloads License Latest Stable Version Latest 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:

use PHPFluent\ArrayStorage\Storage;

Creating and returning a collection

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

Inserting records to a collection

You can use a single array:

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

But you also can use a Record object:

use PHPFluent\ArrayStorage\Record;

$storage = new Storage();
$record = new Record();
$record->name = 'Henrique Moody';

$storage->users->insert($record);

You can create a Record object from Storage object:

$storage = new Storage();

$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 = new Storage();
$storage->users->delete(['name' => 'Henrique Moody']);

Removing all records from a collection

$storage = new Storage();
$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:

use PHPFluent\ArrayStorage\Converter;

Arr

Converts data into an array.

$converter = new Converter\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 = new Converter\Arr(false);
$converter->convert($storage->collectionName); // Returns an array of Record objects

Json

Converts data into JSON format.

$converter = new Converter\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 = new Converter\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 = new Converter\Xml();
$converter->convert($storage->collectionName); // Returns an string with the XML

arraystorage's People

Contributors

henriquemoody avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.