Oihana PHP Arango

PreserveFilter uses FilterTrait

FinalYes

The compiled rule of one `[arango.preserves.<collection>]` entry, and the AQL queries the `preserve` and `reinject` actions run with it.

The rule is written in the ?filter= grammar and compiled by the very compiler of the models (FilterTrait) — no free AQL ever reaches a query :

  • the values are bind parameters ;
  • the keys are written into the AQL, but only those the rule declares in its filters list, as AQL::FILTERS does on a model ;
  • the operators, the groups and every shape the compiler cannot read are refused by the compiler itself ;
  • the collections are bind parameters, and the queries are written here.

A refusal of the compiler — a ValidationException meant for an HTTP answer — is turned into an InvalidArgumentException naming the collection.

Tags
since
1.10.0
author

Marc Alcaraz

Table of Contents

Constants

BACKUP_BIND  : string = 'backup'
The bind parameter naming the backup collection in the queries.
COLLECTION_BIND  : string = 'collection'
The bind parameter naming the preserved collection in the queries.
DEFAULT_MAX_PERCENT  : int = 50
The largest share of its collection, in percent, a rule names by default.
FILTERS  : string = 'filters'
The 'filters' parameter constant.

Properties

$backup  : string
The name of the backup collection, `<collection>__preserved`.
$binds  : array<string|int, mixed>
The bind parameters of the compiled condition.
$collection  : string
The preserved collection.
$condition  : string
The compiled AQL condition, written against `doc`.
$filters  : array<string|int, mixed>|null
Defines all valid filtering conditions for queries used in the list() and count() methods.
$maxPercent  : int
The largest share of the collection, in percent, the rule may name at `reinject`.
$edges  : array<string|int, mixed>|null
No edge : a rule reads the fields of the document itself, never a relation.
$fields  : array<string|int, mixed>|null
No projection : nothing gates the keys but the `filters` of the rule.
$joins  : array<string|int, mixed>|null
No join : a rule reads the fields of the document itself, never a relation.
$view  : array<string|int, mixed>|null
No ArangoSearch view : a rule filters the collection itself.

Methods

__construct()  : mixed
Compiles the rule of a collection.
backupCountQuery()  : AqlQuery
The query counting the documents of the backup.
collectionCountQuery()  : AqlQuery
The query counting every document of the collection.
copyQuery()  : AqlQuery
The query copying the documents the rule names into the backup, and returning how many it copied.
countQuery()  : AqlQuery
The query counting the documents of the collection the rule names.
exceeds()  : bool
Indicates whether `$named` documents out of `$total` exceed the share the rule may name. Compared on integers : `$named × 100 > maxPercent × $total`.
initializeFilters()  : static
Initialize the 'filters' property.
prepareFilter()  : string|null
Prepare the AQL query filtering with specific definitions.
putBackQuery()  : AqlQuery
The query copying the whole backup back into the collection, and returning how many documents it put back.
removeQuery()  : AqlQuery
The query removing from the collection the documents the rule names, and returning how many it removed.
share()  : float
The share, in percent, that `$named` documents are of `$total` — for display only ; the guard compares integers ({@see exceeds()}).
alterFilterKey()  : string
Apply the key-side (left) `alt` transformation to a key expression.
compileFilter()  : string|null
Compiles a filter — the value of `Arango::FILTER`, a member of a group, the operand of a `not` — into its AQL condition.
matchFieldsAuthorized()  : bool
Decides whether every sub-field a `match` names may be read by this caller.
prepareFilterBetween()  : string|null
Prepares an inclusive `between` (range) clause: `key >= @min && key <= @max`.
prepareFilterComparator()  : string
Prepares the filter clause with a specific operator.
prepareFilterKey()  : string
Prepares the filter clause with a specific key and document, with optional function transformations via 'alt' parameter.
prepareFilterValue()  : string
Prepare the filter clause with a specific value to evaluates.
resolveFilterComparator()  : string
Translates an operator code into its AQL comparator, refusing the ones this filter cannot honour.
collectMatchFields()  : array<string|int, mixed>

Constants

BACKUP_BIND

The bind parameter naming the backup collection in the queries.

public string BACKUP_BIND = 'backup'

COLLECTION_BIND

The bind parameter naming the preserved collection in the queries.

public string COLLECTION_BIND = 'collection'

DEFAULT_MAX_PERCENT

The largest share of its collection, in percent, a rule names by default.

public int DEFAULT_MAX_PERCENT = 50

