Skip to content

Commit 992d190

Browse files
committed
pr arangodb#170: manual merge of branch olivermack:simple-checker-methods
contributed by @olivermack
1 parent 8cbb3af commit 992d190

11 files changed

+217
-8
lines changed

README.md

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -510,14 +510,17 @@ try {
510510

511511
$collectionHandler = new CollectionHandler($connection);
512512

513-
// create a new document
513+
// create a new collection
514514
$userCollection = new ArangoCollection();
515-
$userCollection->setName('user');
516-
$id = $collectionHandler->add($userCollection);
515+
$userCollection->setName('user');
516+
$id = $collectionHandler->add($userCollection);
517517

518518
// print the collection id created by the server
519519
var_dump($id);
520520

521+
// check if the collection exists
522+
$result = $collectionHandler->has('user');
523+
var_dump($result);
521524

522525
$handler = new ArangoDocumentHandler($connection);
523526

@@ -534,6 +537,10 @@ try {
534537
// send the document to the server
535538
$id = $handler->add('users', $user);
536539

540+
// check if a document exists
541+
$result = $handler->has("users", $id);
542+
var_dump($result);
543+
537544
// print the document id created by the server
538545
var_dump($id);
539546
var_dump($user->getId());

examples/collection.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,10 @@
1414
$result = $handler->add($col);
1515
var_dump($result);
1616

17+
// check if a collection exists
18+
$result = $handler->has("foobar");
19+
var_dump($result);
20+
1721
// get an existing collection
1822
$result = $handler->get("hihi");
1923
var_dump($result);

examples/document.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,10 @@
5050
// delete the document
5151
$result = $handler->deleteById("users", $id);
5252
var_dump($result);
53+
54+
// check if a document exists
55+
$result = $handler->has("users", "foobar123");
56+
var_dump($result);
5357
} catch (ConnectException $e) {
5458
print $e . PHP_EOL;
5559
} catch (ServerException $e) {

examples/graph.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,9 +42,15 @@
4242
$getResult1 = $graphHandler->getVertex('Graph', 'vertex1');
4343
$getResult2 = $graphHandler->getVertex('Graph', 'vertex2');
4444

45+
// check if vertex exists
46+
var_dump($graphHandler->hasVertex('Graph', 'vertex1'));
47+
4548
// Save the connecting edge
4649
$saveEdgeResult1 = $graphHandler->saveEdge('Graph', 'vertex1', 'vertex2', 'somelabelValue', $edge1);
4750

51+
// check if edge exists
52+
var_dump($graphHandler->hasEdge('Graph', 'edge1'));
53+
4854
// Get the connecting edge
4955
$getEdgeResult1 = $graphHandler->getEdge('Graph', 'edge1');
5056

lib/triagens/ArangoDb/CollectionHandler.php

Lines changed: 32 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -247,6 +247,37 @@ public function get($collectionId)
247247
}
248248

249249

250+
/**
251+
* Check if a collection exists
252+
*
253+
* This will call self::get() internally and checks if there
254+
* was an exception thrown which represents an 404 request.
255+
*
256+
* @throws Exception When any other error than a 404 occurs
257+
*
258+
* @param mixed $collectionId - collection id as a string or number
259+
* @return boolean
260+
*/
261+
public function has($collectionId)
262+
{
263+
try {
264+
// will throw ServerException if entry could not be retrieved
265+
$result = $this->get($collectionId);
266+
return true;
267+
} catch (ServerException $e) {
268+
// we are expecting a 404 to return boolean false
269+
if ($e->getCode() === 404) {
270+
return false;
271+
}
272+
273+
// just rethrow
274+
throw $e;
275+
}
276+
277+
return false;
278+
}
279+
280+
250281
/**
251282
* Get properties of a collection
252283
*
@@ -438,7 +469,7 @@ public function create($collection, $options = array())
438469
if ($collection->getNumberOfShards() !== null) {
439470
$params[Collection::ENTRY_NUMBER_OF_SHARDS] = $collection->getNumberOfShards();
440471
}
441-
472+
442473
if (is_array($collection->getShardKeys())) {
443474
$params[Collection::ENTRY_SHARD_KEYS] = $collection->getShardKeys();
444475
}

lib/triagens/ArangoDb/DocumentHandler.php

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,38 @@ public function get($collectionId, $documentId, array $options = array())
7070
}
7171

7272

73+
/**
74+
* Check if a document exists
75+
*
76+
* This will call self::get() internally and checks if there
77+
* was an exception thrown which represents an 404 request.
78+
*
79+
* @throws Exception When any other error than a 404 occurs
80+
*
81+
* @param string $collectionId - collection id as a string or number
82+
* @param mixed $documentId - document identifier
83+
* @return boolean
84+
*/
85+
public function has($collectionId, $documentId)
86+
{
87+
try {
88+
// will throw ServerException if entry could not be retrieved
89+
$result = $this->get($collectionId, $documentId);
90+
return true;
91+
} catch (ServerException $e) {
92+
// we are expecting a 404 to return boolean false
93+
if ($e->getCode() === 404) {
94+
return false;
95+
}
96+
97+
// just rethrow
98+
throw $e;
99+
}
100+
101+
return false;
102+
}
103+
104+
73105
/**
74106
* Get a single document from a collection
75107
*

lib/triagens/ArangoDb/GraphHandler.php

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -625,6 +625,38 @@ public function getVertex($graph, $vertexId, array $options = array(), $collecti
625625
}
626626

627627

628+
/**
629+
* Check if a vertex exists
630+
*
631+
* This will call self::getVertex() internally and checks if there
632+
* was an exception thrown which represents an 404 request.
633+
*
634+
* @throws Exception When any other error than a 404 occurs
635+
*
636+
* @param mixed $graph - graph name as a string or instance of Graph
637+
* @param mixed $vertexId - the vertex identifier
638+
* @return boolean
639+
*/
640+
public function hasVertex($graph, $vertexId)
641+
{
642+
try {
643+
// will throw ServerException if entry could not be retrieved
644+
$result = $this->getVertex($graph, $vertexId);
645+
return true;
646+
} catch (ServerException $e) {
647+
// we are expecting a 404 to return boolean false
648+
if ($e->getCode() === 404) {
649+
return false;
650+
}
651+
652+
// just rethrow
653+
throw $e;
654+
}
655+
656+
return false;
657+
}
658+
659+
628660
/**
629661
* Replace an existing vertex in a graph, identified graph name and vertex id
630662
*
@@ -971,6 +1003,38 @@ public function getEdge($graph, $edgeId, array $options = array(), $collection =
9711003
}
9721004

9731005

1006+
/**
1007+
* Check if an edge exists
1008+
*
1009+
* This will call self::getEdge() internally and checks if there
1010+
* was an exception thrown which represents an 404 request.
1011+
*
1012+
* @throws Exception When any other error than a 404 occurs
1013+
*
1014+
* @param mixed $graph - graph name as a string or instance of Graph
1015+
* @param mixed $edgeId - the vertex identifier
1016+
* @return boolean
1017+
*/
1018+
public function hasEdge($graph, $edgeId)
1019+
{
1020+
try {
1021+
// will throw ServerException if entry could not be retrieved
1022+
$result = $this->getEdge($graph, $edgeId);
1023+
return true;
1024+
} catch (ServerException $e) {
1025+
// we are expecting a 404 to return boolean false
1026+
if ($e->getCode() === 404) {
1027+
return false;
1028+
}
1029+
1030+
// just rethrow
1031+
throw $e;
1032+
}
1033+
1034+
return false;
1035+
}
1036+
1037+
9741038
/**
9751039
* Replace an existing edge in a graph, identified graph name and edge id
9761040
*

tests/CollectionBasicTest.php

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -657,6 +657,15 @@ public function testGetIndex()
657657
$this->assertEquals(100, $indexInfo[CollectionHandler::OPTION_MIN_LENGTH], 'Min length does not match!');
658658
}
659659

660+
public function testHasCollectionReturnsFalseIfCollectionDoesNotExist()
661+
{
662+
$this->assertFalse($this->collectionHandler->has('just_a_stupid_collection_id_which_does_not_exist'));
663+
}
664+
665+
public function testHasCollectionReturnsTrueIfCollectionExists()
666+
{
667+
$this->assertTrue($this->collectionHandler->has('ArangoDB_PHP_TestSuite_IndexTestCollection'));
668+
}
660669

661670
public function tearDown()
662671
{

tests/DocumentBasicTest.php

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -311,6 +311,31 @@ public function testCreateAndDeleteDocumentUsingDefinedKeyWithArrayAndSaveOnly()
311311
}
312312

313313

314+
public function testHasDocumentReturnsFalseIfDocumentDoesNotExist()
315+
{
316+
$connection = $this->connection;
317+
$collection = $this->collection;
318+
$documentHandler = new DocumentHandler($connection);
319+
$this->assertFalse($documentHandler->has($collection->getId(), 'just_a_stupid_document_id_which_does_not_exist'));
320+
}
321+
322+
323+
public function testHasDocumentReturnsTrueIfDocumentExists()
324+
{
325+
$connection = $this->connection;
326+
$collection = $this->collection;
327+
$documentHandler = new DocumentHandler($connection);
328+
329+
// create doc first
330+
$document = new Document();
331+
$document->someAttribute = 'someValue';
332+
333+
$documentId = $documentHandler->add($collection->getId(), $document);
334+
335+
$this->assertTrue($this->collectionHandler->has($collection->getId(), $documentId));
336+
}
337+
338+
314339
public function tearDown()
315340
{
316341
try {

tests/GeneralGraphExtendedTest.php

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -36,14 +36,14 @@ public function setUp()
3636
$param2 = array();
3737
$param2[] = $this->v3;
3838
$param2[] = $this->v4;
39-
$ed1 = EdgeDefinition::createDirectedRelation(
39+
$ed1 = EdgeDefinition::createDirectedRelation(
4040
$this->e1,
4141
$param1,
4242
$param2
43-
);
43+
);
4444
$ed2 = EdgeDefinition::createUndirectedRelation(
4545
$this->e2, $this->v5
46-
);
46+
);
4747
$this->graph = new Graph($this->graphName);
4848
$this->graph->addEdgeDefinition($ed1);
4949
$this->graph->addEdgeDefinition($ed2);
@@ -427,7 +427,7 @@ public function testGetEdges()
427427

428428
$params = array(
429429
"edgeCollectionRestriction" => $this->e1,
430-
"vertexCollectionRestriction" => $this->v3,
430+
"vertexCollectionRestriction" => array($this->v1, $this->v3),
431431
"maxDepth" => "2",
432432
"direction" => "in",
433433
"filter" => array(
@@ -442,6 +442,7 @@ public function testGetEdges()
442442
)
443443
)
444444
)
445+
445446
);
446447

447448
$cursor = $this->graphHandler->getEdges($this->graphName, $params);

0 commit comments

Comments
 (0)