Oihana PHP Arango

binds

Table of Contents

Functions

aqlBind()  : string
Binds a value to an AQL variable.
aqlBindCollection()  : string
Binds a collection name to an AQL variable.
assertBindVariable()  : void
Asserts that the provided string is a valid ArangoDB bind variable name.
formatBindVariable()  : string
Formats a string as an ArangoDB bind variable.
isBindVariable()  : bool
Checks if the given string is a valid ArangoDB bind variable name.

Functions

aqlBind()

Binds a value to an AQL variable.

aqlBind(mixed $value[, array<string|int, mixed> &$binds = [] ][, string|null $to = null ][, string|null $toPrefix = null ][, bool $isCollection = false ]) : string

If $to is not provided, a unique variable name will be automatically queryId (e.g. query_123456).

If $isCollection is true, the variable will be prefixed with @@ (used for collection binding in AQL). Otherwise, it uses a single @.

Parameters
$value : mixed

The value to bind (e.g. scalar, array, object).

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

The array of all existing bindings. It is updated by reference.

$to : string|null = null

The bind variable name (without @). If null, one is auto-generated.

$toPrefix : string|null = null

The optional prefix to prepend the variable name.

$isCollection : bool = false

Whether the binding targets a collection (@@) or a value (@).

Tags
throws
BindException

If the provided variable name is invalid according to ArangoDB naming rules.

example
$binds = [];

// Manual variable name
$var = aqlBind('John', $binds, 'userId') ;
// $var   => '@userId'
// $binds => [ 'userId' => 'John' ]

// Auto-generated variable name
$var = aqlBind(42, $binds) ;
// $var   => '@q_123456'
// $binds => [ 'userId' => 'John', 'q_123456' => 42 ]
since
1.0.0
author

Marc Alcaraz

Return values
string

The formatted AQL bind variable (e.g. '@userId' or '@@collection').

aqlBindCollection()

Binds a collection name to an AQL variable.

aqlBindCollection(mixed $value[, array<string|int, mixed> &$binds = [] ][, string|null $to = null ][, string|null $toPrefix = null ]) : string

In AQL, collections are bound using double @ prefixes (e.g. @@collection).

Parameters
$value : mixed

The collection name to bind.

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

The array of all existing bindings. It is updated by reference.

$to : string|null = null

The bind variable name (without @). If null, one is auto-generated.

$toPrefix : string|null = null

The optional prefix to prepend the variable name (default 'c').

Tags
throws
BindException

If the provided variable name is invalid.

example
$binds = [];

$collectionVar = aqlBindCollection('users', $binds);
// $collectionVar => '@@c_654321'
// $binds         => [ '@c_654321' => 'users' ]
since
1.0.0
author

Marc Alcaraz

Return values
string

The formatted AQL collection bind variable (e.g. '@@collection').

assertBindVariable()

Asserts that the provided string is a valid ArangoDB bind variable name.

assertBindVariable(string|null $to) : void

A valid bind variable:

  • May optionally start with '@'
  • Must start with a letter (a-zA-Z) or underscore '_'
  • Subsequent characters can be letters, digits, or underscores

Null is considered valid and ignored.

Valid examples:

  • @userId
  • foo
  • _bar123

Invalid examples:

  • 123abc (starts with a digit)
  • @!invalid (invalid character '!')
  • user-id (hyphen not allowed)
Parameters
$to : string|null

The bind variable to validate. Null is allowed.

Tags
throws
BindException

if the string is not a valid bind variable

example
assertBindVariable('@userId');   // ✅ no exception
assertBindVariable('foo');       // ✅ no exception
assertBindVariable('123abc');    // ❌ throws BindException
assertBindVariable('@!invalid'); // ❌ throws BindException
assertBindVariable(null);        // ✅ no exception
since
1.0.0
author

Marc Alcaraz

formatBindVariable()

Formats a string as an ArangoDB bind variable.

formatBindVariable(string $name[, bool $isCollection = false ]) : string
  • If the name already starts with @, it is wrapped (using wrap()) to escape it.
  • If $isCollection is true, the resulting bind variable is prefixed with @@ for collection bind variables.
  • Otherwise, it is prefixed with a single @.
Parameters
$name : string

The name of the bind variable.

$isCollection : bool = false

Whether this is a collection bind variable (prefixed with @@).

Tags
example
formatBindVariable('userId');
// returns '@userId'

formatBindVariable('@userId');
// returns '@`@userId`'

formatBindVariable('users', true);
// returns '@@users'

formatBindVariable('@users', true);
// returns '@@`@users`'
since
1.0.0
author

Marc Alcaraz

Return values
string

The properly formatted bind variable name.

isBindVariable()

Checks if the given string is a valid ArangoDB bind variable name.

isBindVariable(string $bindParameter) : bool

ArangoDB bind variables:

  • May optionally start with '@'
  • Must start with a letter (a-zA-Z) or underscore '_'
  • Subsequent characters can be letters, digits, or underscores

Valid examples:

  • @userId
  • foo
  • _bar123

Invalid examples:

  • 123abc (starts with a digit)
  • @!invalid (invalid character '!')
  • user-id (hyphen not allowed)
Parameters
$bindParameter : string

The string to check

Tags
example
isBindVariable('@userId') ;   // true
isBindVariable('foo') ;       // true
isBindVariable('123abc') ;    // false
isBindVariable('@!invalid') ; // false
since
1.0.0
author

Marc Alcaraz

Return values
bool

True if the string is a valid bind variable name, false otherwise

On this page

Search results