CEL-PHP comes with several built-in extensions that provide functions and operators for common operations.
The Core extension provides fundamental operators and type conversion functions.
1 + 2 // Addition: 3
5 - 3 // Subtraction: 2
4 * 3 // Multiplication: 12
10 / 2 // Division: 5
10 % 3 // Modulo: 11 < 2 // Less than: true
1 <= 1 // Less than or equal: true
2 > 1 // Greater than: true
2 >= 2 // Greater than or equal: true1 == 1 // Equal: true
1 != 2 // Not equal: truetrue && false // Logical AND: false
true || false // Logical OR: true
!true // Logical NOT: false'a' in ['a', 'b', 'c'] // true
'key' in {'key': 'value'} // trueint('42') // 42
int(3.14) // 3
int(b'42') // 42uint('42') // 42u
uint(42) // 42u
uint(b'42') // 42udouble('3.14') // 3.14
double(3) // 3.0string(42) // '42'
string(true) // 'true'bytes('hello') // b'hello'bool('true') // true
bool(0) // false
bool(1) // trueReturns a first-class type value. Bare type names (int, uint, double,
bool, string, bytes, list, map, null_type, type) denote the same
values, so a type can be compared against a literal type name.
type(42) // the `int` type value
type(42) == int // true
type('hello') == string // true
type(type(42)) // the `type` type valueReturns its argument unchanged; a hint for the type checker with no runtime effect.
dyn(42) // 42size('hello') // 5
size([1, 2, 3])// 3
size({'a': 1}) // 1The String extension provides functions for string manipulation.
'hello world'.contains('world') // true
'hello'.contains('bye') // false'hello world'.startsWith('hello') // true
'hello'.startsWith('world') // false'hello world'.endsWith('world') // true
'hello'.endsWith('hello') // trueReturns true when the regular expression matches anywhere in the string (unanchored). An invalid pattern raises an evaluation error.
'hubba'.matches('ubb') // true
'grey'.matches('gr(a|e)y') // true
matches('mañana', 'a+ñ+a+') // true'hello world'.indexOf('world') // 6
'hello world'.indexOf('world', 7) // -1 (not found after index 7)'hello world hello'.lastIndexOf('hello') // 12'hello world'.replace('world', 'CEL') // 'hello CEL''a,b,c'.split(',') // ['a', 'b', 'c']
'hello'.split('') // ['h', 'e', 'l', 'l', 'o']['a', 'b', 'c'].join(',') // 'a,b,c'
['a', 'b'].join() // 'ab''HELLO'.toLowerCase() // 'hello'
'HELLO'.toAsciiLower() // 'hello''hello'.toUpperCase() // 'HELLO'
'hello'.toAsciiUpper() // 'HELLO'' hello '.trim() // 'hello'
' hello'.trimLeft() // 'hello'
'hello '.trimRight() // 'hello'
// With custom characters
'***hello***'.trim('*') // 'hello'The List extension provides functions for list manipulation.
[1, 2, 3, 4, 5].chunk(2) // [[1, 2], [3, 4], [5]][1, 2, 3].contains(2) // true
[1, 2, 3].contains(4) // false[[1, 2], [3, 4]].flatten() // [1, 2, 3, 4][1, 2, 3].reverse() // [3, 2, 1][3, 1, 2].sort() // [1, 2, 3]The DateTime extension provides functions for working with timestamps and durations.
timestamp('2024-01-01T00:00:00Z') // Timestamp value
timestamp(1704067200) // From Unix timestampduration('1h30m') // 1 hour 30 minutes
duration('5s') // 5 seconds
duration('100ms') // 100 millisecondstimestamp('2024-01-15T10:30:45Z').getFullYear() // 2024
timestamp('2024-01-15T10:30:45Z').getMonth() // 0 (0-indexed)
timestamp('2024-01-15T10:30:45Z').getDayOfMonth() // 14 (0-indexed)
timestamp('2024-01-15T10:30:45Z').getDayOfWeek() // 1 (Monday, 0-indexed)
timestamp('2024-01-15T10:30:45Z').getDayOfYear() // 14 (0-indexed)
timestamp('2024-01-15T10:30:45Z').getHours() // 10
timestamp('2024-01-15T10:30:45Z').getMinutes() // 30
timestamp('2024-01-15T10:30:45Z').getSeconds() // 45
timestamp('2024-01-15T10:30:45Z').getMilliseconds() // 0now() // Current timestampThe Math extension provides mathematical functions.
min([1, 2, 3, 4, 5]) // 1
max([1, 2, 3, 4, 5]) // 5sum([1, 2, 3, 4, 5]) // 15mean([1, 2, 3, 4, 5]) // 3.0median([1, 2, 3, 4, 5]) // 3
median([1, 2, 3, 4]) // 2.5clamp(5, 0, 10) // 5
clamp(15, 0, 10) // 10
clamp(-5, 0, 10) // 0toBase(255, 16) // 'ff'
toBase(255, 2) // '11111111'
fromBase('ff', 16) // 255
fromBase('11111111', 2) // 255
baseConvert('ff', 16, 10) // '255'Extensions are enabled by default. You can control which extensions are available:
use Cel;
// Disable standard extensions
$configuration = new Cel\Runtime\Configuration(
enableStandardExtensions: false
);
$runtime = new Cel\Runtime\Runtime(configuration: $configuration);
// Register only specific extensions
$runtime->register(new Cel\Extension\Core\CoreExtension());
$runtime->register(new Cel\Extension\String\StringExtension());
$cel = new Cel\CommonExpressionLanguage(runtime: $runtime);- Learn how to create Custom Functions
- Learn how to create Custom Operators
- Learn about Value Resolvers