Download the PHP package railken/search-query without Composer
On this page you can find all versions of the php package railken/search-query. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Informations about the package search-query
Search Query
Converts a simple expression (e.g. 'x eq 1 or y gt 2') into a tree object. This can be pretty usefull when building the API endpoint for a search.
Requirements
PHP 7.1 or later.
Composer
You can install it via Composer by typing the following command:
Demo
Usage
A simple usage looks like this:
The result formatted in json of $result
:
Nodes
Key node
All alphanumeric (starting with alphabetic character) are converted as . Other characters allowed:
Examples:
Value node
All numbers and strings are converted as . The delimiter of strings can be either or
Examples:
Group node
All round parenthesis create a group node . Of course a nested group can be made. If a parenthesis is missing an exception will be thrown
Examples:
AND, OR
This operators requires a before and after, otherwise an exception will be thrown.
Can be expressed as literals (and, or) or as symbols (&&, ||)
Important: If a parent group is defined and the LogicNode is the only child, the LogicNode will take the place of the GroupNode.
Examples:
EQ, NOT_EQ, GT, GTE, LT, LTE
All these operators requires or a before and afters. Can be expressed as literals (eq, not eq, gt, gte, lt, lte) or as symbols (=, !=, >, >=, < <=)
Examples:
Comparison can be also made between two keys
Examples:
CT, SW, EW
These operators will handle comparison with strings: contains, start with and end with.
Examples:
NULL, NOT NULL
These operators don't require a node after the operator.
Custom resolver
As you already saw, in order to parse the query you have to add the resolvers. So you are free to add any resolvers you want, but pay attention to the order: KeyValue and NodeValue are the foundation for all the resolvers, so be carefull.
Here's an example of custom resolver and node
CustomResolver.php
CustomNode.php
Remember to add the resolver when you're creating the instance of the parser
If you have a more complicated node to resolve simply add the method resolve to the CustomResolver.
Railken\SQ\Languages\BoomTree\Resolvers\CustomResolver
There is also a CustomResolver that works and cycle with all children and it's configurabile through a Closure, it's Railken\SQ\Languages\BoomTree\Resolvers\CustomResolver
. For istance you could attach a prefix to all KeyNode doing this:
License
Open-source software licensed under the MIT license.