Skip to content

Commit 968d7c5

Browse files
committed
Support for Traversal API. Closes #148
1 parent 05ba9d3 commit 968d7c5

File tree

3 files changed

+910
-0
lines changed

3 files changed

+910
-0
lines changed

lib/triagens/ArangoDb/Traversal.php

Lines changed: 247 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,247 @@
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+
* Traversal object<br>
16+
* A Traversal is an object that is used to execute a graph traversal on the server side.<br>
17+
* <br>
18+
*
19+
* The object requires the connection object, the startVertex, the edgeCollection and the optional parameters.<br>
20+
* <br>
21+
*
22+
* @package triagens\ArangoDb
23+
* @since 1.4
24+
*/
25+
class Traversal
26+
{
27+
/**
28+
* The connection object
29+
*
30+
* @var Connection
31+
*/
32+
private $_connection = null;
33+
34+
/**
35+
* The traversal's attributes.
36+
*
37+
* @var array
38+
*/
39+
protected $attributes = array();
40+
41+
/**
42+
* count fields
43+
*/
44+
const OPTION_FIELDS = 'fields';
45+
46+
/**
47+
* Collections index
48+
*/
49+
const ENTRY_STARTVERTEX = 'startVertex';
50+
51+
/**
52+
* Action index
53+
*/
54+
const ENTRY_EDGECOLLECTION = 'edgeCollection';
55+
56+
57+
/**
58+
* Initialise the Traversal object
59+
*
60+
* @param Connection $connection - the connection to be used
61+
* @param string $startVertex - user function initialization data
62+
* @param string $edgeCollection - user function initialization data
63+
* @param array $options
64+
*
65+
* @return \triagens\ArangoDb\Traversal
66+
*/
67+
public function __construct(Connection $connection, $startVertex, $edgeCollection, array $options = null)
68+
{
69+
$this->_connection = $connection;
70+
$this->setStartVertex($startVertex);
71+
$this->setEdgeCollection($edgeCollection);
72+
73+
if (is_array($options)) {
74+
$this->attributes = array_merge($this->attributes, $options);
75+
}
76+
}
77+
78+
79+
/**
80+
* Execute and get the traversal result
81+
*
82+
* @return array $responseArray
83+
*/
84+
public function getResult()
85+
86+
{
87+
$bodyParams = $this->attributes;
88+
89+
90+
$response = $this->_connection->post(
91+
Urls::URL_TRAVERSAL,
92+
$this->getConnection()->json_encode_wrapper($bodyParams)
93+
);
94+
$responseArray = $response->getJson();
95+
96+
return $responseArray;
97+
}
98+
99+
100+
/**
101+
* Return the connection object
102+
*
103+
* @return Connection - the connection object
104+
*/
105+
protected function getConnection()
106+
{
107+
return $this->_connection;
108+
}
109+
110+
111+
/**
112+
* Set name of the user function. It must have at least one namespace, but also can have sub-namespaces.
113+
* correct:
114+
* 'myNamespace:myFunction'
115+
* 'myRootNamespace:mySubNamespace:myFunction'
116+
*
117+
* wrong:
118+
* 'myFunction'
119+
*
120+
*
121+
* @param string $value
122+
*/
123+
public function setStartVertex($value)
124+
{
125+
$this->set(self::ENTRY_STARTVERTEX, (string) $value);
126+
}
127+
128+
129+
/**
130+
* Get name value
131+
*
132+
* @return string name
133+
*/
134+
public function getStartVertex()
135+
{
136+
return $this->get(self::ENTRY_STARTVERTEX);
137+
}
138+
139+
/**
140+
* Set user function code
141+
*
142+
* @param string $value
143+
*/
144+
public function setEdgeCollection($value)
145+
{
146+
$this->set(self::ENTRY_EDGECOLLECTION, (string) $value);
147+
}
148+
149+
150+
/**
151+
* Get user function code
152+
*
153+
* @return string name
154+
*/
155+
public function getEdgeCollection()
156+
{
157+
return $this->get(self::ENTRY_EDGECOLLECTION);
158+
}
159+
160+
161+
/**
162+
* Set an attribute
163+
*
164+
* @param $key
165+
* @param $value
166+
*
167+
* @throws ClientException
168+
*/
169+
public function set($key, $value)
170+
{
171+
if (!is_string($key)) {
172+
throw new ClientException('Invalid attribute key');
173+
}
174+
175+
$this->attributes[$key] = $value;
176+
}
177+
178+
179+
/**
180+
* Set an attribute, magic method
181+
*
182+
* This is a magic method that allows the object to be used without
183+
* declaring all attributes first.
184+
*
185+
* @throws ClientException
186+
*
187+
* @param string $key - attribute name
188+
* @param mixed $value - value for attribute
189+
*
190+
* @return void
191+
*/
192+
public function __set($key, $value)
193+
{
194+
switch ($key) {
195+
case self::ENTRY_STARTVERTEX :
196+
$this->setStartVertex($value);
197+
break;
198+
case self::ENTRY_EDGECOLLECTION :
199+
$this->setEdgeCollection($value);
200+
break;
201+
default:
202+
$this->set($key, $value);
203+
break;
204+
}
205+
}
206+
207+
/**
208+
* Get an attribute
209+
*
210+
* @param string $key - name of attribute
211+
*
212+
* @return mixed - value of attribute, NULL if attribute is not set
213+
*/
214+
public function get($key)
215+
{
216+
if (isset($this->attributes[$key])) {
217+
return $this->attributes[$key];
218+
}
219+
220+
return null;
221+
}
222+
223+
/**
224+
* Get an attribute, magic method
225+
*
226+
* This function is mapped to get() internally.
227+
*
228+
* @param string $key - name of attribute
229+
*
230+
* @return mixed - value of attribute, NULL if attribute is not set
231+
*/
232+
public function __get($key)
233+
{
234+
return $this->get($key);
235+
}
236+
237+
238+
/**
239+
* Returns the action string
240+
*
241+
* @return string - the current action string
242+
*/
243+
public function __toString()
244+
{
245+
return $this->_action;
246+
}
247+
}

lib/triagens/ArangoDb/Urls.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -187,4 +187,9 @@ abstract class Urls
187187
* base URL part for user management
188188
*/
189189
const URL_USER = '/_api/user';
190+
191+
/**
192+
* base URL part for user management
193+
*/
194+
const URL_TRAVERSAL = '/_api/traversal';
190195
}

0 commit comments

Comments
 (0)