This guide will help you get started with CEL-PHP, a PHP implementation of Google's Common Expression Language.
Install CEL-PHP via Composer:
composer require carthage-software/cel-phpThe simplest way to use CEL-PHP is through the convenience function:
use Cel;
// Simple expression evaluation
$result = Cel\evaluate('1 + 2');
echo $result->getRawValue(); // Output: 3You can provide variables to your expressions:
use Cel;
$result = Cel\evaluate('user.age >= 18', [
'user' => [
'age' => 25,
'name' => 'John',
],
]);
echo $result->getRawValue(); // Output: trueFor more control, you can use the full API:
use Cel;
// Create a CEL instance
$cel = new Cel\CommonExpressionLanguage();
// Parse the expression
$expression = $cel->parseString('account.balance >= transaction.amount');
// Evaluate with context
$receipt = $cel->run($expression, [
'account' => ['balance' => 1000],
'transaction' => ['amount' => 500],
]);
echo $receipt->result->getRawValue(); // Output: trueCEL supports various data types:
// Integers
$result = Cel\evaluate('42');
// Floating point
$result = Cel\evaluate('3.14');
// Unsigned integers
$result = Cel\evaluate('42u');$result = Cel\evaluate('"Hello, " + name', ['name' => 'World']);
echo $result->getRawValue(); // Output: Hello, World$result = Cel\evaluate('true && false');
echo $result->getRawValue(); // Output: false$result = Cel\evaluate('[1, 2, 3].size()');
echo $result->getRawValue(); // Output: 3
$result = Cel\evaluate('items[0]', ['items' => ['a', 'b', 'c']]);
echo $result->getRawValue(); // Output: a$result = Cel\evaluate('{"key": "value"}["key"]');
echo $result->getRawValue(); // Output: value
$result = Cel\evaluate('user.name', ['user' => ['name' => 'Alice']]);
echo $result->getRawValue(); // Output: Alice// Duration
$result = Cel\evaluate('duration("1h30m")');
// Timestamp
$result = Cel\evaluate('timestamp("2024-01-01T00:00:00Z")');CEL-PHP supports powerful macros for working with collections:
$result = Cel\evaluate(
'has(user.email)',
['user' => ['name' => 'John']]
);
echo $result->getRawValue(); // Output: false$result = Cel\evaluate(
'numbers.all(n, n > 0)',
['numbers' => [1, 2, 3, 4, 5]]
);
echo $result->getRawValue(); // Output: true$result = Cel\evaluate(
'numbers.exists(n, n > 10)',
['numbers' => [5, 10, 15, 20]]
);
echo $result->getRawValue(); // Output: true$result = Cel\evaluate(
'numbers.exists_one(n, n == 10)',
['numbers' => [5, 10, 15, 20]]
);
echo $result->getRawValue(); // Output: true$result = Cel\evaluate(
'numbers.filter(n, n > 10)',
['numbers' => [5, 10, 15, 20, 25]]
);
// Output: [15, 20, 25]$result = Cel\evaluate(
'numbers.map(n, n * 2)',
['numbers' => [1, 2, 3, 4, 5]]
);
// Output: [2, 4, 6, 8, 10]CEL-PHP throws specific exceptions for different error conditions:
use Cel;
use Cel\Parser\Exception\UnexpectedTokenException;
use Cel\Parser\Exception\UnexpectedEndOfFileException;
use Cel\Exception\EvaluationException;
use Cel\Exception\IncompatibleValueTypeException;
try {
$result = Cel\evaluate('invalid syntax here');
} catch (UnexpectedTokenException | UnexpectedEndOfFileException $e) {
// Handle parsing errors
echo "Parse error: " . $e->getMessage();
} catch (IncompatibleValueTypeException $e) {
// Handle type conversion errors
echo "Type error: " . $e->getMessage();
} catch (EvaluationException $e) {
// Handle runtime evaluation errors
echo "Evaluation error: " . $e->getMessage();
}You can customize the CEL runtime behavior:
use Cel;
// Create a custom configuration
$configuration = new Cel\Runtime\Configuration(
enableMacros: true, // Enable macros (default: true)
enableStandardExtensions: true, // Enable standard extensions (default: true)
allowedMessageClasses: [], // Restrict message types for security
messageClassAliases: [], // Define custom type aliases
enforceMessageClassAliases: false, // Enforce alias usage
);
// Create runtime with custom configuration
$runtime = new Cel\Runtime\Runtime(configuration: $configuration);
$cel = new Cel\CommonExpressionLanguage(runtime: $runtime);
// Use it
$expression = $cel->parseString('1 + 2');
$receipt = $cel->run($expression);- Learn about Extensions - Available functions and operators
- Learn about Caching - Improve performance with caching
- Learn about Custom Functions - Extend CEL with your own functions
- Learn about Custom Operators - Add custom operators
- Learn about Value Resolvers - Support custom types