1 <?php
2
3 /**
4 * ArangoDB PHP client: Traversal
5 *
6 * @author Frank Mayer
7 * @copyright Copyright 2013, triagens GmbH, Cologne, Germany
8 */
9
10 namespace triagens\ArangoDb;
11
12 /**
13 * Provides graph traversal
14 *
15 * A Traversal object is used to execute a graph traversal on the server side.<br>
16 * <br>
17 *
18 * The object requires the connection object, the startVertex, the edgeCollection and the optional parameters.<br>
19 * <br>
20 *
21 * @link https://docs.arangodb.com/HTTP/Traversal/index.html
22 *
23 * @package triagens\ArangoDb
24 * @since 1.4
25 */
26 class Traversal
27 {
28 /**
29 * The connection object
30 *
31 * @var Connection
32 */
33 private $_connection;
34
35 /**
36 * The traversal's attributes.
37 *
38 * @var array
39 */
40 protected $attributes = [];
41
42 /**
43 * count fields
44 */
45 const OPTION_FIELDS = 'fields';
46
47 /**
48 * Collections index
49 */
50 const ENTRY_STARTVERTEX = 'startVertex';
51
52 /**
53 * Action index
54 */
55 const ENTRY_EDGECOLLECTION = 'edgeCollection';
56
57 /**
58 * @var $_action string The action property of the traversal.
59 */
60 protected $_action;
61
62 /**
63 * Initialise the Traversal object
64 *
65 * @param Connection $connection - the connection to be used
66 * @param string $startVertex - user function initialization data
67 * @param string $edgeCollection - user function initialization data
68 * @param array $options
69 *
70 * @throws \triagens\ArangoDb\ClientException
71 */
72 public function __construct(Connection $connection, $startVertex, $edgeCollection, array $options = null)
73 {
74 $this->_connection = $connection;
75 $this->setStartVertex($startVertex);
76 $this->setEdgeCollection($edgeCollection);
77
78 if (is_array($options)) {
79 $this->attributes = array_merge($this->attributes, $options);
80 }
81 }
82
83
84 /**
85 * Execute and get the traversal result
86 *
87 * @return array $responseArray
88 * @throws \triagens\ArangoDb\Exception
89 * @throws \triagens\ArangoDb\ClientException
90 */
91 public function getResult()
92
93 {
94 $bodyParams = $this->attributes;
95
96
97 $response = $this->_connection->post(
98 Urls::URL_TRAVERSAL,
99 $this->getConnection()->json_encode_wrapper($bodyParams)
100 );
101
102 return $response->getJson();
103 }
104
105
106 /**
107 * Return the connection object
108 *
109 * @return Connection - the connection object
110 */
111 protected function getConnection()
112 {
113 return $this->_connection;
114 }
115
116
117 /**
118 * Set name of the user function. It must have at least one namespace, but also can have sub-namespaces.
119 * correct:
120 * 'myNamespace:myFunction'
121 * 'myRootNamespace:mySubNamespace:myFunction'
122 *
123 * wrong:
124 * 'myFunction'
125 *
126 *
127 * @param string $value
128 *
129 * @throws \triagens\ArangoDb\ClientException
130 */
131 public function setStartVertex($value)
132 {
133 $this->set(self::ENTRY_STARTVERTEX, (string) $value);
134 }
135
136
137 /**
138 * Get name value
139 *
140 * @return string name
141 */
142 public function getStartVertex()
143 {
144 return $this->get(self::ENTRY_STARTVERTEX);
145 }
146
147 /**
148 * Set user function code
149 *
150 * @param string $value
151 *
152 * @throws \triagens\ArangoDb\ClientException
153 */
154 public function setEdgeCollection($value)
155 {
156 $this->set(self::ENTRY_EDGECOLLECTION, (string) $value);
157 }
158
159
160 /**
161 * Get user function code
162 *
163 * @return string name
164 */
165 public function getEdgeCollection()
166 {
167 return $this->get(self::ENTRY_EDGECOLLECTION);
168 }
169
170
171 /**
172 * Set an attribute
173 *
174 * @param $key
175 * @param $value
176 *
177 * @throws ClientException
178 */
179 public function set($key, $value)
180 {
181 if (!is_string($key)) {
182 throw new ClientException('Invalid attribute key');
183 }
184
185 $this->attributes[$key] = $value;
186 }
187
188
189 /**
190 * Set an attribute, magic method
191 *
192 * This is a magic method that allows the object to be used without
193 * declaring all attributes first.
194 *
195 * @throws ClientException
196 *
197 * @param string $key - attribute name
198 * @param mixed $value - value for attribute
199 *
200 * @magic
201 *
202 * @return void
203 */
204 public function __set($key, $value)
205 {
206 switch ($key) {
207 case self::ENTRY_STARTVERTEX :
208 $this->setStartVertex($value);
209 break;
210 case self::ENTRY_EDGECOLLECTION :
211 $this->setEdgeCollection($value);
212 break;
213 default:
214 $this->set($key, $value);
215 break;
216 }
217 }
218
219 /**
220 * Get an attribute
221 *
222 * @magic
223 *
224 * @param string $key - name of attribute
225 *
226 * @return mixed - value of attribute, NULL if attribute is not set
227 */
228 public function get($key)
229 {
230 if (isset($this->attributes[$key])) {
231 return $this->attributes[$key];
232 }
233
234 return null;
235 }
236
237 /**
238 * Get an attribute, magic method
239 *
240 * This function is mapped to get() internally.
241 *
242 * @magic
243 *
244 * @param string $key - name of attribute
245 *
246 * @return mixed - value of attribute, NULL if attribute is not set
247 */
248 public function __get($key)
249 {
250 return $this->get($key);
251 }
252
253
254 /**
255 * Is triggered by calling isset() or empty() on inaccessible properties.
256 *
257 * @param string $key - name of attribute
258 *
259 * @return boolean returns true or false (set or not set)
260 */
261 public function __isset($key)
262 {
263 if (isset($this->attributes[$key])) {
264 return true;
265 }
266
267 return false;
268 }
269
270
271 /**
272 * Returns the action string
273 *
274 * @magic
275 *
276 * @return string - the current action string
277 */
278 public function __toString()
279 {
280 return $this->_action;
281 }
282 }
283