guardProjection.php
Table of Contents
Functions
- guardProjection() : string
- Wrap an already-built **fabricated** projection behind an optional guard, so the field can yield `null` (or a `Field::ELSE` fallback) instead of a value rebuilt out of nothing.
Functions
guardProjection()
Wrap an already-built **fabricated** projection behind an optional guard, so the field can yield `null` (or a `Field::ELSE` fallback) instead of a value rebuilt out of nothing.
guardProjection(string $value, array<string|int, mixed> $options, string $doc, string $source[, string|null $test = null ]) : string
Two projections fabricate rather than read, and neither fails when there is nothing to fabricate from:
- aqlFieldDocument() reconstructs an object attribute by attribute. When the
source attribute is missing, each line reads an attribute of a nothing — which AQL
resolves to
nullwithout error — and the object is emitted all the same. - aqlFieldUrl() rebuilds a link with
CONCAT(), which drops its null arguments : a missing key yields a truncated address, not a missing one.
So an empty slot comes back dressed:
{ "_key": null , "name": null , "url": "https://base/things/" }
Two markers, both opt-in, guard it:
- Field::NULLABLE — the declared intent « nothing to build from, no value »,
compiled by default to an
IS_OBJECT()test on$source. On a rebuilt object that is deliberately a type test and not a!= nullcomparison: an attribute that exists but is not an object (a string, a number) rebuilds the very same object of nulls, and the house style tests the type (aqlFieldArray(), aqlFieldObject()). A caller whose shape calls for another test passes it as$test— aqlFieldBool() asks for!= null, becauseIS_BOOL()would make every document storing1or"yes"abstain, whenTO_BOOL()exists precisely to accept them. - Field::WHEN — the general mechanism, sharing the condition grammar of the
scalar conditional projection (buildWhenCondition()). It is compiled against
$doc, the reference the projection itself reads from — the parent of a rebuilt sub-document, and the sub-document itself for a url projected inside one: that is what keeps the read gate of conditionReadsDeniedField() correct without a line of its own, since it gates aField::WHENagainst the projection of the current level.
Declared together they compose with &&. A single condition is emitted bare, a pair
between parentheses — so the guard never depends on the surrounding precedence.
With neither marker the value is returned untouched: the emitted AQL of every existing projection is unchanged, byte for byte.
Parameters
- $value : string
-
The already-built projection value (e.g.
{name:doc.thing.name}). - $options : array<string|int, mixed>
-
The field definition (reads NULLABLE / WHEN / ELSE).
- $doc : string
-
The parent document reference the condition and the else branch read from.
- $source : string
-
The source attribute the projection is rebuilt from — the sub-document for a
Filter::DOCUMENT(e.g.doc.thing), the appended key for aFilter::URL(e.g.doc._key). Read byField::NULLABLEonly. - $test : string|null = null
-
The condition
Field::NULLABLEcompiles to. Defaults toIS_OBJECT($source), the test a rebuilt object calls for.
Tags
Return values
string —The value, guarded when asked for: <cond> ? <value> : <else>.