A rule names the documents of one machine among the documents a restore brought from elsewhere : a small share. Naming most of the collection says the rule is wrong.

FILTERS

The 'filters' parameter constant.

public string FILTERS = 'filters'

Properties

$backup read-only

The name of the backup collection, `<collection>__preserved`.

public string $backup

$binds read-only

The bind parameters of the compiled condition.

public array<string|int, mixed> $binds

$collection read-only

The preserved collection.

public string $collection

$condition read-only

The compiled AQL condition, written against `doc`.

public string $condition

$filters

Defines all valid filtering conditions for queries used in the list() and count() methods.

public array<string|int, mixed>|null $filters = []

$maxPercent read-only

The largest share of the collection, in percent, the rule may name at `reinject`.

public int $maxPercent

$edges

No edge : a rule reads the fields of the document itself, never a relation.

protected array<string|int, mixed>|null $edges = null

$fields

No projection : nothing gates the keys but the `filters` of the rule.

protected array<string|int, mixed>|null $fields = null

$joins

No join : a rule reads the fields of the document itself, never a relation.

protected array<string|int, mixed>|null $joins = null

$view

No ArangoSearch view : a rule filters the collection itself.

protected array<string|int, mixed>|null $view = null

Methods

__construct()

Compiles the rule of a collection.

public __construct(string $collection, array<string|int, mixed> $filters, array<string|int, mixed> $filter[, int $maxPercent = self::DEFAULT_MAX_PERCENT ]) : mixed
Parameters
$collection : string

The collection whose documents the rule names.

$filters : array<string|int, mixed>

The keys the filter may name, and their FilterType.

$filter : array<string|int, mixed>

The decoded filter : a segment or a group.

$maxPercent : int = self::DEFAULT_MAX_PERCENT

The largest share of the collection, in percent, the rule may name at reinject.

Tags
throws
BindException
ConstantException
ContainerExceptionInterface
DependencyException
NotFoundException
NotFoundExceptionInterface
ReflectionException
UnsupportedOperationException

copyQuery()

The query copying the documents the rule names into the backup, and returning how many it copied.

public copyQuery() : AqlQuery
Return values
AqlQuery

exceeds()

Indicates whether `$named` documents out of `$total` exceed the share the rule may name. Compared on integers : `$named × 100 > maxPercent × $total`.

public exceeds(int $named, int $total) : bool
Parameters
$named : int

The documents the rule names.

$total : int

The documents of the collection.

Return values
bool —

True when the rule names more than its maxPercent of the collection.

initializeFilters()

Initialize the 'filters' property.

public initializeFilters([array<string|int, mixed> $init = [] ]) : static
Parameters
$init : array<string|int, mixed> = []
Return values
static

prepareFilter()

Prepare the AQL query filtering with specific definitions.

public prepareFilter([array<string|int, mixed>|null $init = [] ][, array<string|int, mixed>|null &$binds = null ][, string $docRef = AQL::DOC ][, array<string|int, mixed> $auth = [] ]) : string|null

Takes either a whole model init — where Arango::FILTER carries the filter, or is absent for an unfiltered list — or the filter payload itself : a segment ({ key , op , val , … }) or a logical group ([ 'and' , … ]). The payload is compiled by compileFilter(), which refuses every shape it cannot read.

What the request gets wrong is REFUSED, never dropped : a key the model does not declare at that level, a segment that speaks the grammar but names no key, the [*] marker spelled against its type, an unknown operator or alt function — all answer a RequestValidationException (400) naming the fault. Dropped, such a segment used to leave the query without it, and the whole collection came back in 200. What the MODEL gets wrong — a declared key under a form nothing here compiles — answers a plain ValidationException instead, so the two families of fault are told apart by their exception.

null means « no condition » and nothing else : an init that carries no filter (Arango::FILTER absent, null or []), or a bound that expresses neither min nor max.

Parameters
$init : array<string|int, mixed>|null = []

The model init, or the filter payload.

$binds : array<string|int, mixed>|null = null

The bind variables, populated by reference.

$docRef : string = AQL::DOC

The document reference the conditions are written against.

$auth : array<string|int, mixed> = []

The request-level authorizer, captured at the root and passed down.

Tags
throws
RequestValidationException

When the filter has a shape no filter reads, names an unknown key, no key, a mis-spelled [*] marker, or an operator / function the filter cannot honour.

BindException
ConstantException
ContainerExceptionInterface
DependencyException
NotFoundException
NotFoundExceptionInterface
ReflectionException
UnsupportedOperationException
ValidationException
Return values
string|null —

