PdbParam
in package
implements
ArrayableInterface, JsonSerializable, JsonDeserializable
Parameter parser.
This parses a form of query condition that doesn't include the target column.
The parser accepts strings, arrays, and operators from PdbSimpleCondition and PdbCompoundCondition.
The aim to provide a natural language for writing conditions without specifying the target column. This column is then later inserted when converting into a full condition object.
Usage:
$param = PdbParam::parse('> 20');
$condition = $param->toCondition('id');
[$where, $params] = $condition->build();
// For most use-cases, use the prepare() shorthand:
$query = new PdbQuery($pdb);
$query->where(PdbParam::prepare('id', '> 20'));
$query->one();
Examples:
| value | sql |
|---|---|
> 20 |
id > ? |
not null |
id IS NOT NULL |
1, 2, 3 |
id IN (?, ?, ?) |
not in 1, 2, 3 |
id NOT IN (?, ?, ?) |
< 10, > 20 |
id < ? OR id > ? |
and >= 10, <= 20 |
id >= ? AND id <= ? |
not 10, 20 |
NOT (id = ? AND id = ?) |
begins abc |
id LIKE CONCAT(?, '%') |
between 1, 2 |
id BETWEEN ? AND ? |
The parser supports relative dates:
PdbParam::prepare('dateCreated', '> today', ['relativeDates']);
// => WHERE dateCreated > ?
// => [new DateTime('today')]
Table of Contents
Interfaces
- ArrayableInterface
- This object can be explicitly converted to an array with a `toArray()` method.
- JsonSerializable
- JsonDeserializable
- An object that can be deserialized from a JSON array.
Constants
- OPERATORS_COMPOUND : mixed = \karmabunny\pdb\Models\PdbCompoundCondition::OP...
- OPERATORS_PREFIX : mixed = [...self::OPERATORS_COMPOUND, \karmabunny\pdb\M...
- OPERATORS_SIMPLE : mixed = \karmabunny\pdb\Models\PdbSimpleCondition::OPER...
Properties
Methods
- __construct() : void
- build() : PdbParam
- Build a new param.
- buildPattern() : string
- Build a prefix pattern for a list of operators.
- fromJson() : static
- Create an object from a JSON array.
- isCompoundOperator() : bool
- Is this a compound operator?
- isSimpleOperator() : bool
- Is this a simple operator?
- jsonSerialize() : array<string|int, mixed>
- parse() : self
- Parse a value into a param.
- parseArray() : self
- Parse an array of values.
- parseExpression() : null|self
- Parse a simple condition, typically nested.
- parseString() : self
- Parse a string expression.
- prepare() : PdbConditionInterface
- Convert a value/sketch into a condition.
- processValues() : void
- Process values in an array.
- split() : array<string|int, mixed>
- Split a string into an array of values.
- toArray() : array<string|int, mixed>
- Convert this object to an array.
- toCondition() : PdbConditionInterface
- Convert a param into a condition for a specified column name.
Constants
OPERATORS_COMPOUND
public
mixed
OPERATORS_COMPOUND
= \karmabunny\pdb\Models\PdbCompoundCondition::OPERATORS
OPERATORS_PREFIX
public
mixed
OPERATORS_PREFIX
= [...self::OPERATORS_COMPOUND, \karmabunny\pdb\Models\PdbSimpleCondition::IN, \karmabunny\pdb\Models\PdbSimpleCondition::NOT_IN, \karmabunny\pdb\Models\PdbSimpleCondition::BETWEEN]
OPERATORS_SIMPLE
public
mixed
OPERATORS_SIMPLE
= \karmabunny\pdb\Models\PdbSimpleCondition::OPERATORS
Properties
$operator
public
string
$operator
$values
public
array<string|int, mixed>
$values
Methods
__construct()
public
__construct(string $operator, array<string|int, mixed> $values) : void
Parameters
- $operator : string
- $values : array<string|int, mixed>
build()
Build a new param.
public
static build(string $operator, array<string|int, mixed> $values[, array<string|int, mixed> $config = [] ]) : PdbParam
Parameters
- $operator : string
- $values : array<string|int, mixed>
- $config : array<string|int, mixed> = []
Return values
PdbParambuildPattern()
Build a prefix pattern for a list of operators.
public
static buildPattern(array<string|int, mixed> $operators) : string
Parameters
- $operators : array<string|int, mixed>
Return values
stringfromJson()
Create an object from a JSON array.
public
static fromJson(mixed $value) : static
Parameters
- $value : mixed
Tags
Return values
staticisCompoundOperator()
Is this a compound operator?
public
static isCompoundOperator(mixed $operator) : bool
Parameters
- $operator : mixed
Return values
boolisSimpleOperator()
Is this a simple operator?
public
static isSimpleOperator(mixed $operator) : bool
Parameters
- $operator : mixed
Return values
booljsonSerialize()
public
jsonSerialize() : array<string|int, mixed>
Tags
Return values
array<string|int, mixed>parse()
Parse a value into a param.
public
static parse(mixed $value[, array<string|int, mixed> $config = [] ]) : self
This accepts scalars, arrays - anything invalid raises an exception.
Parameters
- $value : mixed
- $config : array<string|int, mixed> = []
Tags
Return values
selfparseArray()
Parse an array of values.
public
static parseArray(array<string|int, mixed> $value[, array<string|int, mixed> $config = [] ]) : self
This can contain simple or compound conditions.
Parameters
- $value : array<string|int, mixed>
- $config : array<string|int, mixed> = []
Tags
Return values
selfparseExpression()
Parse a simple condition, typically nested.
public
static parseExpression(string|array<string|int, mixed> $value[, array<string|int, mixed> $config = [] ]) : null|self
This accepts a string like '> 20' or an array like ['>', '20'].
It cannot support compound or conditions with multiple values.
Parameters
- $value : string|array<string|int, mixed>
- $config : array<string|int, mixed> = []
Return values
null|selfparseString()
Parse a string expression.
public
static parseString(string $value[, array<string|int, mixed> $config = [] ]) : self
This can contain compound or simple conditions.
Parameters
- $value : string
- $config : array<string|int, mixed> = []
Tags
Return values
selfprepare()
Convert a value/sketch into a condition.
public
static prepare(string $column, mixed $value[, array<string|int, mixed> $config = [] ]) : PdbConditionInterface
Parameters
- $column : string
- $value : mixed
- $config : array<string|int, mixed> = []
Return values
PdbConditionInterfaceprocessValues()
Process values in an array.
public
static processValues(array<string|int, mixed> &$values, array<string|int, mixed> $config) : void
Parameters
- $values : array<string|int, mixed>
- $config : array<string|int, mixed>
split()
Split a string into an array of values.
public
static split(string $value) : array<string|int, mixed>
Prefix operators are preserved as the first element.
Parameters
- $value : string
Return values
array<string|int, mixed>toArray()
Convert this object to an array.
public
toArray() : array<string|int, mixed>
Tags
Return values
array<string|int, mixed>toCondition()
Convert a param into a condition for a specified column name.
public
toCondition(string $column) : PdbConditionInterface
Parameters
- $column : string