/** * Get the batch part identified by the array key (0...n) or its id (if it was set with nextBatchPartId($id) ) * * @throws ClientException * @return mixed $partId */ public function getProcessedResponse() { $response = $this->getResponse(); switch ($this->_type) { case 'getdocument': $json = $response->getJson(); $options = $this->getCursorOptions(); $options['isNew'] = false; $response = Document::createFromArray($json, $options); break; case 'document': $json = $response->getJson(); if ($json['error'] === false) { $id = $json[Document::ENTRY_ID]; $response = $id; } break; case 'getedge': $json = $response->getJson(); $options = $this->getCursorOptions(); $options['isNew'] = false; $response = Edge::createFromArray($json, $options); break; case 'edge': $json = $response->getJson(); if ($json['error'] === false) { $id = $json[Edge::ENTRY_ID]; $response = $id; } break; case 'getcollection': $json = $response->getJson(); $options = $this->getCursorOptions(); $options['isNew'] = false; $response = Collection::createFromArray($json, $options); break; case 'collection': $json = $response->getJson(); if ($json['error'] === false) { $id = $json[Collection::ENTRY_ID]; $response = $id; } break; case 'cursor': $options = $this->getCursorOptions(); $options['isNew'] = false; $response = new Cursor($this->_batch->getConnection(), $response->getJson(), $options); break; default: throw new ClientException('Could not determine response data type.'); break; } return $response; }
/** * Get a single edge from a graph * * This will throw if the edge cannot be fetched from the server<br><br> * * @throws Exception * * @param mixed $graph - graph name as a string or instance of Graph * @param mixed $edgeId - edge identifier * @param array $options optional, array of options * <p> * <li><b>_includeInternals</b> - true to include the internal attributes. Defaults to false</li> * <li><b>includeInternals</b> - Deprecated, please use '_includeInternals'.</li> * <li><b>_ignoreHiddenAttributes</b> - true to show hidden attributes. Defaults to false</li> * <li><b>ignoreHiddenAttributes</b> - Deprecated, please use '_ignoreHiddenAttributes'.</li> * </p> * @param string $collection - if one uses a graph with more than one vertex collection one must provide the collection * * @return Document - the edge document fetched from the server * @since 1.2 */ public function getEdge($graph, $edgeId, array $options = array(), $collection = null) { if ($graph instanceof Graph) { $graph = $graph->getKey(); } $parts = explode("/", $edgeId); if (count($parts) === 2) { $edgeId = $parts[1]; $collection = $parts[0]; } if (count($this->getEdgeCollections($graph)) !== 1 && $collection === null) { throw new ClientException('A collection must be provided.'); } else { if (count($this->getEdgeCollections($graph)) === 1 && $collection === null) { $collection = $this->getEdgeCollections($graph); $collection = $collection[0]; } } $url = UrlHelper::buildUrl(Urls::URL_GRAPH, array($graph, Urls::URLPART_EDGE, $collection, $edgeId)); $response = $this->getConnection()->get($url); $jsonArray = $response->getJson(); $edge = $jsonArray['edge']; $options['_isNew'] = false; return Edge::createFromArray($edge, $options); }
/** * Create an array of Edges from the input array * * @param array $data - array of incoming "edge" arrays * * @return void */ private function addEdgesFromArray(array $data) { $this->_result[] = Edge::createFromArray($data, $this->_options); }
/** * Tests if the updateEdge method accepts an instance of Graph as first argument and extracts the graph name out of it. */ public function testUpdateEdgeWithGraphInstance() { $this->createGraph(); $result = $this->graphHandler->updateEdge($this->graph, $this->edge1Name, '', Edge::createFromArray(array('_key' => 'foobar'))); $this->assertTrue($result); }
/** * Create an array of results from the input array * * @param array $data - incoming result * * @return void */ private function setData(array $data) { if (isset($this->_options[self::ENTRY_FLAT]) && $this->_options[self::ENTRY_FLAT]) { $this->_result = $data; } else { $this->_result = array(); if ($this->_options[self::ENTRY_TYPE] === Collection::TYPE_EDGE) { foreach ($data as $row) { $this->_result[] = Edge::createFromArray($row, $this->_options); } } else { foreach ($data as $row) { $this->_result[] = Document::createFromArray($row, $this->_options); } } } }
/** * test for replacing a edge using replace() with wrong encoding * We expect an exception here: * * @expectedException \triagens\ArangoDb\ClientException */ public function testReplaceEdgeWithWrongEncoding() { $edgeHandler = $this->edgeHandler; $edge = Edge::createFromArray(array('someAttribute' => 'someValue', 'someOtherAttribute' => 'someOtherValue')); $edgeId = $edgeHandler->add($this->collection->getId(), $edge); $this->assertTrue(is_numeric($edgeId), 'Did not return an id!'); // inject wrong encoding $isoKey = iconv("UTF-8", "ISO-8859-1//TRANSLIT", "someWrongEncododedAttribute"); $isoValue = iconv("UTF-8", "ISO-8859-1//TRANSLIT", "someWrongEncodedValueü"); $edge->set($isoKey, $isoValue); $edge->set('someOtherAttribute', 'someOtherValue2'); $result = $edgeHandler->replace($edge); $this->assertTrue($result); $resultingEdge = $edgeHandler->get($this->collection->getId(), $edgeId); $this->assertObjectHasAttribute('_id', $resultingEdge, '_id field should exist, empty or with an id'); $this->assertTrue($resultingEdge->someAttribute == 'someValue2', 'Should be :someValue2, is: ' . $resultingEdge->someAttribute); $this->assertTrue($resultingEdge->someOtherAttribute == 'someOtherValue2', 'Should be :someOtherValue2, is: ' . $resultingEdge->someOtherAttribute); $response = $edgeHandler->delete($resultingEdge); $this->assertTrue($response, 'Delete should return true!'); }
$graph->setVerticesCollection('VertexCollection'); $graph->setEdgesCollection('EdgeCollection'); try { $graphHandler->dropGraph($graph); } catch (\Exception $e) { // graph may not yet exist. ignore this error for now } $graphHandler->createGraph($graph); // Define some arrays to build the content of the vertices and edges $vertex1Array = array('_key' => 'vertex1', 'someKey1' => 'someValue1'); $vertex2Array = array('_key' => 'vertex2', 'someKey2' => 'someValue2'); $edge1Array = array('_key' => 'edge1', 'someEdgeKey1' => 'someEdgeValue1'); // Create documents for 2 vertices and a connecting edge $vertex1 = Vertex::createFromArray($vertex1Array); $vertex2 = Vertex::createFromArray($vertex2Array); $edge1 = Edge::createFromArray($edge1Array); // Save the vertices $saveResult1 = $graphHandler->saveVertex('Graph', $vertex1); $saveResult2 = $graphHandler->saveVertex('Graph', $vertex2); // Get the vertices $getResult1 = $graphHandler->getVertex('Graph', 'vertex1'); $getResult2 = $graphHandler->getVertex('Graph', 'vertex2'); // check if vertex exists var_dump($graphHandler->hasVertex('Graph', 'vertex1')); // Save the connecting edge $saveEdgeResult1 = $graphHandler->saveEdge('Graph', $vertex1->getHandle(), $vertex2->getHandle(), 'somelabelValue', $edge1); // check if edge exists var_dump($graphHandler->hasEdge('Graph', 'edge1')); // Get the connecting edge $getEdgeResult1 = $graphHandler->getEdge('Graph', 'edge1'); // Remove vertices and edges
/** */ public function testsaveGetUpdateReplaceRemoveEdge() { $edge1 = Edge::createFromArray($this->edge1Array); $ex = null; try { $this->graphHandler->saveEdge($this->graphName, $this->v1 . "/" . $this->vertex1Array["_key"], $this->v1 . "/" . $this->vertex1Array["_key"], null, $edge1); } catch (Exception $e) { $ex = $e->getMessage(); } $this->assertTrue($ex == 'A collection must be provided.'); $this->createGraph(); $this->graphHandler->saveEdge($this->graphName, $this->v1 . "/" . $this->vertex1Array["_key"], $this->v4 . "/" . $this->vertex4Array["_key"], null, array(), $this->e1); $ex = null; try { $this->graphHandler->getEdge($this->graphName, $this->edge1Array["_key"]); } catch (Exception $e) { $ex = $e->getMessage(); } $this->assertTrue($ex == 'A collection must be provided.'); $v1 = $this->graphHandler->getEdge($this->graphName, $this->edge1Array["_key"], array(), $this->e1); $v2 = $this->graphHandler->getEdge($this->graphName, $this->e1 . "/" . $this->edge1Array["_key"], array()); $this->assertTrue($v1->getInternalKey() == $v2->getInternalKey()); $v1 = $this->graphHandler->getEdge($this->graphName, $this->e1 . "/" . $this->edge1Array["_key"], array()); $v = Edge::createFromArray($this->edge1Array); $v->setRevision($v1->getRevision()); $ex = null; try { $this->graphHandler->replaceEdge($this->graphName, $this->edge1Array["_key"], null, $v, array()); } catch (Exception $e) { $ex = $e->getMessage(); } $this->assertTrue($ex == 'A collection must be provided.'); $v1 = $this->graphHandler->getEdge($this->graphName, $this->e1 . "/" . $this->edge1Array["_key"], array()); $v = Edge::createFromArray($this->edge1Array); $v->setRevision($v1->getRevision()); $this->assertTrue($this->graphHandler->replaceEdge($this->graphName, $this->edge1Array["_key"], null, $v, array('revision' => $v1->getRevision()), $this->e1)); $v1 = $this->graphHandler->getEdge($this->graphName, $this->e1 . "/" . $this->edge1Array["_key"], array()); $v = Edge::createFromArray($this->edge1Array); $v->setRevision($v1->getRevision()); $this->assertTrue($this->graphHandler->replaceEdge($this->graphName, $this->e1 . "/" . $this->edge1Array["_key"], null, $v, array('revision' => true))); $ex = null; try { $this->graphHandler->updateEdge($this->graphName, $this->edge1Array["_key"], null, $v, array()); } catch (Exception $e) { $ex = $e->getMessage(); } $this->assertTrue($ex == 'A collection must be provided.'); $v1 = $this->graphHandler->getEdge($this->graphName, $this->e1 . "/" . $this->edge1Array["_key"], array()); $v = Edge::createFromArray($this->edge1Array); $v->setRevision($v1->getRevision()); $this->assertTrue($this->graphHandler->updateEdge($this->graphName, $this->edge1Array["_key"], null, $v, array('revision' => true), $this->e1)); $v1 = $this->graphHandler->getEdge($this->graphName, $this->e1 . "/" . $this->edge1Array["_key"], array()); $v = Edge::createFromArray($this->edge1Array); $v->setRevision($v1->getRevision()); $this->assertTrue($this->graphHandler->updateEdge($this->graphName, $this->e1 . "/" . $this->edge1Array["_key"], null, $v, array('revision' => $v1->getRevision()))); //removeVertex($graph, $vertexId, $revision = null, $options = array(), $collection = null) $ex = null; try { $this->graphHandler->removeEdge($this->graphName, $this->edge1Array["_key"], null, array()); } catch (Exception $e) { $ex = $e->getMessage(); } $this->assertTrue($ex == 'A collection must be provided.'); $v1 = $this->graphHandler->getVertex($this->graphName, $this->e1 . "/" . $this->edge1Array["_key"], array()); $this->assertTrue($this->graphHandler->removeEdge($this->graphName, $this->e1 . "/" . $this->edge1Array["_key"], $v1->getRevision(), array())); }
public function createGraph() { $vertex1 = Vertex::createFromArray($this->vertex1Array); $vertex2 = Vertex::createFromArray($this->vertex2Array); $vertex3 = Vertex::createFromArray($this->vertex3Array); $vertex4 = Vertex::createFromArray($this->vertex4Array); $vertex5 = Vertex::createFromArray($this->vertex5Array); $edge1 = Edge::createFromArray($this->edge1Array); $edge2 = Edge::createFromArray($this->edge2Array); $edge3 = Edge::createFromArray($this->edge3Array); $edge4 = Edge::createFromArray($this->edge4Array); $edge5 = Edge::createFromArray($this->edge5Array); $this->graphHandler->saveVertex($this->graphName, $vertex1); $this->graphHandler->saveVertex($this->graphName, $vertex2); $this->graphHandler->saveVertex($this->graphName, $vertex3); $this->graphHandler->saveVertex($this->graphName, $vertex4); $this->graphHandler->saveVertex($this->graphName, $vertex5); $this->graphHandler->getVertex($this->graphName, $this->vertex1Name); $this->graphHandler->getVertex($this->graphName, $this->vertex2Name); $this->graphHandler->getVertex($this->graphName, $this->vertex3Name); $this->graphHandler->getVertex($this->graphName, $this->vertex4Name); $this->graphHandler->getVertex($this->graphName, $this->vertex5Name); $this->graphHandler->saveEdge($this->graphName, $this->vertexCollectionName . "/" . $this->vertex1Name, $this->vertexCollectionName . "/" . $this->vertex2Name, $this->edgeLabel1, $edge1); $this->graphHandler->saveEdge($this->graphName, $this->vertexCollectionName . "/" . $this->vertex2Name, $this->vertexCollectionName . "/" . $this->vertex3Name, $this->edgeLabel2, $edge2); $this->graphHandler->saveEdge($this->graphName, $this->vertexCollectionName . "/" . $this->vertex2Name, $this->vertexCollectionName . "/" . $this->vertex4Name, $this->edgeLabel3, $edge3); $this->graphHandler->saveEdge($this->graphName, $this->vertexCollectionName . "/" . $this->vertex5Name, $this->vertexCollectionName . "/" . $this->vertex1Name, $this->edgeLabel4, $edge4); $this->graphHandler->saveEdge($this->graphName, $this->vertexCollectionName . "/" . $this->vertex5Name, $this->vertexCollectionName . "/" . $this->vertex2Name, $this->edgeLabel5, $edge5); }
} catch (\Exception $e) { // collection may already exist - ignore this error for now } // create a new department $marketing = Document::createFromArray(array("name" => "Marketing")); $documentHandler->save("departments", $marketing); // create another department $finance = Document::createFromArray(array("name" => "Finance")); $documentHandler->save("departments", $finance); // create a new employee $john = Document::createFromArray(array("name" => "John")); $documentHandler->save("employees", $john); // create another employee $jane = Document::createFromArray(array("name" => "Jane")); $documentHandler->save("employees", $jane); // now insert a link between Marketing and Jane $worksFor = Edge::createFromArray(array("startDate" => "2009-06-23", "endDate" => "2014-11-12")); $edgeHandler->saveEdge("worksFor", $marketing->getHandle(), $jane->getHandle(), $worksFor); // now insert a link between Finance and Jane $worksFor = Edge::createFromArray(array("startDate" => "2014-11-12")); $edgeHandler->saveEdge("worksFor", $finance->getHandle(), $jane->getHandle(), $worksFor); // now insert a link between Finance and John $worksFor = Edge::createFromArray(array("startDate" => "2012-04-01")); $edgeHandler->saveEdge("worksFor", $finance->getHandle(), $john->getHandle(), $worksFor); } catch (ConnectException $e) { print $e . PHP_EOL; } catch (ServerException $e) { print $e . PHP_EOL; } catch (ClientException $e) { print $e . PHP_EOL; }
/** * save an edge to an edge-collection * * This will save the edge to the collection and return the edges-document's id * * This will throw if the document cannot be saved * * @throws Exception * * @param mixed $collectionId - collection id as string or number * @param mixed $from - from vertex * @param mixed $to - to vertex * @param mixed $document - the edge-document to be added, can be passed as an object or an array * @param bool|array $options - optional, prior to v1.2.0 this was a boolean value for create. Since v1.0.0 it's an array of options. * <p>Options are :<br> * <li>'create' - create the collection if it does not yet exist.</li> * <li>'waitForSync' - if set to true, then all removal operations will instantly be synchronised to disk.<br> * If this is not specified, then the collection's default sync behavior will be applied.</li> * </p> * * @return mixed - id of document created * @since 1.0 */ public function saveEdge($collectionId, $from, $to, $document, $options = array()) { if ($collectionId instanceof Collection) { $collectionId = $collectionId->getName(); } if (is_array($document)) { $document = Edge::createFromArray($document); } $document->setFrom($from); $document->setTo($to); $params = array(self::OPTION_COLLECTION => $collectionId, self::OPTION_FROM => $document->getFrom(), self::OPTION_TO => $document->getTo()); $params = $this->validateAndIncludeOldSingleParameterInParams($options, $params, ConnectionOptions::OPTION_CREATE); $params = $this->includeOptionsInParams($options, $params, array(ConnectionOptions::OPTION_WAIT_SYNC => $this->getConnectionOption(ConnectionOptions::OPTION_WAIT_SYNC))); $data = $document->getAll(); $url = UrlHelper::appendParamsUrl(Urls::URL_EDGE, $params); $response = $this->getConnection()->post($url, $this->json_encode_wrapper($data)); $location = $response->getLocationHeader(); if (!$location) { throw new ClientException('Did not find location header in server response'); } $json = $response->getJson(); $id = UrlHelper::getDocumentIdFromLocation($location); $document->setInternalId($json[Edge::ENTRY_ID]); $document->setRevision($json[Edge::ENTRY_REV]); if ($id != $document->getId()) { throw new ClientException('Got an invalid response from the server'); } $document->setIsNew(false); return $document->getId(); }