2017 © Pedro Peláez
 

library array-query

Array Query

image

mauretto78/array-query

Array Query

  • Wednesday, January 10, 2018
  • by mauretto78
  • Repository
  • 1 Watchers
  • 2 Stars
  • 373 Installations
  • PHP
  • 2 Dependents
  • 0 Suggesters
  • 0 Forks
  • 0 Open issues
  • 16 Versions
  • 4 % Grown

The README.md

Array Query

Scrutinizer Code Quality SensioLabsInsight Codacy Badge Coverage Status license Packagist, (*1)

Array Query allows you to perform queries on multidimensional arrays., (*2)

Use Cases

This library is suitable for you if you need to perform some queries on:, (*3)

  • static php arrays
  • in-memory stored arrays
  • parsed json (or yaml) files

Basic Usage

To instantiate the QueryBuilder do the following:, (*4)

use ArrayQuery\QueryBuilder;

$array = [
    [
        'id' => 1,
        'title' => 'Leanne Graham',
        'email' => 'Sincere@april.biz',
        'rate' => 5,
        'company' => [
            'name' => 'Romaguera-Jacobson',
            'catchPhrase' => 'Face to face bifurcated interface',
            'bs' => 'e-enable strategic applications'
        ]
    ],
    [
        'id' => 2,
        'title' => 'Ervin Howell',
        'email' => 'Shanna@melissa.tv',
        'rate' => 3,
        'company' => [
            'name' => 'Robel-Corkery',
            'catchPhrase' => 'Multi-tiered zero tolerance productivity',
            'bs' => 'transition cutting-edge web services'
        ]
    ],
    [
        'id' => 3,
        'title' => 'Clementine Bauch',
        'email' => 'Nathan@yesenia.net',
        'rate' => 4,
        'company' => [
            'name' => 'Keebler LLC',
            'catchPhrase' => 'User-centric fault-tolerant solution',
            'bs' => 'revolutionize end-to-end systems'
        ]
    ],
    // ..
]

QueryBuilder::create($array);

// to add an element to your array. Do this BEFORE make a query on the array
$element = [
   'id' => 4,
   'title' => 'Patricia Lebsack',
   'email' => 'Julianne.OConner@kory.org',
   'rate' => 2,
   'company' => [
       'name' => 'Robel-Corkery',
       'catchPhrase' => 'Multi-tiered zero tolerance productivity',
       'bs' => 'transition cutting-edge web services'
   ]
];
$qb->addElement($element, 4);

// to remove an element from array by his key. Do this BEFORE make a query on the array
$qb->removeElement(3);

Data consistency

QueryBuilder checks for your data consistency. If an inconsistency is detected a NotConsistentDataException will be raised:, (*5)

use ArrayQuery\QueryBuilder;

$array = [
    [
        'id' => 1,
        'title' => 'Leanne Graham',
        'email' => 'Sincere@april.biz',
        'rate' => 5,
        'company' => [
            'name' => 'Romaguera-Jacobson',
            'catchPhrase' => 'Face to face bifurcated interface',
            'bs' => 'e-enable strategic applications'
        ]
    ],
    [
        'id' => 2,
        'title' => 'Ervin Howell',
        'email' => 'Shanna@melissa.tv',
        'rate' => 3,
        'company' => [
            'name' => 'Robel-Corkery',
            'catchPhrase' => 'Multi-tiered zero tolerance productivity',
            'bs' => 'transition cutting-edge web services'
        ]
    ],
    [
        'id' => 3,
        'title' => 'Clementine Bauch',
        'email' => 'Nathan@yesenia.net',
        'rate' => 4,
        'company' => [
            'name' => 'Keebler LLC',
            'catchPhrase' => 'User-centric fault-tolerant solution',
            'bs' => 'revolutionize end-to-end systems'
        ],
        'extra-field' => 'this is an extra field'
    ],
]

// NotConsistentDataException will be raised
QueryBuilder::create($array);

Quering, sorting and get results

You can perform queries on your array. You can concatenate criteria:, (*6)

use ArrayQuery\QueryBuilder;

// ..

$qb = QueryBuilder::create($array);
$qb
    ->addCriterion('title', 'Leanne', 'CONTAINS')
    ->addCriterion('rate', '3', '>')
    ->sortedBy('title', 'DESC');

// you can search by nested keys    
$qb->addCriterion('company.name', 'Romaguera-Jacobson');

// get results    
foreach ($qb->getResults() as $element){
    // ...
}

// get first result
$first = $qb->getFirstResult();

// get last result
$last = $qb->getLastResult();

// get a result by index
$thirdResult = $qb->getResult(3);

Avaliable criteria operators

  • = (default operator, can be omitted)
  • >
  • <
  • <=
  • >=
  • !=
  • ARRAY_MATCH
  • CONTAINS (case insensitive)
  • ENDS_WITH
  • EQUALS_DATE
  • GT_DATE
  • GTE_DATE
  • IN_ARRAY
  • IN_ARRAY_INVERSED
  • LT_DATE
  • LTE_DATE
  • STARTS_WITH

Avaliable sorting operators

  • ASC (default operator, can be omitted)
  • DESC
  • DATE_ASC
  • DATE_DESC

Joins

You can join arrays. Please consider this full example:, (*7)

