Download the PHP package braincrafted/arrayquery without Composer
On this page you can find all versions of the php package braincrafted/arrayquery. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download braincrafted/arrayquery
More information about braincrafted/arrayquery
Files in braincrafted/arrayquery
Package arrayquery
Short Description ArrayQuery is a library to query arrays.
License MIT
Informations about the package arrayquery
ArrayQuery
ArrayQuery is a library to query arrays.
Motivation
Data is often stored in arrays of arrays (for example, after reading it from CSV) and some items (or rows) have to be picked out before the data can be further processed or stored in a database. Writing such code is not very hard, but it often gets messy. Loops within loops, multiple if or switch statement, temporary variables and so on. ArrayQuery provided a clean and testable interface (inspired by query builders from ORMs) for these "array queries."
Installation
ArrayQuery can be installed using Composer:
Usage
The ArrayQuery
object has to be initialized with a SelectEvaluation
and a WhereEvaluation
object. Filters can be
added to SelectEvaluation
and filters and operators can be added to WhereEvaluation
.
However, the QueryBuilder
can be used to create an instance of ArrayQuery
with built-in operators and filters.
The query object can be used to build queries and execute them. Building the query object contains of three steps:
- Select the fields to be returned
- From the datasource
- Where a clause is matched
When the query is built it has to be executed.
Select
All elements of an item can be selected using the star *
operator:
Single elements of an item can be selected:
Multiple elements of an item can be selected:
Filters can be applied in both cases:
Multiple filters can also be applied:
From
Next the data source from which to select from has to be defined:
Where
Where clauses define which items from the data source are put in the result set:
There are numerous different operators available, which can be defined as third parameter:
Before the clause is evaluated filters can be applied to the test value:
Filters can have arguments:
Execute
There are multiple ways to execute a query.
Find all results:
Find one result:
Note: For performance reasons the first result is returned immediately. There is no error or exception when multiple results are returned.
Find scalar results:
Note: This only works when only one field is selected, an exception is thrown when multiple fields are selected (either through enumeration or by using the star operator).
Find one scalar result:
Note: The same notes as for findOne()
and findScalar()
apply here.
Builtin operators and filters
Some operators and filters are builtin and can be used out of the box (if the QueryBuilder
is used to create the
ArrayQuery
object).
Operators
- Equal
=
- Not equal
!=
- Greater
>
- Greater or equal
>=
- Lower
<
- Lower or equal
<=
- Like
like
(case insensitibe and%
can be used to at the beginning and end to match anything) - Not like
notlike
Filters
Filters can optionally have arguments. Separate arguments from the filter name by a space and separate multiple arguments using a comma.
- Length
length
- Lowercase
lower
- Uppercase
upper
- Trim
trim
- Left trim
ltrim
- Right trim
rtrim
- Replace
replace
(two arguments: search and replace)
Author
License
For the full copyright and license information, please view the LICENSE file that was distributed with this source code.