Skip to content

Commit 213c0eb

Browse files
committed
Added missing methods:
Connection, enabled headers DocumentHandler, added head and fixed get EdgeHandler, chaned calls to not use inherited calls from documentHandler GraphHandler, added getVertices and getEdges
1 parent c4755de commit 213c0eb

File tree

9 files changed

+969
-31
lines changed

9 files changed

+969
-31
lines changed

lib/triagens/ArangoDb/Connection.php

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -137,12 +137,13 @@ public function getOption($name)
137137
* @throws Exception
138138
*
139139
* @param string $url - GET URL
140+
* @param array $customerHeader
140141
*
141142
* @return HttpResponse
142143
*/
143-
public function get($url)
144+
public function get($url, $customerHeader = array())
144145
{
145-
$response = $this->executeRequest(HttpHelper::METHOD_GET, $url, '');
146+
$response = $this->executeRequest(HttpHelper::METHOD_GET, $url, '', $customerHeader);
146147

147148
return $this->parseResponse($response);
148149
}
@@ -187,12 +188,13 @@ public function put($url, $data)
187188
* @throws Exception
188189
*
189190
* @param string $url - PUT URL
191+
* @param array $customerHeader
190192
*
191193
* @return HttpResponse
192194
*/
193-
public function head($url)
195+
public function head($url, $customerHeader=array())
194196
{
195-
$response = $this->executeRequest(HttpHelper::METHOD_HEAD, $url, '');
197+
$response = $this->executeRequest(HttpHelper::METHOD_HEAD, $url, '', $customerHeader );
196198

197199
return $this->parseResponse($response);
198200
}
@@ -320,10 +322,11 @@ public function parseResponse(HttpResponse $response)
320322
* @param string $method - HTTP request method
321323
* @param string $url - HTTP URL
322324
* @param string $data - data to post in body
325+
* @param array $customerHeader - any arry containing header elements
323326
*
324327
* @return HttpResponse
325328
*/
326-
private function executeRequest($method, $url, $data)
329+
private function executeRequest($method, $url, $data, $customerHeader = array())
327330
{
328331
HttpHelper::validateMethod($method);
329332
$database = $this->getDatabase();
@@ -339,10 +342,10 @@ private function executeRequest($method, $url, $data)
339342

340343
if ($this->_captureBatch === true) {
341344
$this->_options->offsetSet(ConnectionOptions::OPTION_BATCHPART, true);
342-
$request = HttpHelper::buildRequest($this->_options, $method, $url, $data);
345+
$request = HttpHelper::buildRequest($this->_options, $method, $url, $data, $customerHeader);
343346
$this->_options->offsetSet(ConnectionOptions::OPTION_BATCHPART, false);
344347
} else {
345-
$request = HttpHelper::buildRequest($this->_options, $method, $url, $data);
348+
$request = HttpHelper::buildRequest($this->_options, $method, $url, $data, $customerHeader);
346349
}
347350

348351
if ($this->_captureBatch === true) {
@@ -356,7 +359,7 @@ private function executeRequest($method, $url, $data)
356359

357360
$this->_options->offsetSet(ConnectionOptions::OPTION_BATCH, true);
358361

359-
$request = HttpHelper::buildRequest($this->_options, $method, $url, $data);
362+
$request = HttpHelper::buildRequest($this->_options, $method, $url, $data, $customerHeader);
360363
$this->_options->offsetSet(ConnectionOptions::OPTION_BATCH, false);
361364
}
362365

lib/triagens/ArangoDb/DocumentHandler.php

Lines changed: 31 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,7 @@ class DocumentHandler extends
5959
* <li>'_ignoreHiddenAttributes' - true to show hidden attributes. Defaults to false</li>
6060
* <li>'ignoreHiddenAttributes' - Deprecated, please use '_ignoreHiddenAttributes'.</li>
6161
* <li>'revision' - the documents revision</li>
62+
* <li>'ifMatch' - boolean if given revision should match or not</li>
6263
* </p>
6364
*
6465
* @return Document - the document fetched from the server
@@ -72,7 +73,7 @@ public function get($collectionId, $documentId, array $options = array())
7273
/**
7374
* Get a single document from a collection
7475
*
75-
* This will throw if the document cannot be fetched from the server
76+
* This will throw if the document cannot be fetched from the server.
7677
*
7778
* @throws Exception
7879
*
@@ -84,20 +85,29 @@ public function get($collectionId, $documentId, array $options = array())
8485
* <li>'includeInternals' - Deprecated, please use '_includeInternals'.</li>
8586
* <li>'_ignoreHiddenAttributes' - true to show hidden attributes. Defaults to false</li>
8687
* <li>'ignoreHiddenAttributes' - Deprecated, please use '_ignoreHiddenAttributes'.</li>
87-
* <li>'revision' - the documents revision</li>
88+
* <li>'ifMatch' - boolean if given revision should match or not</li>
89+
* <li>'revision' - The document is returned if it matches/not matches revision.</li>
8890
* </p>
8991
*
9092
* @return Document - the document fetched from the server
9193
*/
9294
public function getById($collectionId, $documentId, array $options = array())
9395
{
9496
$url = UrlHelper::buildUrl(Urls::URL_DOCUMENT, array($collectionId, $documentId));
95-
if (array_key_exists("revision", $options)) {
96-
$url = UrlHelper::appendParamsUrl($url, array('rev' => $options["revision"]));
97-
unset($options["revision"]);
97+
$headerElements = array();
98+
if (array_key_exists("ifMatch", $options) && array_key_exists("revision", $options)) {
99+
if ($options["ifMatch"] === true) {
100+
$headerElements["If-Match"] = '"' . $options["revision"] .'"';
101+
} else {
102+
$headerElements["If-None-Match"] = '"' . $options["revision"]. '"';
103+
}
98104
}
99105

100-
$response = $this->getConnection()->get($url);
106+
$response = $this->getConnection()->get($url, $headerElements);
107+
108+
if ($response->getHttpCode() === 304) {
109+
throw new ClientException('Document has not changed.');
110+
}
101111

102112
$data = $response->getJson();
103113

@@ -117,20 +127,27 @@ public function getById($collectionId, $documentId, array $options = array())
117127
*
118128
* @param mixed $collectionId - collection id as a string or number
119129
* @param mixed $documentId - document identifier
120-
* @param string $revision - optional,a certain revision
130+
* @param ifMatch' - boolean if given revision should match or not</li>
131+
* @param revision' - The document is returned if it matches/not matches revision.</li>
121132
*
122-
* @return array - an array containing the key "etag".
133+
* @return array - an array containing the complete header including the key httpCode.
123134
*/
124-
public function getHead($collectionId, $documentId, $revision = null)
135+
public function getHead($collectionId, $documentId, $revision = null, $ifMatch = null)
125136
{
126137
$url = UrlHelper::buildUrl(Urls::URL_DOCUMENT, array($collectionId, $documentId));
127-
if ($revision === null) {
128-
$url = UrlHelper::appendParamsUrl($url, array('rev' => $revision));
138+
$headerElements = array();
139+
if ($revision != null && $ifMatch !== null) {
140+
if ($ifMatch) {
141+
$headerElements["If-Match"] = '"' . $revision .'"';
142+
} else {
143+
$headerElements["If-None-Match"] = '"' . $revision . '"';
144+
}
129145
}
130146

131-
$response = $this->getConnection()->head($url);
132-
133-
return $response->getHeaders();
147+
$response = $this->getConnection()->head($url, $headerElements);
148+
$headers = $response->getHeaders();
149+
$headers["httpCode"] = $response->getHttpCode();
150+
return $headers;
134151
}
135152

136153
/**

lib/triagens/ArangoDb/EdgeHandler.php

Lines changed: 235 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -245,4 +245,239 @@ public function outEdges($collectionId, $vertexHandle)
245245
{
246246
return $this->edges($collectionId, $vertexHandle, 'out');
247247
}
248+
249+
/**
250+
* Get a single edge from a collection
251+
*
252+
* This will throw if the edge cannot be fetched from the server.
253+
*
254+
* @throws Exception
255+
*
256+
* @param mixed $collectionId - collection id as a string or number
257+
* @param mixed $edgeId - document identifier
258+
* @param array $options - optional, array of options
259+
* <p>Options are :
260+
* <li>'_includeInternals' - true to include the internal attributes. Defaults to false</li>
261+
* <li>'includeInternals' - Deprecated, please use '_includeInternals'.</li>
262+
* <li>'_ignoreHiddenAttributes' - true to show hidden attributes. Defaults to false</li>
263+
* <li>'ignoreHiddenAttributes' - Deprecated, please use '_ignoreHiddenAttributes'.</li>
264+
* <li>'ifMatch' - boolean if given revision should match or not</li>
265+
* <li>'revision' - The document is returned if it matches/not matches revision.</li>
266+
* </p>
267+
*
268+
* @return edge - the edge fetched from the server
269+
*/
270+
public function getById($collectionId, $edgeId, array $options = array())
271+
{
272+
$url = UrlHelper::buildUrl(Urls::URL_EDGE, array($collectionId, $edgeId));
273+
$headerElements = array();
274+
if (array_key_exists("ifMatch", $options) && array_key_exists("revision", $options)) {
275+
if ($options["ifMatch"] === true) {
276+
$headerElements["If-Match"] = '"' . $options["revision"] .'"';
277+
} else {
278+
$headerElements["If-None-Match"] = '"' . $options["revision"]. '"';
279+
}
280+
}
281+
282+
$response = $this->getConnection()->get($url, $headerElements);
283+
284+
if ($response->getHttpCode() === 304) {
285+
throw new ClientException('Edge has not changed.');
286+
}
287+
288+
$data = $response->getJson();
289+
290+
$options['_isNew'] = false;
291+
return $this->createFromArrayWithContext($data, $options);
292+
}
293+
294+
295+
/**
296+
* Gets information about a single edge from a collection
297+
*
298+
* This will throw if the edge cannot be fetched from the server
299+
*
300+
*
301+
* @throws Exception
302+
*
303+
* @param mixed $collectionId - collection id as a string or number
304+
* @param mixed $edgeId - document identifier
305+
* @param ifMatch' - boolean if given revision should match or not</li>
306+
* @param revision' - The edge is returned if it matches/not matches revision.</li>
307+
*
308+
* @return array - an array containing the complete header including the key httpCode.
309+
*/
310+
public function getHead($collectionId, $edgeId, $revision = null, $ifMatch = null)
311+
{
312+
$url = UrlHelper::buildUrl(Urls::URL_EDGE, array($collectionId, $edgeId));
313+
$headerElements = array();
314+
if ($revision != null && $ifMatch !== null) {
315+
if ($ifMatch) {
316+
$headerElements["If-Match"] = '"' . $revision .'"';
317+
} else {
318+
$headerElements["If-None-Match"] = '"' . $revision . '"';
319+
}
320+
}
321+
322+
$response = $this->getConnection()->head($url, $headerElements);
323+
$headers = $response->getHeaders();
324+
$headers["httpCode"] = $response->getHttpCode();
325+
return $headers;
326+
}
327+
328+
/**
329+
* Remove an edge from a collection, identified by the collection id and edge id
330+
*
331+
* @throws |Exception
332+
*
333+
* @param mixed $collectionId - collection id as string or number
334+
* @param mixed $edgeId - document id as string or number
335+
* @param mixed $revision - optional revision of the document to be deleted
336+
* @param mixed $options - optional, array of options (see below) or the boolean value for $policy (for compatibility prior to version 1.1 of this method)
337+
* <p>Options are :
338+
* <li>'policy' - update policy to be used in case of conflict ('error', 'last' or NULL [use default])</li>
339+
* <li>'waitForSync' - can be used to force synchronisation of the document removal operation to disk even in case that the waitForSync flag had been disabled for the entire collection</li>
340+
* </p>
341+
*
342+
* @return bool - always true, will throw if there is an error
343+
*/
344+
public function removeById($collectionId, $edgeId, $revision = null, $options = array())
345+
{
346+
// This preserves compatibility for the old policy parameter.
347+
$params = array();
348+
$params = $this->validateAndIncludeOldSingleParameterInParams(
349+
$options,
350+
$params,
351+
ConnectionOptions::OPTION_DELETE_POLICY
352+
);
353+
$params = $this->includeOptionsInParams(
354+
$options,
355+
$params,
356+
array('waitForSync' => ConnectionOptions::OPTION_WAIT_SYNC)
357+
);
358+
359+
if (!is_null($revision)) {
360+
$params[ConnectionOptions::OPTION_REVISION] = $revision;
361+
}
362+
363+
$url = UrlHelper::buildUrl(Urls::URL_EDGE, array($collectionId, $edgeId));
364+
$url = UrlHelper::appendParamsUrl($url, $params);
365+
$this->getConnection()->delete($url);
366+
367+
return true;
368+
}
369+
370+
/**
371+
* Replace an existing edge in a collection, identified by collection id and edge id
372+
*
373+
* This will update the edge on the server
374+
*
375+
* This will throw if the edge cannot be Replaced
376+
*
377+
* If policy is set to error (locally or globally through the ConnectionOptions)
378+
* and the passed edge has a _rev value set, the database will check
379+
* that the revision of the to-be-replaced edge is the same as the one given.
380+
*
381+
* @throws Exception
382+
*
383+
* @param mixed $collectionId - collection id as string or number
384+
* @param mixed $edgeId - edge id as string or number
385+
* @param Edge $edge - edge to be updated
386+
* @param mixed $options - optional, array of options (see below) or the boolean value for $policy (for compatibility prior to version 1.1 of this method)
387+
* <p>Options are :
388+
* <li>'policy' - update policy to be used in case of conflict ('error', 'last' or NULL [use default])</li>
389+
* <li>'waitForSync' - can be used to force synchronisation of the edge replacement operation to disk even in case that the waitForSync flag had been disabled for the entire collection</li>
390+
* </p>
391+
*
392+
* @return bool - always true, will throw if there is an error
393+
*/
394+
public function replaceById($collectionId, $edgeId, Edge $edge, $options = array())
395+
{
396+
// This preserves compatibility for the old policy parameter.
397+
$params = array();
398+
$params = $this->validateAndIncludeOldSingleParameterInParams(
399+
$options,
400+
$params,
401+
ConnectionOptions::OPTION_REPLACE_POLICY
402+
);
403+
$params = $this->includeOptionsInParams(
404+
$options,
405+
$params,
406+
array('waitForSync' => ConnectionOptions::OPTION_WAIT_SYNC)
407+
);
408+
409+
$revision = $edge->getRevision();
410+
if (!is_null($revision)) {
411+
$params[ConnectionOptions::OPTION_REVISION] = $revision;
412+
}
413+
414+
$data = $edge->getAll();
415+
$url = UrlHelper::buildUrl(Urls::URL_EDGE, array($collectionId, $edgeId));
416+
$url = UrlHelper::appendParamsUrl($url, $params);
417+
$result = $this->getConnection()->put($url, $this->json_encode_wrapper($data));
418+
$json = $result->getJson();
419+
$edge->setRevision($json[Edge::ENTRY_REV]);
420+
421+
return true;
422+
}
423+
424+
425+
/**
426+
* Update an existing edge in a collection, identified by collection id and edge id
427+
* Attention - The behavior of this method has changed since version 1.1
428+
*
429+
* This will update the edge on the server
430+
*
431+
* This will throw if the edge cannot be updated
432+
*
433+
* If policy is set to error (locally or globally through the ConnectionOptions)
434+
* and the passed edge has a _rev value set, the database will check
435+
* that the revision of the edge to-be-updated is the same as the one given.
436+
*
437+
* @throws Exception
438+
*
439+
* @param mixed $collectionId - collection id as string or number
440+
* @param mixed $edgeId - edge id as string or number
441+
* @param Edge $edge - patch edge which contains the attributes and values to be updated
442+
* @param mixed $options - optional, array of options (see below) or the boolean value for $policy (for compatibility prior to version 1.1 of this method)
443+
* <p>Options are :
444+
* <li>'policy' - update policy to be used in case of conflict ('error', 'last' or NULL [use default])</li>
445+
* <li>'keepNull' - can be used to instruct ArangoDB to delete existing attributes instead setting their values to null. Defaults to true (keep attributes when set to null)</li>
446+
* <li>'waitForSync' - can be used to force synchronisation of the edge update operation to disk even in case that the waitForSync flag had been disabled for the entire collection</li>
447+
* </p>
448+
*
449+
* @return bool - always true, will throw if there is an error
450+
*/
451+
public function updateById($collectionId, $edgeId, Edge $edge, $options = array())
452+
{
453+
// This preserves compatibility for the old policy parameter.
454+
$params = array();
455+
$params = $this->validateAndIncludeOldSingleParameterInParams(
456+
$options,
457+
$params,
458+
ConnectionOptions::OPTION_UPDATE_POLICY
459+
);
460+
$params = $this->includeOptionsInParams(
461+
$options,
462+
$params,
463+
array(
464+
'waitForSync' => $this->getConnectionOption(ConnectionOptions::OPTION_WAIT_SYNC),
465+
'keepNull' => true,
466+
)
467+
);
468+
469+
$revision = $edge->getRevision();
470+
if (!is_null($revision)) {
471+
$params[ConnectionOptions::OPTION_REVISION] = $revision;
472+
}
473+
474+
$url = UrlHelper::buildUrl(Urls::URL_EDGE, array($collectionId, $edgeId));
475+
$url = UrlHelper::appendParamsUrl($url, $params);
476+
$result = $this->getConnection()->patch($url, $this->json_encode_wrapper($edge->getAll()));
477+
$json = $result->getJson();
478+
$edge->setRevision($json[Edge::ENTRY_REV]);
479+
480+
return true;
481+
}
482+
248483
}

0 commit comments

Comments
 (0)