use ArrayQuery\QueryBuilder;

$users = [
    [
        'id' => 1,
        'name' => 'Mauro Cassani',
        'id_category' => 3,
        'email' => 'assistenza@easy-grafica.com'
    ],[
        'id' => 2,
        'name' => 'Mario Rossi',
        'id_category' => 3,
        'email' => 'mario.rossi@gmail.com'
    ],[
        'id' => 3,
        'name' => 'Maria Bianchi',
        'id_category' => 1,
        'email' => 'maria.bianchi@gmail.com'
    ]
];
$category = [
    'id' => 3,
    'name' => 'Web Developer'
];

$qb = QueryBuilder::create($users)
    ->join($category, 'category', 'id_category', 'id')
    ->addCriterion('category.id', 3);

foreach ($qb->getResults() as $element){
    // ...
}

Limit and Offset

You can add criteria and specify limit and offset for your query results:, (*8)

use ArrayQuery\QueryBuilder;

$qb = QueryBuilder::create($array);
$qb
    ->addCriterion('title', ['Leanne'], 'IN_ARRAY')
    ->addCriterion('rate', '3', '>')
    ->sortedBy('title')
    ->limit(0, 10);

foreach ($qb->getResults() as $element){
    // ...
}

Working with dates

You can perform queries based on datetime fields. You can use DATE_ASC or DATE_DESC operator to sort results by date. You must specify date format if your format is not YYYY-mm-dd:, (*9)

use ArrayQuery\QueryBuilder;

$qb = QueryBuilder::create($array);
$qb
    ->addCriterion('registration_date', '01/05/2017', 'GT_DATE', 'd/m/Y')
    ->addCriterion('rate', '3', '>')
    ->sortedBy('registration_date', 'DATE_DESC', 'd/m/Y')
    ->limit(0, 10);

foreach ($qb->getResults() as $element){
    // ...
}

Aliases

You can use aliases by using the as keyword as a delimiter. Do the following:, (*10)

use ArrayQuery\QueryBuilder;

$qb = QueryBuilder::create($array);
$qb
    ->addCriterion('name as n', 'Ervin Howell')
    ->addCriterion('username as user', 'Antonette')
    ->addCriterion('address.street as street', 'Victor Plains');

foreach ($qb->getResults() as $element){
    // ...
    // now you have
    // $element['n']
    // $element['user']
    // $element['street']
}

Shuffled results

You can shuffle query results by using getShuffledResults method:, (*11)

use ArrayQuery\QueryBuilder;

$qb = QueryBuilder::create($array);

foreach ($qb->getShuffledResults() as $element){
    // ...
}

More examples

Please refer to QueryBuilderTest for more examples., (*12)

Support

If you found an issue or had an idea please refer to this section., (*13)

Authors

License

This project is licensed under the MIT License - see the LICENSE.md file for details, (*14)

The Versions

10/01 2018

dev-master

9999999-dev https://github.com/mauretto78/array-query

Array Query

  Sources   Download

MIT

The Requires

  • php ^5.6 || ^7.0

 

The Development Requires

php array query

10/01 2018

v1.1.10

1.1.10.0 https://github.com/mauretto78/array-query

Array Query

  Sources   Download

MIT

The Requires

  • php ^5.6 || ^7.0

 

The Development Requires

php array query

05/01 2018

v1.1.9

1.1.9.0 https://github.com/mauretto78/array-query

Array Query

  Sources   Download

MIT

The Requires

  • php ^5.6 || ^7.0

 

The Development Requires

php array query

05/01 2018

v1.1.8

1.1.8.0 https://github.com/mauretto78/array-query

Array Query

  Sources   Download

MIT

The Requires

  • php ^5.6 || ^7.0

 

The Development Requires

php array query

15/11 2017

v1.1.7

1.1.7.0 https://github.com/mauretto78/array-query

Array Query

  Sources   Download

MIT

The Requires

  • php ^5.6 || ^7.0

 

The Development Requires

php array query

15/11 2017

v1.17

1.17.0.0 https://github.com/mauretto78/array-query

Array Query

  Sources   Download

MIT

The Requires

  • php ^5.6 || ^7.0

 

The Development Requires

php array query

27/10 2017

v1.1.6

1.1.6.0 https://github.com/mauretto78/array-query

Array Query

  Sources   Download

MIT

The Requires

  • php ^5.6 || ^7.0

 

The Development Requires

php array query

24/10 2017

v1.1.5

1.1.5.0 https://github.com/mauretto78/array-query

Array Query

  Sources   Download

MIT

The Requires

  • php ^5.6 || ^7.0

 

The Development Requires

php array query

21/10 2017

v1.1.4

1.1.4.0 https://github.com/mauretto78/array-query

Array Query

  Sources   Download

MIT

The Requires

  • php ^5.6 || ^7.0

 

The Development Requires

php array query

17/10 2017

v1.1.3

1.1.3.0 https://github.com/mauretto78/array-query

Array Query

  Sources   Download

MIT

The Requires

  • php ^5.6 || ^7.0

 

The Development Requires

php array query

31/08 2017

v1.1.2

1.1.2.0 https://github.com/mauretto78/array-query

Array Query

  Sources   Download

MIT

The Requires

  • php ^5.6 || ^7.0

 

The Development Requires

php array query