1 <?php
2
3 /**
4 * ArangoDB PHP client: bind variables
5 *
6 * @package triagens\ArangoDb
7 * @author Jan Steemann
8 * @copyright Copyright 2012, triagens GmbH, Cologne, Germany
9 */
10
11 namespace triagens\ArangoDb;
12
13 /**
14 * A simple container for bind variables
15 *
16 * This container also handles validation of the bind values.<br>
17 * <br>
18 *
19 * @package triagens\ArangoDb
20 * @since 0.2
21 */
22 class BindVars
23 {
24 /**
25 * Current bind values
26 *
27 * @var array
28 */
29 private $_values = [];
30
31 /**
32 * Get all registered bind variables
33 *
34 * @return array - array of all registered bind variables
35 */
36 public function getAll()
37 {
38 return $this->_values;
39 }
40
41 /**
42 * Get the number of bind variables registered
43 *
44 * @return int - number of bind variables registered
45 */
46 public function getCount()
47 {
48 return count($this->_values);
49 }
50
51 /**
52 * Set the value of a single bind variable or set all bind variables at once
53 *
54 * This will also validate the bind values.
55 *
56 * Allowed value types for bind parameters are string, int,
57 * double, bool and array. Arrays must not contain any other
58 * than these types.
59 *
60 * @throws ClientException
61 *
62 * @param string|int|array $name - name of bind variable OR an array with all bind variables
63 * @param string $value - value for bind variable
64 *
65 * @return void
66 */
67 public function set($name, $value = null)
68 {
69 if (is_array($name)) {
70 foreach ($name as $value) {
71 ValueValidator::validate($value);
72 }
73 $this->_values = $name;
74 } else {
75 if (is_int($name) || is_string($name)) {
76 $this->_values[(string) $name] = $value;
77 ValueValidator::validate($value);
78 } else {
79 throw new ClientException('Bind variable name should be string or int');
80 }
81 }
82 }
83
84 /**
85 * Get the value of a bind variable with a specific name
86 *
87 * @param string $name - name of bind variable
88 *
89 * @return mixed - value of bind variable
90 */
91 public function get($name)
92 {
93 if (!array_key_exists($name, $this->_values)) {
94 return null;
95 }
96
97 return $this->_values[$name];
98 }
99 }
100