Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
add $_edgeClass in addition to $_documentClass
  • Loading branch information
jsteemann committed Aug 6, 2018
commit c984944c12b36a275009a9c0d6b04171c848da03
23 changes: 5 additions & 18 deletions lib/ArangoDBClient/Batch.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,16 @@
/**
* Provides batching functionality
*
* <br>
*
* @package ArangoDBClient
* @since 1.1
*/
class Batch
{
/**
* Import $_documentClass functionality
*/
use DocumentClassable;

/**
* Batch Response Object
*
Expand Down Expand Up @@ -560,22 +563,6 @@ public function getConnection()
return $this->_connection;
}

/**
* @var string Document class to use
*/
protected $_documentClass = '\ArangoDBClient\Document';

/**
* Sets the document class to use
*
* @param string $class Document class to use
* @return Batch
*/
public function setDocumentClass($class)
{
$this->_documentClass = $class;
return $this;
}
}

class_alias(Batch::class, '\triagens\ArangoDb\Batch');
27 changes: 7 additions & 20 deletions lib/ArangoDBClient/BatchPart.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,15 +14,17 @@
/**
* Provides batch part functionality
*
* <br>
*
* @package ArangoDBClient
* @since 1.1
*/


class BatchPart
{
/**
* Import $_documentClass functionality
*/
use DocumentClassable;


/**
Expand Down Expand Up @@ -243,6 +245,7 @@ public function getHttpCode()
public function getProcessedResponse()
{
$_documentClass = $this->_documentClass;
$_edgeClass = $this->_edgeClass;

$response = $this->getResponse();
switch ($this->_type) {
Expand Down Expand Up @@ -273,7 +276,7 @@ public function getProcessedResponse()
$json = $response->getJson();
$options = $this->getCursorOptions();
$options['_isNew'] = false;
$response = Edge::createFromArray($json, $options);
$response = $_edgeClass::createFromArray($json, $options);
break;
case 'edge':
$json = $response->getJson();
Expand All @@ -288,7 +291,7 @@ public function getProcessedResponse()
$options['_isNew'] = false;
$response = [];
foreach ($json[EdgeHandler::ENTRY_EDGES] as $data) {
$response[] = Edge::createFromArray($data, $options);
$response[] = $_edgeClass::createFromArray($data, $options);
}
break;
case 'getcollection':
Expand Down Expand Up @@ -338,22 +341,6 @@ private function getCursorOptions()
return $this->_cursorOptions;
}

/**
* @var string Document class to use
*/
protected $_documentClass = '\ArangoDBClient\Document';

/**
* Sets the document class to use
*
* @param string $class Document class to use
* @return BatchPart
*/
public function setDocumentClass($class)
{
$this->_documentClass = $class;
return $this;
}
}

class_alias(BatchPart::class, '\triagens\ArangoDb\BatchPart');
36 changes: 15 additions & 21 deletions lib/ArangoDBClient/Cursor.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,11 @@
*/
class Cursor implements \Iterator
{
/**
* Import $_documentClass functionality
*/
use DocumentClassable;

/**
* The connection object
*
Expand Down Expand Up @@ -476,6 +481,7 @@ private function addDocumentsFromArray(array $data)
private function addPathsFromArray(array $data)
{
$_documentClass = $this->_documentClass;
$_edgeClass = $this->_edgeClass;

$entry = [
'vertices' => [],
Expand All @@ -487,7 +493,7 @@ private function addPathsFromArray(array $data)
$entry['vertices'][] = $_documentClass::createFromArray($v, $this->_options);
}
foreach ($data['edges'] as $v) {
$entry['edges'][] = Edge::createFromArray($v, $this->_options);
$entry['edges'][] = $_edgeClass::createFromArray($v, $this->_options);
}
$this->_result[] = $entry;
}
Expand All @@ -503,6 +509,7 @@ private function addPathsFromArray(array $data)
private function addShortestPathFromArray(array $data)
{
$_documentClass = $this->_documentClass;
$_edgeClass = $this->_edgeClass;

if (!isset($data['vertices'])) {
return;
Expand All @@ -528,7 +535,7 @@ private function addShortestPathFromArray(array $data)
$path['vertices'][] = $v;
}
foreach ($data['edges'] as $v) {
$path['edges'][] = Edge::createFromArray($v, $this->_options);
$path['edges'][] = $_edgeClass::createFromArray($v, $this->_options);
}
$entry['paths'][] = $path;

Expand Down Expand Up @@ -621,7 +628,9 @@ private function addFigureFromArray(array $data)
*/
private function addEdgesFromArray(array $data)
{
$this->_result[] = Edge::createFromArray($data, $this->_options);
$_edgeClass = $this->_edgeClass;

$this->_result[] = $_edgeClass::createFromArray($data, $this->_options);
}


Expand All @@ -635,7 +644,9 @@ private function addEdgesFromArray(array $data)
*/
private function addVerticesFromArray(array $data)
{
$this->_result[] = Vertex::createFromArray($data, $this->_options);
$_documentClass = $this->_documentClass;

$this->_result[] = $_documentClass::createFromArray($data, $this->_options);
}


Expand Down Expand Up @@ -842,23 +853,6 @@ public function getId()
{
return $this->_id;
}

/**
* @var string Document class to use
*/
protected $_documentClass = '\ArangoDBClient\Document';

/**
* Sets the document class to use
*
* @param string $class Document class to use
* @return Cursor
*/
public function setDocumentClass($class)
{
$this->_documentClass = $class;
return $this;
}
}