The AQL condition, or null when nothing constrains the query.

putBackQuery()

The query copying the whole backup back into the collection, and returning how many documents it put back.

public putBackQuery() : AqlQuery
Return values
AqlQuery

removeQuery()

The query removing from the collection the documents the rule names, and returning how many it removed.

public removeQuery() : AqlQuery
Return values
AqlQuery

share()

The share, in percent, that `$named` documents are of `$total` — for display only ; the guard compares integers ({@see exceeds()}).

public share(int $named, int $total) : float
Parameters
$named : int

The documents the rule names.

$total : int

The documents of the collection.

Return values
float —

The share in percent, 0 for an empty collection.

alterFilterKey()

Apply the key-side (left) `alt` transformation to a key expression.

protected alterFilterKey(string $key[, array<string|int, mixed> $init = [] ][, array<string|int, mixed>|null &$binds = null ]) : string

Thin wrapper over static::alterExpression(): it resolves the alt parameter into its key/value sides via static::resolveAltSides() and applies the key-side chain. The three legacy alt forms (string, list of functions, function-with-params) keep transforming the key only, unchanged.

Parameters
$key : string

The key expression to transform.

$init : array<string|int, mixed> = []

Filter initialization array containing the 'alt' parameter.

$binds : array<string|int, mixed>|null = null
Tags
throws
UnsupportedOperationException
ValidationException
Return values
string —

The transformed key expression.

compileFilter()

Compiles a filter — the value of `Arango::FILTER`, a member of a group, the operand of a `not` — into its AQL condition.

protected compileFilter(mixed $init[, array<string|int, mixed>|null &$binds = null ][, string $docRef = AQL::DOC ][, array<string|int, mixed> $auth = [] ]) : string|null

Every shape it cannot read is REFUSED : a scalar or a JSON string, an empty member, an object that speaks no filter grammar ({"agee":1}, {"age":{"$gte":30}}, the {"logic":…,"conditions":[…]} form). They used to be dropped, and dropped, they answered the whole collection — inside an and, they loosened the group.

Parameters
$init : mixed

The filter : a segment or a group.

$binds : array<string|int, mixed>|null = null

The bind variables, populated by reference.

$docRef : string = AQL::DOC

The document reference the conditions are written against.

$auth : array<string|int, mixed> = []

The request-level authorizer, captured at the root and passed down.

Tags
throws
RequestValidationException

When the filter cannot be read, or names a key, an operator or a function the model cannot honour.

BindException
ConstantException
ContainerExceptionInterface
DependencyException
NotFoundException
NotFoundExceptionInterface
ReflectionException
UnsupportedOperationException
ValidationException
Return values
string|null —

The AQL condition, or null when the filter constrains nothing (a declared FilterType::VIRTUAL, a bound with neither min nor max).

matchFieldsAuthorized()

Decides whether every sub-field a `match` names may be read by this caller.

protected matchFieldsAuthorized(array<string|int, mixed> $match, string $relativeBase, array<string|int, mixed>|null $fields, array<string|int, mixed> $auth) : bool

🔑 Shared by both depths. A match gates each sub-field it references on the Field::REQUIRES of that exact sub-field, and a refused one must neutralise the whole predicate to false — never drop it, which would loosen a none match into an existence oracle. The flat lookup gates against the model's own projection; the hierarchical walk gates against the projection of the level it has reached, with the path it has accumulated. Same rule, two inputs.

Parameters
$match : array<string|int, mixed>

The match payload.

$relativeBase : string

The array's path relative to $fields (attachments, resolution.steps).

$fields : array<string|int, mixed>|null

The projection to gate against, or null when the model declares none (nothing to inherit).

$auth : array<string|int, mixed>

The caller's permission context.

Return values
bool —

False as soon as one named sub-field is refused.

prepareFilterBetween()

Prepares an inclusive `between` (range) clause: `key >= @min && key <= @max`.

protected prepareFilterBetween(array<string|int, mixed> $init, array<string|int, mixed>|null &$binds, string $docRef, callable $resolve, bool $defaultBounds) : string|null

The compared key is alt-aware (it flows through static::alterFilterKey()). Each bound is resolved by $resolve, which differs per filter type — a raw bind for numbers/strings, the date machinery (now / timezone) for dates.

Bound omission is type-driven:

  • $defaultBounds = false (number/string): an omitted bound drops its side, yielding a one-sided range (key >= @min or key <= @max).
  • $defaultBounds = true (date): an omitted bound still emits a clause; the resolver maps the null value to "now", so the range is always two-sided.
