Oihana PHP Arango

aqlDocument.php

Table of Contents

Functions

aqlDocument()  : string
Generate a document expression for ArangoDB AQL.

Functions

aqlDocument()

Generate a document expression for ArangoDB AQL.

aqlDocument([object|array<string|int, mixed>|string|null $keyValues = [] ][, array<string|int, mixed> $options = [] ]) : string

Accepts:

  • associative arrays: ['key' => value, ...]
  • indexed arrays of [key, value] pairs: [['key', value], ...]
  • objects: converted to associative arrays
  • strings: returned as-is inside braces
  • null: returns '}'

Options can be passed as an associative array:

  • 'useSpace' : bool, add spaces around braces and after commas
  • 'rawKeys' : array, KEYS whose value is emitted raw (not quoted, not converted). The key itself is still escaped when it is not an identifier. Applies to the top level only.
  • 'rawValues' : array, VALUES (exact strings) emitted raw wherever they appear, nested documents included. It never matches a key : to keep the value of a given key raw, use 'rawKeys'.

A string value is emitted raw when isAQLExpression() recognizes it (a known function call, doc.field, @bind, collection/key) ; anything else is quoted. Compound expressions and parenthesized subqueries are NOT recognized : declare them with 'rawKeys' (or 'rawValues').

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

Array of key-value pairs, associative array, object, string, or null

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

Optional settings: ['useSpace'=>bool, 'rawValues'=>array, 'rawKeys'=>array]

Tags
throws
InvalidArgumentException

If array structure is invalid

UnsupportedOperationException

If a value type is unsupported.

example

Simple associative array

echo document([ '_from'=>'u._id', '_to'=>'p._id' ]);
// {_from:u._id,_to:p._id}

Add spaces around braces and commas

echo document([ '_from'=>'u._id', '_to'=>'p._id' ], ['useSpace'=>true]);
// { _from:u._id, _to:p._id }

Known function calls are detected, no option needed

echo document([ '_key'=>"CONCAT('test', i)", 'name'=>"test", 'foobar'=>true ], ['useSpace'=>true]);
// { _key:CONCAT('test', i), name:'test', foobar:true }

Keeping the value of a key raw (rawKeys)

echo document([ 'next'=>'i + 1', 'items'=>'(FOR x IN things RETURN x)' ], ['rawKeys'=>['next','items']]);
// {next:i + 1,items:(FOR x IN things RETURN x)}

echo document([ 'items'=>'(FOR x IN things RETURN x)' ]);
// {items:'(FOR x IN things RETURN x)'}  ← not detected, quoted

Keeping a value raw wherever it appears (rawValues)

echo document([ 'a'=>'i + 1', 'o'=>[ 'b'=>'i + 1' ] ], ['rawValues'=>['i + 1']]);
// {a:i + 1,o:{b:i + 1}}

echo document([ 'a'=>'i + 1' ], ['rawValues'=>['a']]);
// {a:'i + 1'}  ← 'a' is a key, not a value : no effect

Nested associative array

echo document(['user'=>['name'=>'Eka','age'=>47],'active'=>true]);
// {user:{name:'Eka',age:47},active:true}

Indexed arrays (lists)

echo document(['tags'=>['php','js'],'scores'=>[10,20,30]]);
// {tags:['php','js'],scores:[10,20,30]}

Object input

$obj = (object)['name'=>'Eka','age'=>47];
echo document($obj);
// {name:'Eka',age:47}

Preformatted string

echo document("foo:'bar'");
// {foo:'bar'}

Null or empty

echo document(null);
// }
echo document([], ['useSpace'=>true]);
// { }
author

Marc Alcaraz

since
1.0.0
Return values
string —

JS-like object expression for AQL

On this page

Search results