KMP PHP API Reference

ExpressionEvaluator
in package

Safe expression evaluator for workflow templates, date arithmetic, conditionals, and math.

Supports context path resolution, string template interpolation, date arithmetic, ternary conditionals, basic arithmetic, and PHP-style string concatenation. All parsing is regex-based — no eval() or dynamic code execution.

Table of Contents

Methods

evaluate()  : mixed
Evaluate an expression string and return the computed result.
evaluateArithmetic()  : float|int
Evaluate a basic arithmetic expression: +, -, *, /
evaluateConditional()  : mixed
Evaluate a simple ternary conditional expression.
evaluateDateExpression()  : DateTimeInterface|null
Evaluate a date arithmetic expression.
evaluateTemplate()  : string
Interpolate {{$.path}} placeholders in a string template.
evaluateConcatenation()  : string
Evaluate PHP-style string concatenation with dot operator.
evaluateConditionExpression()  : bool
Evaluate a comparison condition (left operator right).
findTernaryColon()  : int|false
Find the colon separating true/false branches in a ternary, skipping colons inside quotes.
looksLikeArithmetic()  : bool
Check if an expression looks like basic arithmetic (not date arithmetic).
looksLikeConcatenation()  : bool
Check if an expression looks like PHP-style string concatenation.
looksLikeDateExpression()  : bool
Check if an expression looks like date arithmetic.
resolveDate()  : DateTimeInterface|null
Resolve a string to a date value.
resolveExpressionValue()  : mixed
Resolve a single value token: context path, quoted string, numeric literal, or bare string.
resolveNumericValue()  : float|int|null
Resolve a value to a numeric type.

Methods

evaluate()

Evaluate an expression string and return the computed result.

public evaluate(string $expression, array<string|int, mixed> $context) : mixed

Detects expression type automatically and delegates to the appropriate evaluator.

Parameters
$expression : string

Raw expression (without leading '=' prefix)

$context : array<string|int, mixed>

Current workflow context

Return values
mixed

Evaluated result

evaluateArithmetic()

Evaluate a basic arithmetic expression: +, -, *, /

public evaluateArithmetic(string $expression, array<string|int, mixed> $context) : float|int
Parameters
$expression : string

Arithmetic expression

$context : array<string|int, mixed>

Current workflow context

Return values
float|int

Computed result

evaluateConditional()

Evaluate a simple ternary conditional expression.

public evaluateConditional(string $expression, array<string|int, mixed> $context) : mixed

Format: "condition ? trueValue : falseValue" Condition supports: ==, !=, >, <, >=, <=

Parameters
$expression : string

Ternary expression

$context : array<string|int, mixed>

Current workflow context

Return values
mixed

Result of the selected branch, or null if not parseable

evaluateDateExpression()

Evaluate a date arithmetic expression.

public evaluateDateExpression(string $expression, array<string|int, mixed> $context) : DateTimeInterface|null

Supports: "now", "now + 30 days", "$.start_on + 1 year", "$.start_on + $.term_length days"

Parameters
$expression : string

Date expression

$context : array<string|int, mixed>

Current workflow context

Return values
DateTimeInterface|null

Resulting date or null if not parseable

evaluateTemplate()

Interpolate {{$.path}} placeholders in a string template.

public evaluateTemplate(string $template, array<string|int, mixed> $context) : string
Parameters
$template : string

Template string with {{$.path}} markers

$context : array<string|int, mixed>

Current workflow context

Return values
string

Interpolated string

evaluateConcatenation()

Evaluate PHP-style string concatenation with dot operator.

private evaluateConcatenation(string $expression, array<string|int, mixed> $context) : string

Format: "$.first_name . ' ' . $.last_name"

Parameters
$expression : string

Concatenation expression

$context : array<string|int, mixed>

Current workflow context

Return values
string

Concatenated result

evaluateConditionExpression()

Evaluate a comparison condition (left operator right).

private evaluateConditionExpression(string $condition, array<string|int, mixed> $context) : bool
Parameters
$condition : string
$context : array<string|int, mixed>
Return values
bool

findTernaryColon()

Find the colon separating true/false branches in a ternary, skipping colons inside quotes.

private findTernaryColon(string $remainder) : int|false
Parameters
$remainder : string
Return values
int|false

looksLikeArithmetic()

Check if an expression looks like basic arithmetic (not date arithmetic).

private looksLikeArithmetic(string $expression) : bool
Parameters
$expression : string
Return values
bool

looksLikeConcatenation()

Check if an expression looks like PHP-style string concatenation.

private looksLikeConcatenation(string $expression) : bool
Parameters
$expression : string
Return values
bool

looksLikeDateExpression()

Check if an expression looks like date arithmetic.

private looksLikeDateExpression(string $expression) : bool
Parameters
$expression : string
Return values
bool

resolveDate()

Resolve a string to a date value.

private resolveDate(string $value, array<string|int, mixed> $context) : DateTimeInterface|null
Parameters
$value : string
$context : array<string|int, mixed>
Return values
DateTimeInterface|null

resolveExpressionValue()

Resolve a single value token: context path, quoted string, numeric literal, or bare string.

private resolveExpressionValue(string $value, array<string|int, mixed> $context) : mixed
Parameters
$value : string
$context : array<string|int, mixed>

resolveNumericValue()

Resolve a value to a numeric type.

private resolveNumericValue(string $value, array<string|int, mixed> $context) : float|int|null
Parameters
$value : string
$context : array<string|int, mixed>
Return values
float|int|null
On this page

Search results