Parameters
$init : array<string|int, mixed>

The filter init (reads min / max).

$binds : array<string|int, mixed>|null

The bind variables, populated by reference.

$docRef : string

The document reference.

$resolve : callable

fn(mixed $value, ?array &$binds): string — resolves a bound to AQL.

$defaultBounds : bool

Whether an omitted bound still emits a clause (dates) or is dropped.

Tags
throws
UnsupportedOperationException
ValidationException
Return values
string|null —

The range clause, or null when no bound was given — no constraint expressed is no clause, never an empty one.

prepareFilterComparator()

Prepares the filter clause with a specific operator.

protected prepareFilterComparator([array<string|int, mixed> $init = [] ]) : string
Parameters
$init : array<string|int, mixed> = []
Tags
throws
RequestValidationException

When an operator is supplied that this filter cannot honour.

Return values
string

prepareFilterKey()

Prepares the filter clause with a specific key and document, with optional function transformations via 'alt' parameter.

protected prepareFilterKey([string|array<string|int, mixed>|null $init = [] ][, string $docRef = AQL::DOC ][, array<string|int, mixed>|null &$binds = null ]) : string

Supports function chaining:

  • Single function: "alt":"lower"
  • Multiple functions: "alt":["trim","lower"]
  • Functions with params: "alt":[["trim",1],"lower"]
Parameters
$init : string|array<string|int, mixed>|null = []

Filter initialization array

$docRef : string = AQL::DOC

Document reference (default: AQL::DOC)

$binds : array<string|int, mixed>|null = null
Tags
throws
UnsupportedOperationException
ValidationException
example
// Simple key
prepareFilterKey(['key' => 'name'], 'doc')
// Returns: "doc.name"

// With single function
prepareFilterKey(['key' => 'name', 'alt' => 'lower'], 'doc')
// Returns: "LOWER(doc.name)"

// With function chain
prepareFilterKey(['key' => 'name', 'alt' => ['trim', 'lower']], 'doc')
// Returns: "LOWER(TRIM(doc.name))"

// With parameters
prepareFilterKey(['key' => 'code', 'alt' => [['substring', 0, 3]]], 'doc')
// Returns: "SUBSTRING(doc.code, 0, 3)"
Return values
string —

The transformed key expression

prepareFilterValue()

Prepare the filter clause with a specific value to evaluates.

protected prepareFilterValue([array<string|int, mixed>|null $init = [] ][, array<string|int, mixed>|null &$binds = null ]) : string

Binds the raw value, then applies the value-side (right) alt chain when one is set (object form alt:{ key:.. , val:.. } or val:true mirror):

  • scalar value → the chain wraps the bind placeholder, e.g. LOWER(@value).
  • array value (e.g. op:in) → the chain is mapped over each element via an inline projection, e.g. @value[* RETURN LOWER(CURRENT)]. The single bind still holds the whole array, so existing binding behavior is preserved.
Parameters
$init : array<string|int, mixed>|null = []
$binds : array<string|int, mixed>|null = null
Tags
throws
BindException
UnsupportedOperationException
ValidationException
Return values
string

resolveFilterComparator()

Translates an operator code into its AQL comparator, refusing the ones this filter cannot honour.

protected resolveFilterComparator(mixed $op) : string

🚨 Reaching this point means the operator was not handled upstream. The filter types intercept what they can — sw / ew / contains / regex on a string, between on a string, a number or a date, distance on a geo field — and everything else falls through to the infix catalogue. So an operator arriving here that the catalogue does not carry is one of two mistakes, and both used to compile to ==:

  • a code that does not exist (zzz, GT, >), plainly a typo ;
  • a code that exists but not for this field: {"key":"price","op":"sw","val":12} asks for prices starting with 12 and used to answer prices equal to 12 — a handful of plausible rows, in 200, answering a question nobody asked.

The second is the dangerous one: an empty page is noticed, a wrong page is not.

⚠ An absent operator still means equality. null is the documented default and the one case where falling back to == is what the caller meant. An empty string is treated the same way rather than refused — an unfilled <select> submits one, and that is an absence expressed, not a typo.

Parameters
$op : mixed

The operator code supplied by the caller, if any.

Tags
throws
RequestValidationException

When the operator is supplied and cannot be honoured here.

Return values
string —

The AQL comparator.

collectMatchFields()

private collectMatchFields(array<string|int, mixed> $match) : array<string|int, mixed>
Parameters
$match : array<string|int, mixed>
Return values
array<string|int, mixed>
On this page

Search results