1. Go to this page and download the library: Download oxygensuite/php-ast library. Choose the download type require.
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
// Single-level: lines.*.quantity → PLUCK(lines; "quantity")
$evaluator->evaluate('lines.*.quantity', $data); // [2, 3, 1]
// Multi-level: lines.*.taxes.*.amount → extracts all tax amounts from all lines
$evaluator->evaluate('SUM(lines.*.taxes.*.amount)', $data);
// Sum of (quantity * unit_price) per line
$evaluator->evaluate('SUM(MAP(lines; quantity * unit_price))', $data);
// Filter then sum
$evaluator->evaluate('SUM(PLUCK(FILTER(lines; tax_category == 1); "net_amount"))', $data);
// Grouped aggregation
$evaluator->evaluate('GROUP(lines; "tax_category")', $data);
// → [1 => [...], 2 => [...], 3 => [...]]
// Arithmetic on aggregated results
$evaluator->evaluate('SUM(lines.*.net_amount) + SUM(lines.*.vat_amount)', $data);
use OxygenSuite\PhpAst\ContextProvider\ContextProvider;
class MyContext implements ContextProvider
{
public function getContextValue(string $key): mixed
{
return match ($key) {
'tax_rate' => 0.24,
'currency' => 'EUR',
default => null,
};
}
}
$evaluator = new FormulaEvaluator(new MyContext());
$evaluator->evaluate('unit_price * tax_rate', $data); // uses tax_rate from context
use OxygenSuite\PhpAst\AST\ASTEvaluator;
use OxygenSuite\PhpAst\Formulas\Formula;
readonly class DoubleFunction implements Formula
{
public function execute(array $arguments, array $data, ASTEvaluator $evaluator): float|int|string|array
{
return ($arguments[0] ?? 0) * 2;
}
}
use OxygenSuite\PhpAst\AST\ASTEvaluator;
use OxygenSuite\PhpAst\Formulas\ASTFormula;
readonly class MyTransform implements ASTFormula
{
public function execute(array $astNodes, array $data, ASTEvaluator $evaluator): mixed
{
[$itemsNode, $callbackNode] = $astNodes;
$items = $itemsNode->accept($evaluator, $data);
return array_map(
fn($item) => $callbackNode->accept($evaluator, $item),
$items,
);
}
}