Download the PHP package novabytes/odata-query-parser without Composer
On this page you can find all versions of the php package novabytes/odata-query-parser. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Informations about the package odata-query-parser
OData Query Parser for PHP
A framework-agnostic OData 4 parser for PHP 8.2+. Parses query strings ($filter, $select, $expand, $orderby, $top, $skip, $count) and resource paths (/Products(1)/Category) into immutable AST objects.
Zero runtime dependencies.
Installation
Quick Start
You can also parse individual query options directly:
Supported Query Options
$filter
Full expression language with correct operator precedence:
30+ built-in functions:
Lambda expressions:
All literal types:
$select
$expand
$orderby
$top, $skip, $count
Parsed as part of QueryOptionParser::parse():
Resource Path Parsing
Parse OData resource paths (the URL path portion) into structured AST nodes:
AST Structure
Every parsed result is an immutable (readonly class) AST node. The $filter expression tree uses these node types:
| Node | Description |
|---|---|
BinaryExpression |
left operator right (e.g. Price gt 100, A and B) |
UnaryExpression |
operator operand (e.g. not expr, -5) |
PropertyPath |
Dotted/slashed property reference (e.g. Address/City) |
Literal |
Typed value: null, boolean, integer, decimal, string, GUID, date, etc. |
FunctionCall |
Built-in function with arguments (e.g. contains(Name,'x')) |
LambdaExpression |
collection/any(var:predicate) or collection/all(var:predicate) |
ListExpression |
Parenthesized list for in operator (e.g. ('A','B','C')) |
Visitor Pattern
Implement ExpressionVisitor to transform the AST into whatever you need:
A StringifyVisitor is included for round-tripping AST back to OData syntax:
Error Handling
All parse errors throw NovaBytes\OData\Exception\ParseException with position information:
OData 4 Support
System Query Options
| Query Option | Status | Notes |
|---|---|---|
$filter |
Supported | Full expression language with correct operator precedence |
$select |
Supported | Property paths, wildcards (*), nested options |
$expand |
Supported | Navigation paths, nested query options ($filter, $select, $top, etc.) |
$orderby |
Supported | Expressions with asc/desc, multiple sort keys |
$top |
Supported | |
$skip |
Supported | |
$count |
Supported | Inline count (true/false) |
$search |
Not yet | Planned for a future release |
$compute |
Not yet | Planned for a future release |
$apply |
Not yet | Data aggregation extension, planned for a future release |
$format |
Not yet | |
$index |
Not yet | |
$schemaversion |
Not yet | |
$skiptoken |
Not yet | Opaque server-driven paging token |
$deltatoken |
Not yet | Opaque server-driven delta token |
Filter Operators
| Category | Operators | Status |
|---|---|---|
| Comparison | eq, ne, gt, ge, lt, le |
Supported |
| Logical | and, or, not |
Supported |
| Arithmetic | add, sub, mul, div, divby, mod |
Supported |
| Membership | in |
Supported |
| Enum flags | has |
Supported |
| Grouping | ( ) |
Supported |
| Lambda | any, all |
Supported |
| Negation | - (unary minus) |
Supported |
Filter Functions
| Category | Functions | Status |
|---|---|---|
| String | contains, startswith, endswith, length, indexof, substring, tolower, toupper, trim, concat, matchesPattern |
Supported |
| Date/Time | year, month, day, hour, minute, second, fractionalseconds, totalseconds, date, time, totaloffsetminutes, now, mindatetime, maxdatetime |
Supported |
| Math | round, floor, ceiling |
Supported |
| Geo | geo.distance, geo.length, geo.intersects |
Supported |
| Collection | hassubset, hassubsequence |
Supported |
| Type | cast, isof |
Supported |
Literal Types
| Type | Example | Status |
|---|---|---|
| Null | null |
Supported |
| Boolean | true, false |
Supported |
| Integer | 42, -1 |
Supported |
| Decimal | 3.14, 1.5e10 |
Supported |
| String | 'Milk', 'O''Brien' |
Supported |
| GUID | 01234567-89ab-cdef-0123-456789abcdef |
Supported |
| Date | 2023-01-15 |
Supported |
| DateTimeOffset | 2023-01-15T14:30:00Z |
Supported |
| TimeOfDay | 14:30:00 |
Supported |
| Duration | duration'P1DT2H30M' |
Supported |
| NaN / Infinity | NaN, INF, -INF |
Supported |
| Binary | binary'T0RhdGE=' |
Not yet |
| Enum | Namespace.Color'Red' |
Not yet |
| Geography/Geometry | geography'SRID=0;Point(...)' |
Not yet |
Metadata
Generate OData metadata documents and OpenAPI specifications from entity type definitions.
Entity Types
Define your entity types using the metadata value objects:
EDM Type Resolver
Map database column types to OData EDM types:
CSDL Generation
Generate an OData v4 CSDL XML metadata document. When entity types have CRUD operations, capability annotations (InsertRestrictions, UpdateRestrictions, DeleteRestrictions) are included automatically:
OpenAPI Generation
Generate an OpenAPI 3.0 specification. CRUD operations are reflected as separate paths and schemas:
Design Decisions
- Framework-agnostic -- zero dependencies, works with any PHP 8.2+ project. Use it with Laravel, Symfony, API Platform, Slim, or plain PHP.
- Schema-unaware -- the parser does not validate property names or types against a data model. It parses syntax only. Schema validation belongs in a separate layer.
- Immutable AST -- all nodes are
readonly class, safe to cache and share. - Pratt parser -- the
$filterexpression parser uses top-down operator precedence parsing for correct handling of all 11 precedence levels defined in the OData 4.01 spec.
Requirements
- PHP >= 8.2
License
MIT