Search Parser
, (*1)
Parses Lucene/Google style search strings into PostgresSQL full text query string., (*2)
Installation
Install using composer:, (*3)
composer require digitlab/search-parser
Add the service provider in app/config/app.php:, (*4)
DigitLab\SearchParser\SearchParserServiceProvider::class,
Usage
Basic Usage
To just parse a full text query you can simply use SearchParser:, (*5)
$parser = new SearchParser();
$filters = $parser->parse('string to parse');
will produce, (*6)
[
'search' => 'string&to&parse'
]
Filters
To handle filters you need to extend SearchParser and add a handle function or add a pass through filter:, (*7)
class CustomSearchParser extends SearchParser
{
/**
* The filters that should be returned without handlers.
*
* @var array
*/
protected $passthruFilters = ['other'];
/**
* Handle the state filter.
*
* @param mixed $state
* @return array
*/
protected function handleState($state)
{
return ['some' => $state];
}
}
$parser = new CustomSearchParser();
$filters = $parser->parse('state:pending other:string string to parse');
will produce, (*8)
[
'search' => 'string&to&parse',
'some' => 'pending',
'other' => 'string'
]
Custom Query Key
You can customise the array key of the query by overriding the $queryName
variable in your custom class., (*9)
class CustomSearchParser extends SearchParser
{
/**
* The name of the query in the result array.
*
* @var string
*/
protected $queryName = 'other';
}
$parser = new CustomSearchParser();
$filters = $parser->parse('string to parse');
will produce, (*10)
[
'other' => 'string&to&parse'
]
License
Adaptive View is licensed under The MIT License (MIT)., (*11)