class_alias(Cursor::class, '\triagens\ArangoDb\Cursor');
55 changes: 55 additions & 0 deletions lib/ArangoDBClient/DocumentClassable.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
<?php

/**
* ArangoDB PHP client: mixin for $_documentClass functionality
*
* @package ArangoDBClient
* @author Jan Steemann
* @copyright Copyright 2018, triagens GmbH, Cologne, Germany
*/

namespace ArangoDBClient;

/**
* Add functionality for $_documentClass
*
* @package ArangoDBClient
* @since 3.4
*/
trait DocumentClassable
{
/**
* @var string Document class to use
*/
protected $_documentClass = '\ArangoDBClient\Document';

/**
* @var string Edge class to use
*/
protected $_edgeClass = '\ArangoDBClient\Edge';

/**
* Sets the document class to use
*
* @param string $class Document class to use
* @return DocumentClassable
*/
public function setDocumentClass($class)
{
$this->_documentClass = $class;
return $this;
}

/**
* Sets the edge class to use
*
* @param string $class Edge class to use
* @return DocumentClassable
*/
public function setEdgeClass($class)
{
$this->_edgeClass = $class;
return $this;
}

}
20 changes: 18 additions & 2 deletions lib/ArangoDBClient/EdgeHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,19 @@ class EdgeHandler extends DocumentHandler
*/
const OPTION_DIRECTION = 'direction';

/**
* Construct a new handler
*
* @param Connection $connection - connection to be used
*
*/
public function __construct(Connection $connection)
{
parent::__construct($connection);

$this->setDocumentClass('\ArangoDBClient\Edge');
}

/**
* Intermediate function to call the createFromArray function from the right context
*
Expand All @@ -74,7 +87,9 @@ class EdgeHandler extends DocumentHandler
*/
public function createFromArrayWithContext($data, $options)
{
return Edge::createFromArray($data, $options);
$_edgeClass = $this->_edgeClass;

return $_edgeClass::createFromArray($data, $options);
}


Expand Down Expand Up @@ -127,7 +142,8 @@ public function saveEdge($collection, $from, $to, $document, array $options = []
$collection = $this->makeCollection($collection);

if (is_array($document)) {
$document = Edge::createFromArray($document);
$_edgeClass = $this->_edgeClass;
$document = $_edgeClass::createFromArray($document);
}
$document->setFrom($from);
$document->setTo($to);
Expand Down
22 changes: 5 additions & 17 deletions lib/ArangoDBClient/Export.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,11 @@
*/
class Export
{
/**
* Import $_documentClass functionality
*/
use DocumentClassable;

/**
* The connection object
*
Expand Down Expand Up @@ -264,23 +269,6 @@ private function getCursorOptions()

return $result;
}

/**
* @var string Document class to use
*/
protected $_documentClass = '\ArangoDBClient\Document';

/**
* Sets the document class to use
*
* @param string $class Document class to use
* @return Export
*/
public function setDocumentClass($class)
{
$this->_documentClass = $class;
return $this;
}
}

class_alias(Export::class, '\triagens\ArangoDb\Export');
24 changes: 7 additions & 17 deletions lib/ArangoDBClient/ExportCursor.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,11 @@
*/
class ExportCursor
{
/**
* Import $_documentClass functionality
*/
use DocumentClassable;

/**
* The connection object
*
Expand Down Expand Up @@ -204,6 +209,7 @@ public function getNextBatch()
private function setData(array $data)
{
$_documentClass = $this->_documentClass;
$_edgeClass = $this->_edgeClass;

if (isset($this->_options[self::ENTRY_FLAT]) && $this->_options[self::ENTRY_FLAT]) {
$this->_result = $data;
Expand All @@ -212,7 +218,7 @@ private function setData(array $data)

if ($this->_options[self::ENTRY_TYPE] === Collection::TYPE_EDGE) {
foreach ($data as $row) {
$this->_result[] = Edge::createFromArray($row, $this->_options);
$this->_result[] = $_edgeClass::createFromArray($row, $this->_options);
}
} else {
foreach ($data as $row) {
Expand Down Expand Up @@ -281,22 +287,6 @@ public function getId()
return $this->_id;
}

/**
* @var string Document class to use
*/
protected $_documentClass = '\ArangoDBClient\Document';

/**
* Sets the document class to use
*
* @param string $class Document class to use
* @return ExportCursor
*/
public function setDocumentClass($class)
{
$this->_documentClass = $class;
return $this;
}
}

class_alias(ExportCursor::class, '\triagens\ArangoDb\ExportCursor');
Loading