eventjet / ausdruck
A small expression engine for PHP
Requires
- php: >=8.2
- ext-ctype: *
Requires (Dev)
- eventjet/coding-standard: ^3.18
- infection/infection: ^0.29.10
- phpstan/extension-installer: ^1.4
- phpstan/phpstan: ^2.1
- phpstan/phpstan-phpunit: ^2.0
- phpstan/phpstan-strict-rules: ^2.0
- phpunit/phpunit: ^11.5
- psalm/plugin-phpunit: ^0.19.2
- vimeo/psalm: ^6.2
- dev-master
- 0.2.x-dev
- 0.2.2
- 0.2.1
- 0.2.0
- 0.1.7
- 0.1.6
- 0.1.5
- 0.1.4
- 0.1.3
- 0.1.2
- 0.1.1
- v0.1.0
- dev-test-files
- dev-value-declaration-statements
- dev-parser-instances
- dev-php8.4
- dev-some-of-t-equals-t
- dev-empty-array-in-struct
- dev-assert-with-empty-array
- dev-get-subtypes
- dev-compare-canonical-types
- dev-refresh
- dev-bytes
- dev-operators-as-functions
- dev-remove-object-type-2
- dev-structs
- dev-remove-object-type
- dev-php-transpiler
- dev-variable-declarations
- dev-track-position
This package is auto-updated.
Last update: 2025-02-06 19:43:53 UTC
README
A small expression engine for PHP.
Quick start
composer require eventjet/ausdruck
use Eventjet\Ausdruck\Parser\ExpressionParser; use Eventjet\Ausdruck\Parser\Types; class Person { public function __construct(public string $name) {} } $expression = ExpressionParser::parse( 'joe:MyPersonType.name:string()', new Types(['MyPersonType' => Person::class]), ); $scope = new Scope( // Passing values to the expression ['joe' => new Person('Joe')], // Custom function definitions ['name' => static fn (Person $person): string => $person->name], ); $name = $expression->evaluate($scope); assert($name === 'Joe');
Documentation
Accessing scope variables
Syntax: varName:type
Scope variables are passed from PHP when it calls evaluate()
on the expression:
use Eventjet\Ausdruck\Parser\ExpressionParser; use Eventjet\Ausdruck\Scope; $x = ExpressionParser::parse('foo:int') ->evaluate(new Scope(['foo' => 123])); assert($x === 123);
Examples
foo:int
, foo:list<string>
See Types
Literals
123
: Integer"foo"
: String1.23
: Float[1, myInt:int, 3]
: List of integers["foo", myString:string, "bar"]
: List of strings
Operators
Both operands must be of the same type.
Where's the rest? We're implementing more as we need them.
Types
The following types are supported:
int
: Integerstring
: Stringbool
: Booleanfloat
: Floating point numberlist<T>
: List of type Tmap<K, V>
: Map with key type K and value type V- Any other type will be treated as an alias that you will have to provide when parsing the expression:
use Eventjet\Ausdruck\Parser\ExpressionParser; use Eventjet\Ausdruck\Type; ExpressionParser::parse('foo:MyType', ['MyType' => Type::object(Foo::class)]);
Functions
Syntax: target.functionName:returnType(arg1, arg2, ...)
The target can be any expression. It will be passed as the first argument to the function.
Example
haystack:list<string>.contains:bool(needle:string)
Built-In Functions
Custom Functions
You can pass custom functions along with the scope variables:
use Eventjet\Ausdruck\Parser\ExpressionParser;use Eventjet\Ausdruck\Scope; $scope = new Scope( ['foo' => 'My secret'], ['mask' => fn (string $str, string $mask) => str_repeat($mask, strlen($str))] ); $result = ExpressionParser::parse('foo:string.mask("x")')->evaluate($scope); assert($result === 'xxxxxxxxx');
The target of the function/method call (foo:string
in the example above) will be passed as the first argument to the
function.
Lambdas
Syntax: |arg1, arg2, ... | expression
To access an argument, you must specify its type, just like when accessing scope variables.
Example
|item| item:int > 5