Documentation

Expression
in package

Predicate expression parser class

Tags
category

Pop

author

Nick Sagona, III nick@popphp.org

copyright

Copyright (c) 2009-2026 Nick Sagona, III

license

https://www.popphp.org/license New BSD License

version
7.0.0

Table of Contents

Properties

$operators  : array<string|int, mixed>
Allowed operators

Methods

convertExpressionsToShorthand()  : array<string|int, mixed>
Convert to expression to shorthand value
convertExpressionToShorthand()  : array<string|int, mixed>
Convert to expression to shorthand value
isShorthand()  : bool
Method to check if the column is shorthand
parse()  : array<string|int, mixed>
Method to parse a predicate string expression into its components
parseExpressions()  : array<string|int, mixed>
Method to parse predicate string expressions into its components
parseShorthand()  : array<string|int, mixed>
Method to parse the shorthand columns to create expressions and their parameters
prepareExpression()  : array<string|int, mixed>
Prepare a basic expression as a direct prepared predicate clause
prepareExpressions()  : array<string|int, mixed>
Prepare basic expressions as direct prepared predicate clauses
quote()  : string
Quote the value (if it is not a numeric value)
stripIdQuotes()  : string
Strip ID quotes
stripQuotes()  : string
Strip quotes
getColumnEnd()  : int|false
Locate the end of the column at the start of a predicate expression
unwrapExpression()  : string
Strip one layer of enclosing parentheses from a predicate expression
applyExpression()  : array<string|int, mixed>
Add a rendered expression fragment to the expressions array, keyed by column name when using named (':') placeholders, or appended positionally otherwise
applyParam()  : array<string|int, mixed>
Add a bound parameter value to the params array, keyed by column name when using named (':') placeholders, or by the running column index otherwise
buildBetweenExpression()  : array<string|int, mixed>
Build a BETWEEN/NOT BETWEEN expression fragment and its two bound boundary values
buildComparisonExpression()  : array<string|int, mixed>
Build a LIKE/NOT LIKE or standard comparison-operator expression fragment and its bound value
buildInExpression()  : array<string|int, mixed>
Build an IN/NOT IN expression fragment and its bound parameter value(s)
buildNullExpression()  : string
Build an IS NULL/IS NOT NULL expression fragment

Properties

$operators

Allowed operators

protected static array<string|int, mixed> $operators = ['>=', '<=', '!=', '=', '>', '<', 'NOT LIKE', 'LIKE', 'NOT BETWEEN', 'BETWEEN', 'NOT IN', 'IN', 'IS NOT NULL', 'IS NULL']

Methods

convertExpressionsToShorthand()

Convert to expression to shorthand value

public static convertExpressionsToShorthand(array<string|int, mixed> $expressions) : array<string|int, mixed>
Parameters
$expressions : array<string|int, mixed>
Return values
array<string|int, mixed>

convertExpressionToShorthand()

Convert to expression to shorthand value

public static convertExpressionToShorthand(string $expression) : array<string|int, mixed>
Parameters
$expression : string
Return values
array<string|int, mixed>

isShorthand()

Method to check if the column is shorthand

public static isShorthand(string $column) : bool
Parameters
$column : string
Return values
bool

parse()

Method to parse a predicate string expression into its components

public static parse(string $expression) : array<string|int, mixed>
Parameters
$expression : string
Tags
throws
Exception
Return values
array<string|int, mixed>

parseExpressions()

Method to parse predicate string expressions into its components

public static parseExpressions(array<string|int, mixed> $expressions) : array<string|int, mixed>
Parameters
$expressions : array<string|int, mixed>
Return values
array<string|int, mixed>

parseShorthand()

Method to parse the shorthand columns to create expressions and their parameters

public static parseShorthand(array<string|int, mixed> $columns[, string|null $placeholder = null ][, bool $flatten = true ]) : array<string|int, mixed>
Parameters
$columns : array<string|int, mixed>
$placeholder : string|null = null
$flatten : bool = true
Return values
array<string|int, mixed>

prepareExpression()

Prepare a basic expression as a direct prepared predicate clause

public static prepareExpression(string $expression[, AbstractSql|null $sql = null ][, bool $withParams = true ]) : array<string|int, mixed>
Parameters
$expression : string
$sql : AbstractSql|null = null
$withParams : bool = true
Return values
array<string|int, mixed>

