Download the PHP package technically/search-query without Composer
On this page you can find all versions of the php package technically/search-query. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download technically/search-query
More information about technically/search-query
Files in technically/search-query
Package search-query
Short Description Parse plaintext search queries into easy-to-use structures
License MIT
Informations about the package search-query
Technically Search Query
🔍 Parse plaintext search queries into easy-to-use filter structures.
This library takes a human-typed search query string and parses it into a structured Query object containing typed filters (KeywordFilter, FieldFilter). It supports quoted strings, negation, comparison operators, and field-based filtering.
Installation
Requirements:
- PHP 8.4+
Quick Start
Supported Query Syntax
| Syntax | Parsed As |
|---|---|
hello |
KeywordFilter('hello') |
"hello world" |
KeywordFilter('hello world', quoted: true) |
-hello |
KeywordFilter('hello', exclude: true) |
tag:php |
FieldFilter('tag', ':', 'php') |
-tag:php |
FieldFilter('tag', ':', 'php', exclude: true) |
year>2020 |
FieldFilter('year', '>', '2020') |
year>=2020 |
FieldFilter('year', '>=', '2020') |
year<2020 |
FieldFilter('year', '<', '2020') |
year<=2020 |
FieldFilter('year', '<=', '2020') |
hello\ world |
KeywordFilter('hello world') (escaped whitespace) |
"custom field":value |
FieldFilter('custom field', ':', 'value') |
Negation
A leading - (minus) before a keyword or field filter negates it. Multiple minuses are gracefully collapsed into a single negation.
Quoting
Double quotes group multiple words into a single token. Quotes can be escaped with \.
Escaping
The backslash \ escape character works both inside and outside quoted strings:
The Tolerant Reader
The parser is built using the Tolerant Reader design pattern — to be forgiving with malformed input. It never throws.
API Reference
QueryParser
The main entry point for parsing query strings.
The parser accepts an optional Tokenizer instance in its constructor. By default, it uses QueryTokenizer.
Methods
parse(string $query): Query— Parses a query string into aQueryobject.
Query
An immutable value object representing the parsed search query.
Properties
public readonly array $filters— Array ofFilterinstances.
Methods
static empty(): self— Create a new empty query.isEmpty(): bool— Check if the query is empty (has no filters).toString(): string— Serializes the query back to the search query syntax string.
Filters
All filters implement the Technically\SearchQuery\Filters\Filter marker interface.
KeywordFilter
Represents a free-text keyword search term.
Properties:
public readonly string $keyword— The keyword value.public readonly bool $quoted— Whether the keyword was originally quoted.public readonly bool $exclude— Whether the keyword is negated.
Methods:
unquote(): self— Returns a new instance withquotedset tofalse.toString(): string— Serializes the filter back to query syntax.
FieldFilter
Represents a field-based filter (field:operator:value).
Properties:
public readonly string $field— The field name.public readonly FilterOperator $operator— The comparison operator.public readonly string $value— The filter value.public readonly bool $quoted— Whether the value was originally quoted.public readonly bool $exclude— Whether the filter is negated.
Methods:
matches(...): bool— Check whether the filter matches the given properties.unquote(): self— Returns a new instance withquotedset tofalse.toString(): string— Serializes the filter back to query syntax.
Examples
Parse a complex query
Serialize filters back to strings
Custom tokenization
Running Tests
Tests are written with Pest PHP.
License
MIT
Credits
Implemented by :space_invader: Ivan Voskoboinyk.
All versions of search-query with dependencies
ext-json Version *