Expression Builder
Usage
Expression builder for Cloudflare Rules language.
php
use Cloudflare\ExpressionBuilder;
$expression = (new ExpressionBuilder())
->field('ip.src')
->eq('127.0.0.1')
->build();
echo $expression;
// ip.src eq 127.0.0.1
Grouping
The Rules language supports parentheses ((
,)
) as grouping symbols. Grouping symbols allow you to organize expressions, enforce precedence, and nest expressions.
php
use Cloudflare\ExpressionBuilder;
$expression = (new ExpressionBuilder())
->field('ip.src')
->eq('127.0.0.1')
->or()
->group(function(ExpressionBuilder $builder){
$builder->not()->field('ssl')->or()->field('ip.src.country')->eq('GB');
})
->build();
echo $expression;
// ip.src eq 127.0.0.1 or (not ssl or ip.src.country eq "GB")
Functions
The Cloudflare Rules language provides functions for manipulating and validating values in an expression.
php
use Cloudflare\ExpressionBuilder;
$expression = (new ExpressionBuilder())
->addFunction('lower', 'ip.src.country')
->eq('127.0.0.1')
->build();
echo $expression;
// lower(ip.src.country) eq "gb"
Raw expressions
php
use Cloudflare\ExpressionBuilder;
$expression = (new ExpressionBuilder())
->addExpression('ip.src.country', 'eq', 'GB')
->build();
echo $expression;
// ip.src.country eq "GB"
ExpressionBuilder
implements Stringable
so you can simply do:
php
use Cloudflare\ExpressionBuilder;
$expression = (string) (new ExpressionBuilder())
->addExpression('ip.src.country', 'eq', 'GB');
echo $expression;
// ip.src.country eq "GB"
Table of Contents