Oihana PHP Arango

aqlSerialize.php

Table of Contents

Functions

aqlSerialize()  : string
Serialize a value (array, object, scalar) into an AQL fragment.

Functions

aqlSerialize()

Serialize a value (array, object, scalar) into an AQL fragment.

aqlSerialize(mixed $value[, bool $topLevel = true ]) : string

CustomRules:

  • Strings are returned as-is (raw expression)
  • Associative arrays => {key:value}
  • Indexed arrays => [v1,v2,...]
  • Objects => {key:value} using public properties
  • JsonSerializable objects are jsonSerialized first
Parameters
$value : mixed
$topLevel : bool = true
Tags
example

Associative array

echo aqlSerialize(['name' => 'John', 'age' => 30]);
// {name:'John',age:30}

Indexed array

echo aqlSerialize([1, 2, 3]);
// [1,2,3]

Nested array

echo aqlSerialize(['user' => ['id' => 1, 'tags' => ['php','js']]]);
// {user:{id:1,tags:['php','js']}}

Object

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

Nested object

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

JsonSerializable object

class User implements JsonSerializable {
public string $name = 'John';
public function jsonSerialize() { return ['name'=>$this->name]; }
}
$user = new User();
echo aqlSerialize($user);
// {name:'John'}

String (raw AQL)

echo aqlSerialize("FOR u IN users RETURN u");
// FOR u IN users RETURN u

Scalar values

echo aqlSerialize(true);
// true
echo aqlSerialize(123);
// 123

Mixed nested structures

$data =
[
   'user' => (object)['id'=>1,'roles'=>['admin','editor']],
   'tags' => ['php','js'],
   'count' => 10
];
echo aqlSerialize($data);
// {user:{id:1,roles:['admin','editor']},tags:['php','js'],count:10}
author

Marc Alcaraz

since
1.0.0
Return values
string
On this page

Search results