1 <?php
2
3 /**
4 * ArangoDB PHP client: base handler
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 base class for REST-based handlers
15 *
16 * @package triagens\ArangoDb
17 * @since 0.2
18 */
19 abstract class Handler
20 {
21 /**
22 * Connection object
23 *
24 * @param Connection
25 */
26 private $_connection;
27
28
29 /**
30 * Construct a new handler
31 *
32 * @param Connection $connection - connection to be used
33 *
34 */
35 public function __construct(Connection $connection)
36 {
37 $this->_connection = $connection;
38 }
39
40 /**
41 * Return the connection object
42 *
43 * @return Connection - the connection object
44 */
45 protected function getConnection()
46 {
47 return $this->_connection;
48 }
49
50
51 /**
52 * Return a connection option
53 * This is a convenience function that calls json_encode_wrapper on the connection
54 *
55 * @param $optionName - The option to return a value for
56 *
57 * @return mixed - the option's value
58 * @throws \triagens\ArangoDb\ClientException
59 */
60 protected function getConnectionOption($optionName)
61 {
62 return $this->getConnection()->getOption($optionName);
63 }
64
65
66 /**
67 * Return a json encoded string for the array passed.
68 * This is a convenience function that calls json_encode_wrapper on the connection
69 *
70 * @param array $body - The body to encode into json
71 *
72 * @return string - json string of the body that was passed
73 * @throws \triagens\ArangoDb\ClientException
74 */
75 protected function json_encode_wrapper($body)
76 {
77 return $this->getConnection()->json_encode_wrapper($body);
78 }
79
80
81 //todo: (@frankmayer) check if refactoring a bit more makes sense...
82 /**
83 * Helper function that runs through the options given and includes them into the parameters array given.
84 * Only options that are set in $includeArray will be included.
85 * This is only for options that are to be sent to the ArangoDB server in form of url parameters (like 'waitForSync', 'keepNull', etc...) .
86 *
87 * @param array $options - The options array that holds the options to include in the parameters
88 * @param array $includeArray - The array that defines which options are allowed to be included, and what their default value is. for example: 'waitForSync'=>true
89 *
90 * @return array $params - array of parameters for use in a url
91 * @throws \triagens\ArangoDb\ClientException
92 * @internal param array $params - The parameters into which the options will be included.
93 */
94 protected function includeOptionsInParams($options, array $includeArray = [])
95 {
96 $params = [];
97 foreach ($options as $key => $value) {
98 if (array_key_exists($key, $includeArray)) {
99 if ($key === ConnectionOptions::OPTION_UPDATE_POLICY) {
100 UpdatePolicy::validate($value);
101 }
102 $params[$key] = $value;
103 if ($value === null) {
104 $params[$key] = $includeArray[$key];
105 }
106 }
107 }
108
109 return $params;
110 }
111
112
113 //todo: (@frankmayer) check if refactoring a bit more makes sense...
114 /**
115 * Helper function that runs through the options given and includes them into the parameters array given.
116 * Only options that are set in $includeArray will be included.
117 * This is only for options that are to be sent to the ArangoDB server in a json body(like 'limit', 'skip', etc...) .
118 *
119 * @param array $options - The options array that holds the options to include in the parameters
120 * @param array $body - The array into which the options will be included.
121 * @param array $includeArray - The array that defines which options are allowed to be included, and what their default value is. for example: 'waitForSync'=>true
122 *
123 * @return array $params - array of parameters for use in a url
124 */
125 protected function includeOptionsInBody($options, $body, array $includeArray = [])
126 {
127 foreach ($options as $key => $value) {
128 if (array_key_exists($key, $includeArray)) {
129 $body[$key] = $value;
130 if ($value === null && $includeArray[$key] !== null) {
131 $body[$key] = $includeArray[$key];
132 }
133 }
134 }
135
136 return $body;
137 }
138
139 /**
140 * Turn a value into a collection name
141 *
142 * @throws ClientException
143 *
144 * @param mixed $value - document, collection or string
145 *
146 * @return string - collection name
147 */
148 protected function makeCollection($value)
149 {
150 if ($value instanceof Collection) {
151 return $value->getName();
152 }
153 if ($value instanceof Document) {
154 return $value->getCollectionId();
155 }
156
157 return $value;
158 }
159
160 }
161