Download the PHP package rasuvaeff/specification without Composer
On this page you can find all versions of the php package rasuvaeff/specification. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download rasuvaeff/specification
More information about rasuvaeff/specification
Files in rasuvaeff/specification
Package specification
Short Description Type-safe specification pattern for composing Yiisoft DB queries: AND, OR, NOT, comparisons, IN, BETWEEN, LIKE — with SQL injection protection.
License BSD-3-Clause
Homepage https://github.com/rasuvaeff/specification
Informations about the package specification
rasuvaeff/specification
Specification pattern for building Yiisoft DB queries.
Using an AI coding assistant?
llms.txtis a compact, self-contained reference of the whole public API plus copy-paste recipes — drop it into the model's context. Contributors: seeAGENTS.md. Projects using the llm/skills Composer plugin also get this package's agent skill synced into.agents/skills/automatically on install.
Requirements
- PHP 8.3+
yiisoft/db^2.0.1
Installation
Usage
SpecificationBuilder
Fluent builder for composing query conditions:
Available methods:
| Method | SQL equivalent |
|---|---|
where($col, $val, $op) |
col op val (any operator) |
whereEqual($col, $val) |
col = val |
whereNotEqual($col, $val) |
col != val |
whereGreaterThan($col, $val) |
col > val |
whereGreaterThanOrEqual($col, $val) |
col >= val |
whereLessThan($col, $val) |
col < val |
whereLessThanOrEqual($col, $val) |
col <= val |
whereIn($col, $values) |
col IN (values) |
whereNotIn($col, $values) |
col NOT IN (values) |
whereLike($col, $pattern) |
col LIKE pattern |
whereNotLike($col, $pattern) |
col NOT LIKE pattern |
whereBetween($col, $from, $to) |
col BETWEEN from AND to |
whereNotBetween($col, $from, $to) |
col NOT BETWEEN from AND to |
whereIlike($col, $pattern) |
col ILIKE pattern |
whereNotIlike($col, $pattern) |
col NOT ILIKE pattern |
whereStartsWith($col, $prefix) |
col LIKE prefix% |
whereEndsWith($col, $suffix) |
col LIKE %suffix |
whereContains($col, $substring) |
col LIKE %substring% |
whereNull($col) |
col IS NULL |
whereNotNull($col) |
col IS NOT NULL |
orWhere(callable) |
OR (nested conditions) |
notWhere(callable) |
NOT (nested conditions) |
orderBy($columns) |
ORDER BY col [ASC\|DESC] |
limit($n) |
LIMIT n |
offset($n) |
OFFSET n |
Specifications
Building blocks for composing complex conditions:
ComparisonSpecification factory methods
Custom visitor
Implement SpecificationVisitor<T> to traverse the specification tree:
Examples
Runnable, offline examples (in-memory SQLite) live in examples/:
builder.php (AND/IN/BETWEEN) and or-not-raw.php (OR/NOT/raw/order+limit).
Security
- Values are parameterized. All comparison/IN/BETWEEN/LIKE values are bound
as parameters by
yiisoft/db, so they are safe against SQL injection. - Column names are not validated — they are passed to
yiisoft/dband quoted as identifiers, but there is no allow-list. Pass only trusted column names (typically hard-coded), never raw user input. RawSpecificationis a raw escape hatch. The condition string is not escaped — never build it from untrusted input. Pass user values only through the$paramsmap (placeholders):new RawSpecification('age > :age', ['age' => $value]).
Performance
SpecificationBuilder is immutable — each where*(), limit(), and offset() call
clones the builder before returning. This is safe and predictable but carries a small
overhead (~3.3µs for a 7-step chain, vs ~2.4µs for direct CompositeSpecification
composition). orWhere() additionally allocates a temporary builder and invokes a
closure (~2.8µs vs ~1.4µs for direct OrSpecification::create()).
For most web request workloads (1–5 specs per request, DB queries taking 1–100ms)
this overhead is negligible. For high-throughput batch processing where specs are
built in a tight loop, prefer the direct CompositeSpecification API:
Benchmarks live in benchmarks/ and run via composer bench (requires
testo/bench).
Notes
ilike/not ilikeare PostgreSQL-specific; other drivers (e.g. MySQL) do not support them. Uselikefor case-insensitive needs on those drivers.- For OR conditions use
OrSpecificationorSpecificationBuilder::orWhere().CompositeSpecificationcomposes with AND semantics. withOrCondition()value formats: a scalar is plain equality ('status' => 'active'); an array whose first element is a known operator is a shorthand ('age' => ['>', 18],'type' => ['in', ['a', 'b']]); any other array is treated as a value, so a plain list ('name' => ['a', 'b']) becomes anINcondition. The operator is matched case-insensitively.
License
BSD-3-Clause.