prepareExpressions()

Prepare basic expressions as direct prepared predicate clauses

public static prepareExpressions(array<string|int, mixed> $expressions[, AbstractSql|null $sql = null ][, bool $withParams = true ][, bool $flatten = true ]) : array<string|int, mixed>
Parameters
$expressions : array<string|int, mixed>
$sql : AbstractSql|null = null
$withParams : bool = true
$flatten : bool = true
Return values
array<string|int, mixed>

quote()

Quote the value (if it is not a numeric value)

public static quote(string $value) : string
Parameters
$value : string
Return values
string

stripIdQuotes()

Strip ID quotes

public static stripIdQuotes(string $identifier) : string
Parameters
$identifier : string
Return values
string

stripQuotes()

Strip quotes

public static stripQuotes(string $value) : string
Parameters
$value : string
Return values
string

getColumnEnd()

Locate the end of the column at the start of a predicate expression

protected static getColumnEnd(string $expression) : int|false

The column runs up to the first whitespace that is not inside parentheses and not inside a quoted string, so that the arguments of a function call are kept with the column instead of being read as the operator - 'COUNT(DISTINCT id) > 1' has to yield the column 'COUNT(DISTINCT id)', not 'COUNT(DISTINCT' and the operator 'id)'.

Parameters
$expression : string
Return values
int|false

unwrapExpression()

Strip one layer of enclosing parentheses from a predicate expression

protected static unwrapExpression(string $expression) : string

Only strips when the opening parenthesis is matched by the very last character, so that '(id = 1)' is unwrapped while '(a = 1) AND (b = 2)' and 'COUNT(*) > 1' are left alone.

Parameters
$expression : string
Return values
string

applyExpression()

Add a rendered expression fragment to the expressions array, keyed by column name when using named (':') placeholders, or appended positionally otherwise

private static applyExpression(array<string|int, mixed> $expressions, string $parsedColumn, string|null $placeholder, string $newExpression) : array<string|int, mixed>
Parameters
$expressions : array<string|int, mixed>
$parsedColumn : string
$placeholder : string|null
$newExpression : string
Return values
array<string|int, mixed>

applyParam()

Add a bound parameter value to the params array, keyed by column name when using named (':') placeholders, or by the running column index otherwise

private static applyParam(array<string|int, mixed> $params, string $parsedColumn, string|null $placeholder, int $j, mixed $paramValue) : array<string|int, mixed>
Parameters
$params : array<string|int, mixed>
$parsedColumn : string
$placeholder : string|null
$j : int
$paramValue : mixed
Return values
array<string|int, mixed>

buildBetweenExpression()

Build a BETWEEN/NOT BETWEEN expression fragment and its two bound boundary values

private static buildBetweenExpression(string $parsedColumn, string $operator, string $value, string|null $placeholder, string|null $pHolder, int &$i) : array<string|int, mixed>
Parameters
$parsedColumn : string
$operator : string
$value : string
$placeholder : string|null
$pHolder : string|null
$i : int
Return values
array<string|int, mixed>

[string $newExpression, array $paramValue]

buildComparisonExpression()

Build a LIKE/NOT LIKE or standard comparison-operator expression fragment and its bound value

private static buildComparisonExpression(string $column, string $parsedColumn, string $operator, mixed $value, string|null $placeholder, string|null $pHolder, int &$i) : array<string|int, mixed>
Parameters
$column : string
$parsedColumn : string
$operator : string
$value : mixed
$placeholder : string|null
$pHolder : string|null
$i : int
Return values
array<string|int, mixed>

[string $newExpression, mixed $paramValue]

buildInExpression()

Build an IN/NOT IN expression fragment and its bound parameter value(s)

private static buildInExpression(string $parsedColumn, string $operator, array<string|int, mixed> $value, string|null $placeholder, string|null $pHolder, int &$i) : array<string|int, mixed>
Parameters
$parsedColumn : string
$operator : string
$value : array<string|int, mixed>
$placeholder : string|null
$pHolder : string|null
$i : int
Return values
array<string|int, mixed>

[string $newExpression, array $paramValue]

buildNullExpression()

Build an IS NULL/IS NOT NULL expression fragment

private static buildNullExpression(string $parsedColumn, string $operator) : string
Parameters
$parsedColumn : string
$operator : string
Return values
string

        
On this page

Search results