Download the PHP package oxygensuite/php-ast without Composer
On this page you can find all versions of the php package oxygensuite/php-ast. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download oxygensuite/php-ast
More information about oxygensuite/php-ast
Files in oxygensuite/php-ast
Package php-ast
Short Description A lightweight Abstract Syntax Tree (AST) implementation for PHP, enabling structured parsing, analysis, and transformation of PHP code.
License MIT
Informations about the package php-ast
oxygensuite/php-ast
A lightweight formula engine for PHP built on an Abstract Syntax Tree (AST). Parse, evaluate, and transform expressions with support for variables, functions, wildcards, and operator precedence.
Requirements
- PHP 8.4+
- ext-ctype
- ext-mbstring
Installation
Quick Start
Variables
Variables use dot-notation for nested property access. Types are preserved (string, int, float, bool, null, array).
| Syntax | Resolves to |
|---|---|
uid |
$data['uid'] |
lines.0.quantity |
$data['lines'][0]['quantity'] |
a.b.c |
$data['a']['b']['c'] |
Variables can be used directly in expressions:
Wildcards
The * wildcard extracts values across all items in an array.
Operators
Listed from highest to lowest precedence:
| Precedence | Operators | Description |
|---|---|---|
| 1 | ** |
Exponentiation |
| 2 | * / % |
Multiplication, division, modulo |
| 3 | + - |
Addition, subtraction |
| 4 | == != < > <= >= <=> |
Comparison |
| 5 | && |
Logical AND |
| 6 | \|\| |
Logical OR |
| 7 | ?? ?: |
Null coalescing, elvis |
Parentheses override precedence: (a + b) * c.
Functions
All functions use semicolons (;) as argument separators.
Aggregation
| Function | Description | Example |
|---|---|---|
SUM(values) |
Sum of numeric values | SUM(lines.*.quantity) |
AVG(values) |
Average | AVG(lines.*.unit_price) |
MIN(values) |
Minimum value | MIN(lines.*.unit_price) |
MAX(values) |
Maximum value | MAX(lines.*.quantity) |
COUNT(values) |
Number of elements | COUNT(lines.*.quantity) |
Rounding
| Function | Description | Example |
|---|---|---|
ROUND(value; precision) |
Round to precision decimals | ROUND(10.567; 2) → 10.57 |
CEIL(value) |
Round up | CEIL(10.1) → 11 |
FLOOR(value) |
Round down | FLOOR(10.9) → 10 |
Array Manipulation
| Function | Description | Example |
|---|---|---|
PLUCK(array; "key") |
Extract column from array of objects | PLUCK(lines; "quantity") |
FLAT(array; depth) |
Flatten nested arrays (default depth: 1) | FLAT([[1,2],[3,[4,5]]]; 2) |
MAP(array; expr) |
Apply expression per item | MAP(lines; quantity * unit_price) |
FILTER(array; condition) |
Filter items by condition | FILTER(lines; unit_price > 10) |
GROUP(array; "key") |
Group items by key | GROUP(lines; "tax_category") |
SORT(array) |
Sort array | SORT(lines.*.quantity) |
JOIN(array; separator) |
Join into string | JOIN(lines.*.quantity; ",") |
FIRST(array) |
First element | FIRST(lines) |
LAST(array) |
Last element | LAST(lines) |
Conditional
| Function | Description | Example |
|---|---|---|
IF(cond; true_val; false_val) |
Conditional | IF(quantity > 10; "bulk"; "single") |
VALUE(array; "key") |
Get value by key | VALUE(line; "quantity") |
Composing Functions
Functions can be nested and combined with arithmetic:
Context Providers
For injecting external values (e.g., configuration, session data) into formula evaluation:
Context values take priority over data array values.
Custom Functions
Simple Function
Implement the Formula interface:
AST-Aware Function
For functions that need per-item evaluation (like MAP/FILTER), implement ASTFormula instead. It receives raw AST nodes so you can evaluate expressions against each item:
Caching
Parsed ASTs are automatically cached per evaluator instance (default: 1000 entries). Repeated evaluation of the same formula string skips parsing entirely.
Error Handling
| Scenario | Behavior |
|---|---|
| Missing variable | Returns null |
| Division by zero | Returns 0 |
| Unknown function | Throws RuntimeException |
| Invalid expression | Throws RuntimeException |
| Recursion depth > 32 | Throws RuntimeException |
License
MIT
All versions of php-ast with dependencies
ext-ctype Version *
ext-mbstring Version *