Exemple #1
0
 /**
  * Get the neighbour vertices connected to this vertex via some edge. The vertices and their connecting edges can be filtered by AQL.
  * @param Document|AModel|string A vertex pod, model or string of the vertex id.
  * @param  string       $direction   "in" for inbound neighbours, "out" for outbound neighbours and "any" for all neighbours.
  * @param  string|array $labels      A string representing one label or an array of labels we want the inbound edges to have.
  * @param  string       $aql         An optional AQL fragment if we want to filter the edges or vertices, for example:
  *                                   FILTER doc.edge.someproperty == "somevalue" && doc.vertex.anotherproperty == "anothervalue"
  * @param  array        $params      An optional associative array containing parameters to bind to the query.
  * @param  string       $placeholder Set this to something else if you do not wish to use "doc" to refer to documents in your query.
  * @return array
  */
 public function getNeighbours($model, $direction = "any", $labels = null, $aql = "", $params = array(), $placeholder = "doc")
 {
     $id = $this->getVertexId($model);
     if (!$id) {
         return array();
     }
     if (strtolower($direction) == "in") {
         $direction = "inbound";
     }
     if (strtolower($direction) == "out") {
         $direction = "outbound";
     }
     if ($labels && !is_array($labels)) {
         $labels = array($labels);
     }
     $vertexCollection = $this->_toolbox->generateBindingParameter('@vertexCollection', $params);
     $edgeCollection = $this->_toolbox->generateBindingParameter('@edgeCollection', $params);
     $vertexParameter = $this->_toolbox->generateBindingParameter('vertexid', $params);
     $directionParameter = $this->_toolbox->generateBindingParameter('direction', $params);
     if (!$labels) {
         $query = "FOR {$placeholder} in NEIGHBORS(@{$vertexCollection}, @{$edgeCollection}, @{$vertexParameter}, @{$directionParameter}) " . $aql . " return {$placeholder}.vertex";
     } else {
         $count = 0;
         $labelString = '';
         foreach ($labels as $label) {
             $labelParameter = $this->_toolbox->generateBindingParameter('label', $params);
             if ($count > 0) {
                 $labelString .= ", ";
             }
             $labelString .= "{'\$label': @{$labelParameter}}";
             $params[$labelParameter] = $label;
             $count++;
         }
         $query = "FOR {$placeholder} in NEIGHBORS(@{$vertexCollection}, @{$edgeCollection}, @{$vertexParameter}, @{$directionParameter}, [{$labelString}]) " . $aql . " return {$placeholder}.vertex";
     }
     $params[$vertexCollection] = $this->_toolbox->getVertexCollectionName();
     $params[$edgeCollection] = $this->_toolbox->getEdgeCollectionName();
     $params[$vertexParameter] = $id;
     $params[$directionParameter] = $direction;
     if ($this->_toolbox->getTransactionManager()->hasTransaction()) {
         $this->_toolbox->getTransactionManager()->addReadCollection($this->_toolbox->getEdgeCollectionName());
         $this->_toolbox->getTransactionManager()->addReadCollection($this->_toolbox->getVertexCollectionName());
         $statement = json_encode(array('query' => $query, 'bindVars' => $params), JSON_FORCE_OBJECT);
         $this->_toolbox->getTransactionManager()->addCommand("db._createStatement({$statement}).execute().elements();", "GraphManager:getNeighbours", null, true);
     } else {
         try {
             $result = $this->_toolbox->getQuery()->getAll($query, $params);
         } catch (\Exception $e) {
             throw new GraphManagerException($e->getMessage(), $e->getCode());
         }
         if (empty($result)) {
             return array();
         }
         return $this->convertToPods("vertex", $result);
     }
 }
Exemple #2
0
 /**
  * @covers Paradox\Toolbox::getEdgeCollectionName
  */
 public function testGetEdgeCollectionNameOnNonGraphToolbox()
 {
     try {
         $this->toolbox->getEdgeCollectionName();
     } catch (\Exception $e) {
         $this->assertInstanceOf('Paradox\\exceptions\\ToolboxException', $e, 'Exception thrown was not a Paradox\\exceptions\\ToolboxException');
         return;
     }
     $this->fail('Calling getEdgeCollectionName() on a non-graph toolbox did not throw an exception');
 }
Exemple #3
0
 /**
  * Determines the collection name to use. For graphs, it converts "vertex" and "edge" into the appropriate collection names on the server.
  * @param  string          $type The collection name.
  * @throws FinderException
  * @return string
  */
 private function getCollectionName($type)
 {
     if ($this->_toolbox->isGraph()) {
         if (!$this->_toolbox->getPodManager()->validateType($type)) {
             throw new FinderException("When finding documents in graphs, only the types 'vertex' and 'edge' are allowed.");
         }
         //Is a vertex
         if (strtolower($type) == "vertex") {
             return $this->_toolbox->getVertexCollectionName();
             //Is an edge
         } else {
             return $this->_toolbox->getEdgeCollectionName();
         }
     } else {
         return $type;
     }
 }