diff --git a/lib/ArangoDBClient/Batch.php b/lib/ArangoDBClient/Batch.php index 18ae0a21..d845fc9e 100644 --- a/lib/ArangoDBClient/Batch.php +++ b/lib/ArangoDBClient/Batch.php @@ -14,13 +14,16 @@ /** * Provides batching functionality * - *
- * * @package ArangoDBClient * @since 1.1 */ class Batch { + /** + * Import $_documentClass functionality + */ + use DocumentClassable; + /** * Batch Response Object * @@ -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'); diff --git a/lib/ArangoDBClient/BatchPart.php b/lib/ArangoDBClient/BatchPart.php index 4ef1938e..2ca64a64 100644 --- a/lib/ArangoDBClient/BatchPart.php +++ b/lib/ArangoDBClient/BatchPart.php @@ -14,8 +14,6 @@ /** * Provides batch part functionality * - *
- * * @package ArangoDBClient * @since 1.1 */ @@ -23,6 +21,10 @@ class BatchPart { + /** + * Import $_documentClass functionality + */ + use DocumentClassable; /** @@ -243,6 +245,7 @@ public function getHttpCode() public function getProcessedResponse() { $_documentClass = $this->_documentClass; + $_edgeClass = $this->_edgeClass; $response = $this->getResponse(); switch ($this->_type) { @@ -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(); @@ -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': @@ -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'); diff --git a/lib/ArangoDBClient/Collection.php b/lib/ArangoDBClient/Collection.php index f8c878a3..ca189c86 100644 --- a/lib/ArangoDBClient/Collection.php +++ b/lib/ArangoDBClient/Collection.php @@ -489,8 +489,6 @@ public function getName() */ public function setType($type) { - assert(is_int($type)); - if ($type === 'document') { $type = self::TYPE_DOCUMENT; } else if ($type === 'edge') { @@ -505,6 +503,7 @@ public function setType($type) throw new ClientException('Invalid type used for collection'); } + assert(is_int($type)); $this->_type = $type; } diff --git a/lib/ArangoDBClient/Cursor.php b/lib/ArangoDBClient/Cursor.php index fd36fcbb..04fb1bd8 100644 --- a/lib/ArangoDBClient/Cursor.php +++ b/lib/ArangoDBClient/Cursor.php @@ -24,6 +24,11 @@ */ class Cursor implements \Iterator { + /** + * Import $_documentClass functionality + */ + use DocumentClassable; + /** * The connection object * @@ -476,6 +481,7 @@ private function addDocumentsFromArray(array $data) private function addPathsFromArray(array $data) { $_documentClass = $this->_documentClass; + $_edgeClass = $this->_edgeClass; $entry = [ 'vertices' => [], @@ -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; } @@ -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; @@ -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; @@ -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); } @@ -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); } @@ -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'); diff --git a/lib/ArangoDBClient/DocumentClassable.php b/lib/ArangoDBClient/DocumentClassable.php new file mode 100644 index 00000000..480e17cc --- /dev/null +++ b/lib/ArangoDBClient/DocumentClassable.php @@ -0,0 +1,55 @@ +_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; + } + +} diff --git a/lib/ArangoDBClient/EdgeHandler.php b/lib/ArangoDBClient/EdgeHandler.php index 3e1f0b53..b825d460 100644 --- a/lib/ArangoDBClient/EdgeHandler.php +++ b/lib/ArangoDBClient/EdgeHandler.php @@ -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 * @@ -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); } @@ -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); diff --git a/lib/ArangoDBClient/Export.php b/lib/ArangoDBClient/Export.php index 2f074b3d..70b95ef0 100644 --- a/lib/ArangoDBClient/Export.php +++ b/lib/ArangoDBClient/Export.php @@ -18,6 +18,11 @@ */ class Export { + /** + * Import $_documentClass functionality + */ + use DocumentClassable; + /** * The connection object * @@ -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'); diff --git a/lib/ArangoDBClient/ExportCursor.php b/lib/ArangoDBClient/ExportCursor.php index 75c253c3..37eadade 100644 --- a/lib/ArangoDBClient/ExportCursor.php +++ b/lib/ArangoDBClient/ExportCursor.php @@ -24,6 +24,11 @@ */ class ExportCursor { + /** + * Import $_documentClass functionality + */ + use DocumentClassable; + /** * The connection object * @@ -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; @@ -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) { @@ -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'); diff --git a/lib/ArangoDBClient/GraphHandler.php b/lib/ArangoDBClient/GraphHandler.php index 357386d4..7cbd6470 100644 --- a/lib/ArangoDBClient/GraphHandler.php +++ b/lib/ArangoDBClient/GraphHandler.php @@ -177,7 +177,7 @@ public function getGraph($graph, array $options = []) $data = $response->getJson(); $options['_isNew'] = false; - + $result = Graph::createFromArray($data['graph'], $options); $result->set(Graph::ENTRY_KEY, $data['graph'][self::OPTION_NAME]); @@ -640,7 +640,9 @@ public function saveVertex($graph, $document, $collection = null) } if (is_array($document)) { - $document = Vertex::createFromArray($document); + $_documentClass = $this->_documentClass; + + $document = $_documentClass::createFromArray($document); } if ($collection === null) { @@ -730,7 +732,9 @@ public function getVertex($graph, $vertexId, array $options = [], $collection = $options['_isNew'] = false; - return Vertex::createFromArray($vertex, $options); + $_documentClass = $this->_documentClass; + + return $_documentClass::createFromArray($vertex, $options); } @@ -1036,7 +1040,8 @@ public function saveEdge($graph, $from, $to, $label = null, $document, $collecti } if (is_array($document)) { - $document = Edge::createFromArray($document); + $_edgeClass = $this->_edgeClass; + $document = $_edgeClass::createFromArray($document); } if (null !== $label) { $document->set('$label', $label); @@ -1112,7 +1117,8 @@ public function getEdge($graph, $edgeId, array $options = [], $collection = null $options['_isNew'] = false; - return Edge::createFromArray($edge, $options); + $_edgeClass = $this->_edgeClass; + return $_edgeClass::createFromArray($edge, $options); } diff --git a/lib/ArangoDBClient/Handler.php b/lib/ArangoDBClient/Handler.php index 88e44a56..965365fe 100644 --- a/lib/ArangoDBClient/Handler.php +++ b/lib/ArangoDBClient/Handler.php @@ -18,6 +18,11 @@ */ abstract class Handler { + /** + * Import $_documentClass functionality + */ + use DocumentClassable; + /** * Connection object * @@ -157,23 +162,6 @@ protected function makeCollection($value) return $value; } - /** - * @var string Document class to use - */ - protected $_documentClass = '\ArangoDBClient\Document'; - - /** - * Sets the document class to use - * - * @param string $class Document class to use - * @return Handler - */ - public function setDocumentClass($class) - { - $this->_documentClass = $class; - return $this; - } - } class_alias(Handler::class, '\triagens\ArangoDb\Handler'); diff --git a/lib/ArangoDBClient/UserHandler.php b/lib/ArangoDBClient/UserHandler.php index 45d8901f..b3eccfc3 100644 --- a/lib/ArangoDBClient/UserHandler.php +++ b/lib/ArangoDBClient/UserHandler.php @@ -148,7 +148,8 @@ public function get($username) $options = ['_isNew' => false]; - return User::createFromArray($data, $options); + $_documentClass = $this->_documentClass; + return $_documentClass::createFromArray($data, $options); } diff --git a/lib/ArangoDBClient/VertexHandler.php b/lib/ArangoDBClient/VertexHandler.php index 1c68a7f4..3211ded6 100644 --- a/lib/ArangoDBClient/VertexHandler.php +++ b/lib/ArangoDBClient/VertexHandler.php @@ -34,7 +34,9 @@ class VertexHandler extends DocumentHandler */ public function createFromArrayWithContext($data, $options) { - return Vertex::createFromArray($data, $options); + $_documentClass = $this->_documentClass; + + return $_documentClass::createFromArray($data, $options); } } diff --git a/tests/CustomEdgeClassTest.php b/tests/CustomEdgeClassTest.php new file mode 100644 index 00000000..39f20885 --- /dev/null +++ b/tests/CustomEdgeClassTest.php @@ -0,0 +1,118 @@ +connection = getConnection(); + $this->collectionHandler = new CollectionHandler($this->connection); + + try { + $this->collectionHandler->drop('ArangoDB_PHP_TestSuite_TestCollection_01'); + } catch (\Exception $e) { + // don't bother us, if it's already deleted. + } + + $this->collection = new Collection(); + $this->collection->setName('ArangoDB_PHP_TestSuite_TestCollection_01' . '_' . static::$testsTimestamp); + $this->collection->setType('edge'); + $this->collectionHandler->create($this->collection); + } + + + /** + * Try to retrieve an edge with custom document class + */ + public function testGetCustomEdgeWithHandler() + { + $connection = $this->connection; + $collection = $this->collection; + $edge = new Edge(); + $edgeHandler = new EdgeHandler($connection); + + $edge->someAttribute = 'someValue'; + $edge->setFrom('test/v1'); + $edge->setTo('test/v2'); + $documentId = $edgeHandler->saveEdge($collection->getName(), 'test/v1', 'test/v2', $edge); + + $edgeHandler->setEdgeClass(CustomEdgeClass1::class); + $resultingEdge1 = $edgeHandler->get($collection->getName(), $documentId); + static::assertInstanceOf(CustomEdgeClass1::class, $resultingEdge1, 'Retrieved edge isn\'t made with provided CustomEdgeClass1!'); + + $edgeHandler->setEdgeClass(CustomEdgeClass2::class); + $resultingEdge2 = $edgeHandler->get($collection->getName(), $documentId); + static::assertInstanceOf(CustomEdgeClass2::class, $resultingEdge2, 'Retrieved edge isn\'t made with provided CustomEdgeClass2!'); + + $edgeHandler->setEdgeClass(Edge::class); + $resultingEdge = $edgeHandler->get($collection->getName(), $documentId); + static::assertInstanceOf(Edge::class, $resultingEdge, 'Retrieved edge isn\'t made with provided Edge!'); + static::assertNotInstanceOf(CustomEdgeClass1::class, $resultingEdge, 'Retrieved edge is made with CustomEdgeClass1!'); + static::assertNotInstanceOf(CustomEdgeClass2::class, $resultingEdge, 'Retrieved edge is made with CustomEdgeClass2!'); + + $resultingAttribute = $resultingEdge->someAttribute; + static::assertSame('someValue', $resultingAttribute); + static::assertSame('test/v1', $resultingEdge->getFrom()); + static::assertSame('test/v2', $resultingEdge->getTo()); + + $resultingAttribute1 = $resultingEdge1->someAttribute; + static::assertSame('someValue', $resultingAttribute1); + static::assertSame('test/v1', $resultingEdge1->getFrom()); + static::assertSame('test/v2', $resultingEdge1->getTo()); + + $resultingAttribute2 = $resultingEdge2->someAttribute; + static::assertSame('someValue', $resultingAttribute2); + static::assertSame('test/v1', $resultingEdge2->getFrom()); + static::assertSame('test/v2', $resultingEdge2->getTo()); + + $edgeHandler->remove($edge); + } + + public function tearDown() + { + try { + $this->collectionHandler->drop('ArangoDB_PHP_TestSuite_TestCollection_01' . '_' . static::$testsTimestamp); + } catch (\Exception $e) { + // don't bother us, if it's already deleted. + } + } + +} + +class CustomEdgeClass1 extends Edge +{ +} + +class CustomEdgeClass2 extends Edge +{ +}