Home

Awesome

<p align="center"> <img src="https://raw.githubusercontent.com/nahid/qarray/master/media/example.png" width="600" alt="PHP QArray"> </p> # QArray - Query Engine For Array

QArray is query engine for PHP array data. It helps to developers querying any kind of array data with ORM like feel.

Installation

composer require nahid/qarray

Implementation

Since QArray is an abstract layer of code, so you have to implement your own system with a lots of pre build functionality.

Lets start our first implementation for JSON data.

class JsonQueryEngine extends \Nahid\QArray\QueryEngine
{
    public function readPath($path)
    {
        $data = file_get_contents($path);

        return json_decode($data, true);
    }

    public function parseData($data)
    {
        return json_decode($data, true);
    }
}

Here you see we implement a new engine for JSON data. QueryEngine is an abstract class, so we have to implement its two abstract function readPath and parseData

Usage

Our implementation were complete, now we can use this implementation.

class Product
{
    protected $query;

    public function __construct(\Nahid\QArray\QueryEngine $query)
    {
        $this->query = $query;
    }

    public function getMacbook()
    {
        try {
            return $this->query
                ->from('.')
                ->where('cat', 2)
                ->get();
        } catch (\Exception $e) {
           return false;
        }

    }
}

Here we develop a class for Products and this class use JsonQueryEngine for fetch data from JSON file.

Lets see, how to use it.

here is our JSON file data.json

[
  {"id":1, "name":"iPhone", "cat":1, "price": 80000},
  {"id":2, "name":"macbook pro 2017", "cat": 2, "price": 210000},
  {"id":3, "name":"Redmi 3S Prime", "cat": 1, "price": 12000},
  {"id":4, "name":"Redmi 4X", "cat":1, "price": 15000},
  {"id":5, "name":"macbook air", "cat": 2, "price": 110000}
]

And now we read it to querying data.

$data = new JsonQueryEngine('data.json');
$query = new SomethingBuilder($data);

dump($query->getMacbook()->toArray());

Output

array:2 [
  1 => array:4 [
    "id" => 2
    "name" => "macbook pro 2017"
    "cat" => 2
    "price" => 210000
  ]
  4 => array:4 [
    "id" => 5
    "name" => "macbook air"
    "cat" => 2
    "price" => 110000
  ]
]

Pretty neat, huh?

Let's explore the full API to see what else magic this library can do for you. Shall we?

API

List of API:

Available operation for where clause

example:

Let's say you want to find the 'users' who has id of 1. You can do it like this:

$q = new Jsonq('data.json');
$res = $q->from('users')->where('id', '=', 1)->get();

You can add multiple where conditions. It'll give the result by AND-ing between these multiple where conditions.

$q = new Jsonq('data.json');
$res = $q->from('users')
->where('id', '=', 1)
->where('location', '=', 'barisal')
->get();

See a detail example here.

orWhere(key, op, val)

Parameters of orWhere() are the same as where(). The only difference between where() and orWhere() is: condition given by the orWhere() method will OR-ed the result with other conditions.

For example, if you want to find the users with id of 1 or 2, you can do it like this:

$q = new Jsonq('data.json');
$res = $q->from('users')
->where('id', '=', 1)
->orWhere('id', '=', 2)
->get();

See detail example here.

whereIn(key, val)

This method will behave like where(key, 'in', val) method call.

whereNotIn(key, val)

This method will behave like where(key, 'notin', val) method call.

whereNull(key)

This method will behave like where(key, 'null') or where(key, '=', null) method call.

whereNotNull(key)

This method will behave like where(key, 'notnull') or where(key, '!=', null) method call.

whereStartsWith(key, val)

This method will behave like where(key, 'startswith', val) method call.

whereEndsWith(key, val)

This method will behave like where(key, 'endswith', val) method call.

whereContains(key, val)

This method will behave like where(key, 'contains', val) method call.

whereDataType(key, val)

This method will behave like whereDataType(key, 'type', val) method call.

sum(column)

Bugs and Issues

If you encounter any bugs or issues, feel free to open an issue at github.

Also, you can shoot me an email to mailto:nahid.dns@gmail.com for hugs or bugs.