├── .gitignore ├── .travis.yml ├── src ├── Collection │ ├── Map.php │ ├── ArrayList.php │ ├── CollectionInterface.php │ └── AbstractCollection.php ├── Result │ ├── Result.php │ ├── RecordViewInterface.php │ ├── RecordCursorInterface.php │ ├── ResultSummaryInterface.php │ ├── StatementStatisticsInterface.php │ ├── StatementResult.php │ ├── AbstractRecordView.php │ ├── Record.php │ ├── AbstractRecordCursor.php │ ├── ResultCollection.php │ ├── CombinedStatistics.php │ └── StatementStatistics.php ├── Driver │ ├── PipelineInterface.php │ ├── ConfigInterface.php │ ├── DriverInterface.php │ ├── Protocol.php │ └── SessionInterface.php ├── Type │ ├── Identity.php │ ├── Node.php │ ├── Relationship.php │ ├── Path.php │ └── MapAccessor.php ├── Transaction │ ├── TransactionState.php │ └── TransactionInterface.php ├── Cypher │ ├── StatementType.php │ ├── StatementCollectionInterface.php │ ├── StatementInterface.php │ ├── StatementCollection.php │ └── Statement.php ├── GraphDatabaseInterface.php ├── Connection │ ├── ConnectionInterface.php │ └── BaseConfiguration.php ├── Graph │ ├── Direction.php │ ├── Label.php │ ├── RelationshipType.php │ ├── PropertyBagInterface.php │ ├── NodeInterface.php │ ├── PropertyBag.php │ ├── RelationshipInterface.php │ ├── Node.php │ └── Relationship.php ├── Collections.php └── Schema │ ├── ConstraintType.php │ ├── IndexDefinition.php │ └── SchemaDefinition.php ├── phpunit.xml.dist ├── LICENSE ├── tests ├── Collection │ └── CollectionsTest.php ├── Graph │ ├── RelationshipTypeUnitTest.php │ ├── LabelUnitTest.php │ ├── DirectionUnitTest.php │ ├── PropertyBagUnitTest.php │ ├── NodeUnitTest.php │ └── RelationshipUnitTest.php ├── Cypher │ ├── StatementTypeUnitTest.php │ ├── StatementCollectionUnitTest.php │ └── StatementUnitTest.php ├── Result │ ├── RecordCursor.php │ ├── StatementStatisticsUnitTest.php │ └── ResultCollectionTest.php ├── Driver │ └── ProtocolEnumUnitTest.php └── Schema │ ├── IndexDefinitionUnitTest.php │ ├── ConstraintTypeUnitTest.php │ └── SchemaDefinitionUnitTest.php ├── composer.json └── README.md /.gitignore: -------------------------------------------------------------------------------- 1 | vendor/ 2 | composer.lock 3 | phpunit.xml -------------------------------------------------------------------------------- /.travis.yml: -------------------------------------------------------------------------------- 1 | language: php 2 | sudo: false 3 | php: 4 | - 5.5 5 | - 5.6 6 | - 7.0 7 | 8 | 9 | before_install: 10 | - composer self-update 11 | - composer install --prefer-source --no-interaction 12 | 13 | script: 14 | - ./vendor/bin/phpunit 15 | 16 | notifications: 17 | email: "christophe@graphaware.com" -------------------------------------------------------------------------------- /src/Collection/Map.php: -------------------------------------------------------------------------------- 1 | 9 | * 10 | * For the full copyright and license information, please view the LICENSE 11 | * file that was distributed with this source code. 12 | */ 13 | 14 | class Map extends AbstractCollection {} -------------------------------------------------------------------------------- /src/Collection/ArrayList.php: -------------------------------------------------------------------------------- 1 | 9 | * 10 | * For the full copyright and license information, please view the LICENSE 11 | * file that was distributed with this source code. 12 | */ 13 | 14 | class ArrayList extends AbstractCollection {} -------------------------------------------------------------------------------- /src/Result/Result.php: -------------------------------------------------------------------------------- 1 | 7 | * 8 | * For the full copyright and license information, please view the LICENSE 9 | * file that was distributed with this source code. 10 | */ 11 | 12 | namespace GraphAware\Common\Result; 13 | 14 | interface Result extends StatementResult 15 | { 16 | } 17 | -------------------------------------------------------------------------------- /src/Result/RecordViewInterface.php: -------------------------------------------------------------------------------- 1 | 7 | * 8 | * For the full copyright and license information, please view the LICENSE 9 | * file that was distributed with this source code. 10 | */ 11 | 12 | namespace GraphAware\Common\Result; 13 | 14 | interface RecordViewInterface extends Record 15 | { 16 | } 17 | -------------------------------------------------------------------------------- /src/Result/RecordCursorInterface.php: -------------------------------------------------------------------------------- 1 | 7 | * 8 | * For the full copyright and license information, please view the LICENSE 9 | * file that was distributed with this source code. 10 | */ 11 | 12 | namespace GraphAware\Common\Result; 13 | 14 | interface RecordCursorInterface extends Result 15 | { 16 | } 17 | -------------------------------------------------------------------------------- /src/Driver/PipelineInterface.php: -------------------------------------------------------------------------------- 1 | 7 | * 8 | * For the full copyright and license information, please view the LICENSE 9 | * file that was distributed with this source code. 10 | */ 11 | 12 | namespace GraphAware\Common\Driver; 13 | 14 | /** 15 | * @deprecated Will be removed i 4.0. 16 | */ 17 | interface ConfigInterface 18 | { 19 | } 20 | -------------------------------------------------------------------------------- /src/Driver/DriverInterface.php: -------------------------------------------------------------------------------- 1 | 7 | * 8 | * For the full copyright and license information, please view the LICENSE 9 | * file that was distributed with this source code. 10 | */ 11 | 12 | namespace GraphAware\Common\Driver; 13 | 14 | interface DriverInterface 15 | { 16 | /** 17 | * @return SessionInterface 18 | */ 19 | public function session(); 20 | } 21 | -------------------------------------------------------------------------------- /phpunit.xml.dist: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | ./tests 7 | 8 | 9 | 10 | 11 | tests 12 | vendor 13 | bin 14 | 15 | 16 | -------------------------------------------------------------------------------- /src/Collection/CollectionInterface.php: -------------------------------------------------------------------------------- 1 | 7 | * 8 | * For the full copyright and license information, please view the LICENSE 9 | * file that was distributed with this source code. 10 | */ 11 | 12 | namespace GraphAware\Common\Collection; 13 | 14 | interface CollectionInterface 15 | { 16 | public function setElements(array $elements); 17 | 18 | public function getElements(); 19 | } -------------------------------------------------------------------------------- /src/Type/Identity.php: -------------------------------------------------------------------------------- 1 | 7 | * 8 | * For the full copyright and license information, please view the LICENSE 9 | * file that was distributed with this source code. 10 | */ 11 | 12 | namespace GraphAware\Common\Type; 13 | 14 | interface Identity 15 | { 16 | /** 17 | * Returns the identity identifier of the object. 18 | * 19 | * @return int 20 | */ 21 | public function identity(); 22 | } 23 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | Copyright 2015 Graph Aware Limited 2 | 3 | Licensed under the Apache License, Version 2.0 (the "License"); 4 | you may not use this file except in compliance with the License. 5 | You may obtain a copy of the License at 6 | 7 | http://www.apache.org/licenses/LICENSE-2.0 8 | 9 | Unless required by applicable law or agreed to in writing, software 10 | distributed under the License is distributed on an "AS IS" BASIS, 11 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | See the License for the specific language governing permissions and 13 | limitations under the License. -------------------------------------------------------------------------------- /src/Transaction/TransactionState.php: -------------------------------------------------------------------------------- 1 | 7 | * 8 | * For the full copyright and license information, please view the LICENSE 9 | * file that was distributed with this source code. 10 | */ 11 | 12 | namespace GraphAware\Common\Transaction; 13 | 14 | use MyCLabs\Enum\Enum; 15 | 16 | final class TransactionState extends Enum 17 | { 18 | const OPEN = 'TRANSACTION_OPEN'; 19 | 20 | const ROLLED_BACK = 'TRANSACTION_ROLLED_BACK'; 21 | 22 | const COMMITED = 'TRANSACTION_COMMITED'; 23 | } 24 | -------------------------------------------------------------------------------- /src/Driver/Protocol.php: -------------------------------------------------------------------------------- 1 | 7 | * 8 | * For the full copyright and license information, please view the LICENSE 9 | * file that was distributed with this source code. 10 | */ 11 | 12 | namespace GraphAware\Common\Driver; 13 | 14 | use MyCLabs\Enum\Enum; 15 | 16 | class Protocol extends Enum 17 | { 18 | const HTTP = 'HTTP'; 19 | 20 | const HTTPS = 'HTTPS'; 21 | 22 | const TCP = 'TCP'; 23 | 24 | const TLS = 'TLS'; 25 | 26 | const WS = 'WS'; 27 | 28 | const WSS = 'WSS'; 29 | } 30 | -------------------------------------------------------------------------------- /src/Cypher/StatementType.php: -------------------------------------------------------------------------------- 1 | 7 | * 8 | * For the full copyright and license information, please view the LICENSE 9 | * file that was distributed with this source code. 10 | */ 11 | 12 | namespace GraphAware\Common\Cypher; 13 | 14 | use MyCLabs\Enum\Enum; 15 | 16 | final class StatementType extends Enum 17 | { 18 | const READ_ONLY = 'STATEMENT_READ_ONLY'; 19 | 20 | const READ_WRITE = 'STATEMENT_READ_WRITE'; 21 | 22 | const WRITE_ONLY = 'STATEMENT_WRITE_ONLY'; 23 | 24 | const SCHEMA_WRITE = 'STATEMENT_SCHEMA_WRITE'; 25 | } 26 | -------------------------------------------------------------------------------- /src/Type/Node.php: -------------------------------------------------------------------------------- 1 | 6 | * 7 | * For the full copyright and license information, please view the LICENSE 8 | * file that was distributed with this source code. 9 | */ 10 | 11 | namespace GraphAware\Common\Type; 12 | 13 | interface Node extends MapAccessor, Identity 14 | { 15 | /** 16 | * Returns all labels of the node. 17 | * 18 | * @return string[] 19 | */ 20 | public function labels(); 21 | /** 22 | * @param string $label 23 | * 24 | * @return bool 25 | */ 26 | public function hasLabel($label); 27 | } 28 | -------------------------------------------------------------------------------- /tests/Collection/CollectionsTest.php: -------------------------------------------------------------------------------- 1 | assertInstanceOf(ArrayList::class, $listcoll); 15 | $this->assertCount(3, $listcoll->getElements()); 16 | } 17 | 18 | public function testMapCreation() 19 | { 20 | $mapcoll = Collections::asMap(['hello' => 'you']); 21 | $this->assertInstanceOf(Map::class, $mapcoll); 22 | $this->assertCount(1, $mapcoll->getElements()); 23 | } 24 | } -------------------------------------------------------------------------------- /src/Type/Relationship.php: -------------------------------------------------------------------------------- 1 | 7 | * 8 | * For the full copyright and license information, please view the LICENSE 9 | * file that was distributed with this source code. 10 | */ 11 | 12 | namespace GraphAware\Common\Type; 13 | 14 | interface Relationship extends MapAccessor, Identity 15 | { 16 | /** 17 | * Returns the type of the relationship. 18 | * 19 | * @return string 20 | */ 21 | public function type(); 22 | 23 | /** 24 | * Returns whether or not the relationship has the given type. 25 | * 26 | * @param string $type 27 | * 28 | * @return bool 29 | */ 30 | public function hasType($type); 31 | } 32 | -------------------------------------------------------------------------------- /src/GraphDatabaseInterface.php: -------------------------------------------------------------------------------- 1 | 7 | * 8 | * For the full copyright and license information, please view the LICENSE 9 | * file that was distributed with this source code. 10 | */ 11 | 12 | namespace GraphAware\Common; 13 | 14 | use GraphAware\Common\Connection\BaseConfiguration; 15 | use GraphAware\Common\Driver\ConfigInterface; 16 | use GraphAware\Common\Driver\DriverInterface; 17 | 18 | interface GraphDatabaseInterface 19 | { 20 | /** 21 | * @param string $uri 22 | * @param BaseConfiguration|null $config 23 | * 24 | * @return DriverInterface 25 | */ 26 | public static function driver($uri, ConfigInterface $config = null); 27 | } 28 | -------------------------------------------------------------------------------- /tests/Graph/RelationshipTypeUnitTest.php: -------------------------------------------------------------------------------- 1 | 7 | * 8 | * For the full copyright and license information, please view the LICENSE 9 | * file that was distributed with this source code. 10 | */ 11 | 12 | namespace GraphAware\Common\Tests\Graph; 13 | 14 | use GraphAware\Common\Graph\RelationshipType; 15 | 16 | /** 17 | * @group unit 18 | * @group graph 19 | */ 20 | class RelationshipTypeUnitTest extends \PHPUnit_Framework_TestCase 21 | { 22 | public function testInstance() 23 | { 24 | $type = RelationshipType::withName("FOLLOWS"); 25 | $this->assertEquals("FOLLOWS", $type->getName()); 26 | $this->assertEquals("FOLLOWS", (string) $type); 27 | } 28 | } -------------------------------------------------------------------------------- /composer.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "graphaware/neo4j-common", 3 | "description": "Common Utilities library for Neo4j", 4 | "keywords": [ 5 | "graph", "neo4j", "database", "cypher", "statement" 6 | ], 7 | "type": "library", 8 | "license": "ApacheV2", 9 | "authors": [ 10 | { 11 | "name": "Christophe Willemsen", 12 | "email": "christophe@graphaware.com" 13 | } 14 | ], 15 | "require": { 16 | "myclabs/php-enum": "^1.4" 17 | }, 18 | "require-dev": { 19 | "phpunit/phpunit": "^4.0" 20 | }, 21 | "autoload": { 22 | "psr-4": { 23 | "GraphAware\\Common\\": "src/" 24 | } 25 | }, 26 | "autoload-dev": { 27 | "psr-4": { 28 | "GraphAware\\Common\\Tests\\": "tests/" 29 | } 30 | } 31 | } 32 | -------------------------------------------------------------------------------- /tests/Cypher/StatementTypeUnitTest.php: -------------------------------------------------------------------------------- 1 | 7 | * 8 | * For the full copyright and license information, please view the LICENSE 9 | * file that was distributed with this source code. 10 | */ 11 | 12 | namespace GraphAware\Common\Tests\Cypher; 13 | 14 | use GraphAware\Common\Cypher\StatementType; 15 | 16 | /** 17 | * @group unit 18 | * @group cypher 19 | * @group tck 20 | */ 21 | class StatementTypeUnitTest extends \PHPUnit_Framework_TestCase 22 | { 23 | public function testStatementTypeStrings() 24 | { 25 | $this->assertEquals("STATEMENT_READ_WRITE", StatementType::READ_WRITE); 26 | $this->assertEquals("STATEMENT_READ_ONLY", StatementType::READ_ONLY); 27 | } 28 | } -------------------------------------------------------------------------------- /tests/Result/RecordCursor.php: -------------------------------------------------------------------------------- 1 | 7 | * 8 | * For the full copyright and license information, please view the LICENSE 9 | * file that was distributed with this source code. 10 | */ 11 | 12 | namespace GraphAware\Common\Cypher; 13 | 14 | interface StatementCollectionInterface 15 | { 16 | /** 17 | * @return StatementInterface[] 18 | */ 19 | public function getStatements(); 20 | 21 | /** 22 | * @param StatementInterface $statement 23 | */ 24 | public function add(StatementInterface $statement); 25 | 26 | /** 27 | * @return bool 28 | */ 29 | public function isEmpty(); 30 | 31 | /** 32 | * @return int 33 | */ 34 | public function getCount(); 35 | } 36 | -------------------------------------------------------------------------------- /tests/Driver/ProtocolEnumUnitTest.php: -------------------------------------------------------------------------------- 1 | 7 | * 8 | * For the full copyright and license information, please view the LICENSE 9 | * file that was distributed with this source code. 10 | */ 11 | 12 | namespace GraphAware\Common\Tests\Protocol; 13 | 14 | use GraphAware\Common\Driver\Protocol; 15 | 16 | /** 17 | * @group unit 18 | * @group driver 19 | * @group driver-tck 20 | */ 21 | class ProtocolEnumUnitTest extends \PHPUnit_Framework_TestCase 22 | { 23 | public function testDifferentProtocolsEnums() 24 | { 25 | $this->assertEquals('HTTP', Protocol::HTTP); 26 | $this->assertEquals('HTTPS', Protocol::HTTPS); 27 | $this->assertEquals('TCP', Protocol::TCP); 28 | $this->assertEquals('TLS', Protocol::TLS); 29 | $this->assertEquals('WS', Protocol::WS); 30 | $this->assertEquals('WSS', Protocol::WSS); 31 | } 32 | } -------------------------------------------------------------------------------- /src/Connection/ConnectionInterface.php: -------------------------------------------------------------------------------- 1 | 7 | * 8 | * For the full copyright and license information, please view the LICENSE 9 | * file that was distributed with this source code. 10 | */ 11 | 12 | namespace GraphAware\Common\Connection; 13 | 14 | use GraphAware\Common\Driver\DriverInterface; 15 | 16 | interface ConnectionInterface 17 | { 18 | /** 19 | * @param DriverInterface $driver 20 | * @param null|string $user 21 | * @param null|string $password 22 | */ 23 | public function __construct(DriverInterface $driver, $user = null, $password = null); 24 | 25 | /** 26 | * @return DriverInterface 27 | */ 28 | public function getDriver(); 29 | 30 | /** 31 | * @return null|string 32 | */ 33 | public function getUser(); 34 | 35 | /** 36 | * @return null|string 37 | */ 38 | public function getPassword(); 39 | } 40 | -------------------------------------------------------------------------------- /src/Collection/AbstractCollection.php: -------------------------------------------------------------------------------- 1 | 7 | * 8 | * For the full copyright and license information, please view the LICENSE 9 | * file that was distributed with this source code. 10 | */ 11 | 12 | namespace GraphAware\Common\Collection; 13 | 14 | abstract class AbstractCollection implements CollectionInterface 15 | { 16 | /** @var array */ 17 | protected $elements = []; 18 | 19 | /** 20 | * @param array $elements 21 | */ 22 | public function __construct(array $elements) 23 | { 24 | $this->elements = $elements; 25 | } 26 | 27 | /** 28 | * @param array $elements 29 | */ 30 | public function setElements(array $elements) 31 | { 32 | $this->elements = $elements; 33 | } 34 | 35 | /** 36 | * @return array 37 | */ 38 | public function getElements() 39 | { 40 | return $this->elements; 41 | } 42 | 43 | } -------------------------------------------------------------------------------- /src/Graph/Direction.php: -------------------------------------------------------------------------------- 1 | 7 | * 8 | * For the full copyright and license information, please view the LICENSE 9 | * file that was distributed with this source code. 10 | */ 11 | 12 | namespace GraphAware\Common\Graph; 13 | 14 | use MyCLabs\Enum\Enum; 15 | 16 | class Direction extends Enum 17 | { 18 | const INCOMING = 'INCOMING'; 19 | 20 | const OUTGOING = 'OUTGOING'; 21 | 22 | const BOTH = 'BOTH'; 23 | 24 | /** 25 | * @return Direction 26 | */ 27 | public static function INCOMING() 28 | { 29 | return new self(self::INCOMING); 30 | } 31 | 32 | /** 33 | * @return Direction 34 | */ 35 | public static function OUTGOING() 36 | { 37 | return new self(self::OUTGOING); 38 | } 39 | 40 | /** 41 | * @return Direction 42 | */ 43 | public static function BOTH() 44 | { 45 | return new self(self::BOTH); 46 | } 47 | } 48 | -------------------------------------------------------------------------------- /src/Collections.php: -------------------------------------------------------------------------------- 1 | 7 | * 8 | * For the full copyright and license information, please view the LICENSE 9 | * file that was distributed with this source code. 10 | */ 11 | 12 | namespace GraphAware\Common; 13 | 14 | use GraphAware\Common\Collection\ArrayList; 15 | use GraphAware\Common\Collection\Map; 16 | 17 | /** 18 | * Helper class for creating wrappers around arrays as there is no possibility in php 19 | * to define if an empty array should be sent as a map or as list via Cypher. 20 | */ 21 | class Collections 22 | { 23 | /** 24 | * @param array $elements 25 | * 26 | * @return Map 27 | */ 28 | public static function asMap(array $elements) 29 | { 30 | return new Map($elements); 31 | } 32 | 33 | /** 34 | * @param array $elements 35 | * 36 | * @return ArrayList 37 | */ 38 | public static function asList(array $elements) 39 | { 40 | return new ArrayList($elements); 41 | } 42 | } -------------------------------------------------------------------------------- /src/Graph/Label.php: -------------------------------------------------------------------------------- 1 | 7 | * 8 | * For the full copyright and license information, please view the LICENSE 9 | * file that was distributed with this source code. 10 | */ 11 | 12 | namespace GraphAware\Common\Graph; 13 | 14 | class Label 15 | { 16 | /** 17 | * @var string 18 | */ 19 | protected $name; 20 | 21 | /** 22 | * @param string $name 23 | */ 24 | public function __construct($name) 25 | { 26 | $this->name = (string) $name; 27 | } 28 | 29 | /** 30 | * @param string $name 31 | * 32 | * @return Label 33 | */ 34 | public static function label($name) 35 | { 36 | return new self($name); 37 | } 38 | 39 | /** 40 | * @return string 41 | */ 42 | public function getName() 43 | { 44 | return $this->name; 45 | } 46 | 47 | /** 48 | * @return string 49 | */ 50 | public function __toString() 51 | { 52 | return $this->name; 53 | } 54 | } 55 | -------------------------------------------------------------------------------- /src/Result/ResultSummaryInterface.php: -------------------------------------------------------------------------------- 1 | 7 | * 8 | * For the full copyright and license information, please view the LICENSE 9 | * file that was distributed with this source code. 10 | */ 11 | 12 | namespace GraphAware\Common\Result; 13 | 14 | use GraphAware\Common\Cypher\StatementInterface; 15 | use GraphAware\Common\Cypher\StatementType; 16 | 17 | interface ResultSummaryInterface 18 | { 19 | /** 20 | * @param \GraphAware\Common\Cypher\StatementInterface $statement 21 | */ 22 | public function __construct(StatementInterface $statement); 23 | 24 | /** 25 | * @return \GraphAware\Common\Cypher\StatementInterface 26 | */ 27 | public function statement(); 28 | 29 | /** 30 | * @return StatementStatistics 31 | */ 32 | public function updateStatistics(); 33 | 34 | /** 35 | * @return array 36 | */ 37 | public function notifications(); 38 | 39 | /** 40 | * @return StatementType 41 | */ 42 | public function statementType(); 43 | } 44 | -------------------------------------------------------------------------------- /src/Graph/RelationshipType.php: -------------------------------------------------------------------------------- 1 | 7 | * 8 | * For the full copyright and license information, please view the LICENSE 9 | * file that was distributed with this source code. 10 | */ 11 | 12 | namespace GraphAware\Common\Graph; 13 | 14 | class RelationshipType 15 | { 16 | /** 17 | * @var string 18 | */ 19 | protected $name; 20 | 21 | /** 22 | * @param string $name 23 | */ 24 | private function __construct($name) 25 | { 26 | $this->name = $name; 27 | } 28 | 29 | /** 30 | * @param string $name 31 | * 32 | * @return RelationshipType 33 | */ 34 | public static function withName($name) 35 | { 36 | return new self((string) $name); 37 | } 38 | 39 | /** 40 | * @return string 41 | */ 42 | public function getName() 43 | { 44 | return $this->name; 45 | } 46 | 47 | /** 48 | * @return string 49 | */ 50 | public function __toString() 51 | { 52 | return $this->name; 53 | } 54 | } 55 | -------------------------------------------------------------------------------- /tests/Graph/LabelUnitTest.php: -------------------------------------------------------------------------------- 1 | 7 | * 8 | * For the full copyright and license information, please view the LICENSE 9 | * file that was distributed with this source code. 10 | */ 11 | 12 | namespace GraphAware\Common\Tests\Graph; 13 | 14 | use GraphAware\Common\Graph\Label; 15 | 16 | /** 17 | * @group unit 18 | * @group graph 19 | */ 20 | class LabelUnitTest extends \PHPUnit_Framework_TestCase 21 | { 22 | public function testInstance() 23 | { 24 | $label = new Label("User"); 25 | $this->assertInstanceOf(Label::class, $label); 26 | $this->assertEquals("User", $label->getName()); 27 | } 28 | 29 | public function testStatic() 30 | { 31 | $label = Label::label("User"); 32 | $this->assertInstanceOf(Label::class, $label); 33 | $this->assertEquals("User", $label->getName()); 34 | } 35 | 36 | public function testClassImplementsToString() 37 | { 38 | $label = Label::label("User"); 39 | $this->assertEquals("User", (string) $label); 40 | } 41 | } -------------------------------------------------------------------------------- /tests/Schema/IndexDefinitionUnitTest.php: -------------------------------------------------------------------------------- 1 | 7 | * 8 | * For the full copyright and license information, please view the LICENSE 9 | * file that was distributed with this source code. 10 | */ 11 | 12 | namespace GraphAware\Common\Tests\Schema; 13 | 14 | use GraphAware\Common\Schema\ConstraintType; 15 | use GraphAware\Common\Schema\IndexDefinition; 16 | use GraphAware\Common\Graph\Label; 17 | 18 | /** 19 | * @group unit 20 | * @group schema 21 | */ 22 | class IndexDefinitionUnitTest extends \PHPUnit_Framework_TestCase 23 | { 24 | public function testInstance() 25 | { 26 | $def = new IndexDefinition(Label::label("User"), "login"); 27 | $this->assertInstanceOf(IndexDefinition::class, $def); 28 | } 29 | 30 | public function testUniqueChecks() 31 | { 32 | $def = new IndexDefinition(Label::label("User"), "login", ConstraintType::UNIQUENESS()); 33 | $this->assertTrue($def->isUnique()); 34 | $this->assertEquals(ConstraintType::UNIQUENESS, $def->getConstraintType()); 35 | } 36 | } -------------------------------------------------------------------------------- /src/Driver/SessionInterface.php: -------------------------------------------------------------------------------- 1 | 7 | * 8 | * For the full copyright and license information, please view the LICENSE 9 | * file that was distributed with this source code. 10 | */ 11 | 12 | namespace GraphAware\Common\Driver; 13 | 14 | use GraphAware\Common\Transaction\TransactionInterface; 15 | 16 | interface SessionInterface 17 | { 18 | /** 19 | * @param string $statement 20 | * @param array $parameters 21 | * @param null|string $tag 22 | * 23 | * @return \GraphAware\Common\Result\Result 24 | */ 25 | public function run($statement, array $parameters = [], $tag = null); 26 | 27 | public function close(); 28 | 29 | /** 30 | * @return TransactionInterface 31 | */ 32 | public function transaction(); 33 | 34 | /** 35 | * @param string|null $query 36 | * @param array $parameters 37 | * @param string|null $tag 38 | * 39 | * @return PipelineInterface 40 | */ 41 | public function createPipeline($query = null, array $parameters = array(), $tag = null); 42 | } 43 | -------------------------------------------------------------------------------- /src/Schema/ConstraintType.php: -------------------------------------------------------------------------------- 1 | 7 | * 8 | * For the full copyright and license information, please view the LICENSE 9 | * file that was distributed with this source code. 10 | */ 11 | 12 | namespace GraphAware\Common\Schema; 13 | 14 | use MyCLabs\Enum\Enum; 15 | 16 | class ConstraintType extends Enum 17 | { 18 | const UNIQUENESS = 'UNIQUENESS'; 19 | 20 | const NODE_PROPERTY_EXISTENCE = 'NODE_PROPERTY_EXISTENCE'; 21 | 22 | const RELATIONSHIP_PROPERTY_EXISTENCE = 'RELATIONSHIP_PROPERTY_EXISTENCE'; 23 | 24 | /** 25 | * @return ConstraintType 26 | */ 27 | public static function UNIQUENESS() 28 | { 29 | return new self(self::UNIQUENESS); 30 | } 31 | 32 | /** 33 | * @return ConstraintType 34 | */ 35 | public static function NODE_PROPERTY_EXISTENCE() 36 | { 37 | return new self(self::NODE_PROPERTY_EXISTENCE); 38 | } 39 | 40 | /** 41 | * @return ConstraintType 42 | */ 43 | public static function RELATIONSHIP_PROPERTY_EXISTENCE() 44 | { 45 | return new self(self::RELATIONSHIP_PROPERTY_EXISTENCE); 46 | } 47 | } 48 | -------------------------------------------------------------------------------- /tests/Schema/ConstraintTypeUnitTest.php: -------------------------------------------------------------------------------- 1 | 7 | * 8 | * For the full copyright and license information, please view the LICENSE 9 | * file that was distributed with this source code. 10 | */ 11 | 12 | namespace GraphAware\Common\Tests\Schema; 13 | 14 | use GraphAware\Common\Schema\ConstraintType; 15 | 16 | /** 17 | * @group unit 18 | * @group schema 19 | */ 20 | class ContraintTypeUnitTest extends \PHPUnit_Framework_TestCase 21 | { 22 | public function testInstance() 23 | { 24 | $c = new ConstraintType(ConstraintType::UNIQUENESS); 25 | $this->assertEquals("UNIQUENESS", $c); 26 | 27 | $cn = new ConstraintType(ConstraintType::NODE_PROPERTY_EXISTENCE); 28 | $this->assertEquals("NODE_PROPERTY_EXISTENCE", $cn); 29 | 30 | $cr = new ConstraintType(ConstraintType::RELATIONSHIP_PROPERTY_EXISTENCE); 31 | $this->assertEquals("RELATIONSHIP_PROPERTY_EXISTENCE", $cr); 32 | 33 | $this->assertEquals($c, ConstraintType::UNIQUENESS()); 34 | $this->assertEquals($cn, ConstraintType::NODE_PROPERTY_EXISTENCE()); 35 | $this->assertEquals($cr, ConstraintType::RELATIONSHIP_PROPERTY_EXISTENCE()); 36 | } 37 | } -------------------------------------------------------------------------------- /tests/Graph/DirectionUnitTest.php: -------------------------------------------------------------------------------- 1 | 7 | * 8 | * For the full copyright and license information, please view the LICENSE 9 | * file that was distributed with this source code. 10 | */ 11 | 12 | namespace GraphAware\Common\Tests\Graph; 13 | 14 | use GraphAware\Common\Graph\Direction; 15 | 16 | /** 17 | * @group unit 18 | * @group graph 19 | */ 20 | class DirectionUnitTest extends \PHPUnit_Framework_TestCase 21 | { 22 | public function testInstance() 23 | { 24 | $direction = new Direction(Direction::INCOMING); 25 | $this->assertEquals("INCOMING", $direction); 26 | 27 | $out = new Direction(Direction::OUTGOING); 28 | $this->assertEquals("OUTGOING", $out); 29 | 30 | $both = new Direction(Direction::BOTH); 31 | $this->assertEquals("BOTH", $both); 32 | } 33 | 34 | public function testStaticMethodCalls() 35 | { 36 | $inc = Direction::INCOMING(); 37 | $this->assertEquals(Direction::INCOMING, $inc); 38 | 39 | $out = Direction::OUTGOING(); 40 | $this->assertEquals(Direction::OUTGOING, $out); 41 | 42 | $both = Direction::BOTH(); 43 | $this->assertEquals(Direction::BOTH, $both); 44 | } 45 | } -------------------------------------------------------------------------------- /src/Cypher/StatementInterface.php: -------------------------------------------------------------------------------- 1 | 7 | * 8 | * For the full copyright and license information, please view the LICENSE 9 | * file that was distributed with this source code. 10 | */ 11 | 12 | namespace GraphAware\Common\Cypher; 13 | 14 | interface StatementInterface 15 | { 16 | /** 17 | * @return string 18 | */ 19 | public function text(); 20 | 21 | /** 22 | * @return array 23 | */ 24 | public function parameters(); 25 | 26 | /** 27 | * @return null|string 28 | */ 29 | public function getTag(); 30 | 31 | /** 32 | * @return bool 33 | */ 34 | public function hasTag(); 35 | 36 | /** 37 | * @return StatementType 38 | */ 39 | public function statementType(); 40 | 41 | /** 42 | * @param string $text 43 | * 44 | * @return StatementInterface 45 | */ 46 | public function withText($text); 47 | 48 | /** 49 | * @param array $parameters 50 | * 51 | * @return StatementInterface 52 | */ 53 | public function withParameters(array $parameters); 54 | 55 | /** 56 | * @param array $parameters 57 | * 58 | * @return StatementInterface 59 | */ 60 | public function withUpdatedParameters(array $parameters); 61 | } 62 | -------------------------------------------------------------------------------- /src/Graph/PropertyBagInterface.php: -------------------------------------------------------------------------------- 1 | 7 | * 8 | * For the full copyright and license information, please view the LICENSE 9 | * file that was distributed with this source code. 10 | */ 11 | 12 | namespace GraphAware\Common\Graph; 13 | 14 | interface PropertyBagInterface 15 | { 16 | /** 17 | * Returns a property value for the given $key. Throws an exception if not found. 18 | * 19 | * @param string $key 20 | * 21 | * @return mixed 22 | * 23 | * @throws \InvalidArgumentException when the object has no property with the given $key 24 | */ 25 | public function getProperty($key); 26 | 27 | /** 28 | * Returns whether or not a property exist with the given $key. 29 | * 30 | * @param string $key 31 | * 32 | * @return bool 33 | */ 34 | public function hasProperty($key); 35 | 36 | /** 37 | * Returns all properties of this bag. 38 | * 39 | * @return array 40 | */ 41 | public function getProperties(); 42 | 43 | /** 44 | * Sets a property value for the given $key. 45 | * 46 | * @param string $key 47 | * @param mixed $value 48 | */ 49 | public function setProperty($key, $value); 50 | } 51 | -------------------------------------------------------------------------------- /src/Graph/NodeInterface.php: -------------------------------------------------------------------------------- 1 | 7 | * 8 | * For the full copyright and license information, please view the LICENSE 9 | * file that was distributed with this source code. 10 | */ 11 | 12 | namespace GraphAware\Common\Graph; 13 | 14 | interface NodeInterface extends PropertyBagInterface 15 | { 16 | /** 17 | * Returns the node labels. 18 | * 19 | * @return Label[] 20 | */ 21 | public function getLabels(); 22 | 23 | /** 24 | * Returns whether or not the node has the given $name label. 25 | * 26 | * @param string $name 27 | * 28 | * @return bool 29 | */ 30 | public function hasLabel($name); 31 | 32 | /** 33 | * Returns the node internal id. 34 | * 35 | * @return int 36 | */ 37 | public function getId(); 38 | 39 | /** 40 | * Returns the node relationships. 41 | * 42 | * @return RelationshipInterface[] 43 | */ 44 | public function getRelationships(); 45 | 46 | /** 47 | * Returns whether or not the node has relationships. 48 | * 49 | * @return bool 50 | */ 51 | public function hasRelationships(); 52 | 53 | /** 54 | * Add a relationship to the Node. 55 | * 56 | * @param RelationshipInterface $relationship 57 | */ 58 | public function addRelationship(RelationshipInterface $relationship); 59 | } 60 | -------------------------------------------------------------------------------- /src/Result/StatementStatisticsInterface.php: -------------------------------------------------------------------------------- 1 | 7 | * 8 | * For the full copyright and license information, please view the LICENSE 9 | * file that was distributed with this source code. 10 | */ 11 | 12 | namespace GraphAware\Common\Result; 13 | 14 | interface StatementStatisticsInterface 15 | { 16 | /** 17 | * @return bool 18 | */ 19 | public function containsUpdates(); 20 | 21 | /** 22 | * @return int 23 | */ 24 | public function nodesCreated(); 25 | 26 | /** 27 | * @return int 28 | */ 29 | public function nodesDeleted(); 30 | 31 | /** 32 | * @return int 33 | */ 34 | public function relationshipsCreated(); 35 | 36 | /** 37 | * @return int 38 | */ 39 | public function relationshipsDeleted(); 40 | 41 | /** 42 | * @return int 43 | */ 44 | public function propertiesSet(); 45 | 46 | /** 47 | * @return int 48 | */ 49 | public function labelsAdded(); 50 | 51 | /** 52 | * @return int 53 | */ 54 | public function labelsRemoved(); 55 | 56 | /** 57 | * @return int 58 | */ 59 | public function indexesAdded(); 60 | 61 | /** 62 | * @return int 63 | */ 64 | public function indexesRemoved(); 65 | 66 | /** 67 | * @return int 68 | */ 69 | public function constraintsAdded(); 70 | 71 | /** 72 | * @return int 73 | */ 74 | public function constraintsRemoved(); 75 | } 76 | -------------------------------------------------------------------------------- /tests/Graph/PropertyBagUnitTest.php: -------------------------------------------------------------------------------- 1 | 7 | * 8 | * For the full copyright and license information, please view the LICENSE 9 | * file that was distributed with this source code. 10 | */ 11 | 12 | namespace GraphAware\Common\Tests\Graph; 13 | 14 | use GraphAware\Common\Graph\PropertyBag; 15 | use \InvalidArgumentException; 16 | use \ArrayIterator; 17 | 18 | /** 19 | * @group unit 20 | * @group graph 21 | */ 22 | class PropertyBagUnitTest extends \PHPUnit_Framework_TestCase 23 | { 24 | public function testNewInstance() 25 | { 26 | $bag = new PropertyBag(); 27 | $this->assertInstanceOf(PropertyBag::class, $bag); 28 | $this->assertFalse($bag->hasProperty('none')); 29 | $this->setExpectedException(InvalidArgumentException::class); 30 | $bag->getProperty("cool"); 31 | } 32 | 33 | public function testBagWithProperties() 34 | { 35 | $bag = new PropertyBag($this->getProperties()); 36 | $this->assertCount(2, $bag->getProperties()); 37 | $this->assertEquals(1, $bag->getProperty("id")); 38 | } 39 | 40 | public function testSet() 41 | { 42 | $bag = new PropertyBag($this->getProperties()); 43 | $this->assertCount(2, $bag->getProperties()); 44 | $bag->setProperty("age", 34); 45 | $this->assertCount(3, $bag->getProperties()); 46 | $this->assertEquals(34, $bag->getProperty("age")); 47 | } 48 | 49 | protected function getProperties() 50 | { 51 | return array("id" => 1, "name" => "Michael"); 52 | } 53 | } -------------------------------------------------------------------------------- /src/Result/StatementResult.php: -------------------------------------------------------------------------------- 1 | 9 | * 10 | * For the full copyright and license information, please view the LICENSE 11 | * file that was distributed with this source code. 12 | */ 13 | 14 | use GraphAware\Common\Cypher\StatementInterface; 15 | 16 | interface StatementResult 17 | { 18 | /** 19 | * @param StatementInterface $statement 20 | */ 21 | public function __construct(StatementInterface $statement); 22 | 23 | /** 24 | * @return StatementInterface 25 | */ 26 | public function statement(); 27 | 28 | /** 29 | * @return ResultSummaryInterface 30 | */ 31 | public function summarize(); 32 | 33 | /** 34 | * @return bool 35 | */ 36 | public function hasSummary(); 37 | 38 | /** 39 | * @return Record[] 40 | */ 41 | public function records(); 42 | 43 | /** 44 | * @return bool 45 | */ 46 | public function isOpen(); 47 | 48 | /** 49 | * @void 50 | */ 51 | public function close(); 52 | 53 | /** 54 | * @return int 55 | */ 56 | public function size(); 57 | 58 | /** 59 | * @return Record 60 | * @throws \RuntimeException When there is no record 61 | */ 62 | public function firstRecord(); 63 | 64 | /** 65 | * Returns the first record of the result or the given default if the Record cursor is empty 66 | * 67 | * @param mixed $default 68 | * @return mixed|Record 69 | */ 70 | public function firstRecordOrDefault($default); 71 | } 72 | -------------------------------------------------------------------------------- /src/Graph/PropertyBag.php: -------------------------------------------------------------------------------- 1 | 7 | * 8 | * For the full copyright and license information, please view the LICENSE 9 | * file that was distributed with this source code. 10 | */ 11 | 12 | namespace GraphAware\Common\Graph; 13 | 14 | /** 15 | * PropertyBag is a Common API for handling both Nodes and Relationships properties. 16 | * It acts as a container for key/value pairs. 17 | */ 18 | class PropertyBag implements PropertyBagInterface 19 | { 20 | /** 21 | * @var array 22 | */ 23 | protected $properties; 24 | 25 | /** 26 | * @param array $properties 27 | */ 28 | public function __construct(array $properties = array()) 29 | { 30 | $this->properties = $properties; 31 | } 32 | 33 | /** 34 | * {@inheritdoc} 35 | */ 36 | public function getProperty($key) 37 | { 38 | if (!array_key_exists($key, $this->properties)) { 39 | throw new \InvalidArgumentException(sprintf('No property with key "%s" found', $key)); 40 | } 41 | 42 | return $this->properties[$key]; 43 | } 44 | 45 | /** 46 | * {@inheritdoc} 47 | */ 48 | public function hasProperty($key) 49 | { 50 | return array_key_exists($key, $this->properties); 51 | } 52 | 53 | /** 54 | * {@inheritdoc} 55 | */ 56 | public function getProperties() 57 | { 58 | return $this->properties; 59 | } 60 | 61 | /** 62 | * {@inheritdoc} 63 | */ 64 | public function setProperty($key, $value) 65 | { 66 | $this->properties[$key] = $value; 67 | } 68 | } 69 | -------------------------------------------------------------------------------- /src/Transaction/TransactionInterface.php: -------------------------------------------------------------------------------- 1 | 7 | * 8 | * For the full copyright and license information, please view the LICENSE 9 | * file that was distributed with this source code. 10 | */ 11 | 12 | namespace GraphAware\Common\Transaction; 13 | 14 | use GraphAware\Common\Cypher\Statement; 15 | use GraphAware\Common\Cypher\StatementInterface; 16 | use GraphAware\Common\Result\Result; 17 | use GraphAware\Common\Result\ResultCollection; 18 | 19 | interface TransactionInterface 20 | { 21 | /** 22 | * @return bool 23 | */ 24 | public function isOpen(); 25 | 26 | /** 27 | * @return bool 28 | */ 29 | public function isCommited(); 30 | 31 | /** 32 | * @return bool 33 | */ 34 | public function isRolledBack(); 35 | 36 | /** 37 | * @return string 38 | */ 39 | public function status(); 40 | 41 | /** 42 | */ 43 | public function commit(); 44 | 45 | /** 46 | */ 47 | public function rollback(); 48 | 49 | /** 50 | * @param string $statement 51 | * @param array $parameters 52 | * @param null|string $tag 53 | */ 54 | public function push($statement, array $parameters = array(), $tag = null); 55 | 56 | public function begin(); 57 | 58 | /** 59 | * @param Statement $statement 60 | * 61 | * @return Result 62 | */ 63 | public function run(Statement $statement); 64 | 65 | /** 66 | * @param StatementInterface[] $statements 67 | * 68 | * @return ResultCollection|Result[] 69 | */ 70 | public function runMultiple(array $statements); 71 | } 72 | -------------------------------------------------------------------------------- /src/Type/Path.php: -------------------------------------------------------------------------------- 1 | 7 | * 8 | * For the full copyright and license information, please view the LICENSE 9 | * file that was distributed with this source code. 10 | */ 11 | 12 | namespace GraphAware\Common\Type; 13 | 14 | interface Path 15 | { 16 | /** 17 | * Returns the start node of the path. 18 | * 19 | * @return Node 20 | */ 21 | public function start(); 22 | 23 | /** 24 | * Returns the end node of the path. 25 | * 26 | * @return Node 27 | */ 28 | public function end(); 29 | 30 | /** 31 | * Returns the length of the path. 32 | * 33 | * @return int 34 | */ 35 | public function length(); 36 | 37 | /** 38 | * Returns whether or not the path contains the given node. 39 | * 40 | * @param Node $node 41 | * 42 | * @return bool 43 | */ 44 | public function containsNode(Node $node); 45 | 46 | /** 47 | * Returns whether or not the path contains the given relationship. 48 | * 49 | * @param Relationship $relationship 50 | * 51 | * @return bool 52 | */ 53 | public function containsRelationship(Relationship $relationship); 54 | 55 | /** 56 | * Returns the nodes in the path, nodes will appear in the same order as they appear in the path. 57 | * 58 | * @return Node[] 59 | */ 60 | public function nodes(); 61 | 62 | /** 63 | * Returns the relationships in the path, relationships will appear in the same order as they appear in the path. 64 | * 65 | * @return Relationship[] 66 | */ 67 | public function relationships(); 68 | } 69 | -------------------------------------------------------------------------------- /src/Cypher/StatementCollection.php: -------------------------------------------------------------------------------- 1 | 7 | * 8 | * For the full copyright and license information, please view the LICENSE 9 | * file that was distributed with this source code. 10 | */ 11 | 12 | namespace GraphAware\Common\Cypher; 13 | 14 | class StatementCollection implements StatementCollectionInterface 15 | { 16 | /** 17 | * @var StatementInterface[] 18 | */ 19 | protected $statements = []; 20 | 21 | /** 22 | * @var null|string 23 | */ 24 | protected $tag; 25 | 26 | /** 27 | * @param null|string $tag 28 | */ 29 | public function __construct($tag = null) 30 | { 31 | $this->tag = null !== $tag ? (string) $tag : null; 32 | } 33 | 34 | /** 35 | * {@inheritdoc} 36 | */ 37 | public function getStatements() 38 | { 39 | return $this->statements; 40 | } 41 | 42 | /** 43 | * {@inheritdoc} 44 | */ 45 | public function add(StatementInterface $statement) 46 | { 47 | $this->statements[] = $statement; 48 | } 49 | 50 | /** 51 | * {@inheritdoc} 52 | */ 53 | public function isEmpty() 54 | { 55 | return empty($this->statements); 56 | } 57 | 58 | /** 59 | * {@inheritdoc} 60 | */ 61 | public function getCount() 62 | { 63 | return count($this->statements); 64 | } 65 | 66 | /** 67 | * @return null|string 68 | */ 69 | public function getTag() 70 | { 71 | return $this->tag; 72 | } 73 | 74 | /** 75 | * @return bool 76 | */ 77 | public function hasTag() 78 | { 79 | return null !== $this->tag; 80 | } 81 | } 82 | -------------------------------------------------------------------------------- /tests/Schema/SchemaDefinitionUnitTest.php: -------------------------------------------------------------------------------- 1 | 7 | * 8 | * For the full copyright and license information, please view the LICENSE 9 | * file that was distributed with this source code. 10 | */ 11 | 12 | namespace GraphAware\Common\Tests\Schema; 13 | 14 | use GraphAware\Common\Schema\SchemaDefinition; 15 | use GraphAware\Common\Schema\IndexDefinition; 16 | use GraphAware\Common\Graph\Label; 17 | use GraphAware\Common\Schema\ConstraintType; 18 | 19 | /** 20 | * @group unit 21 | * @group schema 22 | */ 23 | class SchemaDefinitionUnitTest extends \PHPUnit_Framework_TestCase 24 | { 25 | public function testInstance() 26 | { 27 | $def = new SchemaDefinition(); 28 | $this->assertInstanceOf(SchemaDefinition::class, $def); 29 | $this->assertFalse($def->hasIndexes()); 30 | $this->assertFalse($def->hasUniqueConstraints()); 31 | } 32 | 33 | public function testAddIndexDefinition() 34 | { 35 | $def = new SchemaDefinition(); 36 | $def->addIndex(new IndexDefinition(Label::label("User"), "login")); 37 | $this->assertCount(1, $def->getIndexes()); 38 | $this->assertTrue($def->hasIndexes()); 39 | $this->assertFalse($def->hasUniqueConstraints()); 40 | $this->assertTrue($def->hasIndex(Label::label("User"), "login")); 41 | } 42 | 43 | public function testUniqueChecks() 44 | { 45 | $def = new SchemaDefinition(); 46 | $def->addIndex(new IndexDefinition(Label::label("User"), "login", ConstraintType::UNIQUENESS())); 47 | $this->assertTrue($def->hasUniqueConstraints()); 48 | $this->assertCount(1, $def->getIndexes()); 49 | $this->assertCount(1, $def->getUniqueConstraints()); 50 | } 51 | } -------------------------------------------------------------------------------- /tests/Cypher/StatementCollectionUnitTest.php: -------------------------------------------------------------------------------- 1 | 7 | * 8 | * For the full copyright and license information, please view the LICENSE 9 | * file that was distributed with this source code. 10 | */ 11 | 12 | namespace GraphAware\Common\Tests\Cypher; 13 | 14 | use GraphAware\Common\Cypher\Statement; 15 | use GraphAware\Common\Cypher\StatementCollection; 16 | 17 | /** 18 | * @group unit 19 | * @group cypher 20 | */ 21 | class StatementCollectionUnitTest extends \PHPUnit_Framework_TestCase 22 | { 23 | public function testNewInstance() 24 | { 25 | $coll = new StatementCollection(); 26 | $this->assertInstanceOf(StatementCollection::class, $coll); 27 | $this->assertTrue($coll->isEmpty()); 28 | $this->assertEquals(0, $coll->getCount()); 29 | $this->assertCount(0, $coll->getStatements()); 30 | $this->assertFalse($coll->hasTag()); 31 | $this->assertNull($coll->getTag()); 32 | } 33 | 34 | public function testAddStatements() 35 | { 36 | $coll = new StatementCollection(); 37 | $coll->add($this->getStatement()); 38 | $this->assertFalse($coll->isEmpty()); 39 | $this->assertEquals(1, $coll->getCount()); 40 | $this->assertCount(1, $coll->getStatements()); 41 | $coll->add($this->getStatement()); 42 | $this->assertEquals(2, $coll->getCount()); 43 | } 44 | 45 | public function testTaggedCollection() 46 | { 47 | $coll = new StatementCollection("test"); 48 | $this->assertTrue($coll->hasTag()); 49 | $this->assertEquals("test", $coll->getTag()); 50 | } 51 | 52 | private function getStatement() 53 | { 54 | return Statement::create("MATCH (n) RETURN n"); 55 | } 56 | } -------------------------------------------------------------------------------- /tests/Result/StatementStatisticsUnitTest.php: -------------------------------------------------------------------------------- 1 | 7 | * 8 | * For the full copyright and license information, please view the LICENSE 9 | * file that was distributed with this source code. 10 | */ 11 | 12 | namespace GraphAware\Common\Tests\Result; 13 | 14 | use GraphAware\Common\Result\StatementStatistics; 15 | use GraphAware\Common\Result\StatementStatisticsInterface; 16 | use InvalidArgumentException; 17 | 18 | /** 19 | * @group unit 20 | * @group result 21 | * @group summary 22 | */ 23 | class StatementStatisticsUnitTest extends \PHPUnit_Framework_TestCase 24 | { 25 | public function testNoStats() 26 | { 27 | $stats = new StatementStatistics(); 28 | $this->assertInstanceOf(StatementStatisticsInterface::class, $stats); 29 | $this->assertFalse($stats->containsUpdates()); 30 | $this->assertEquals(0, $stats->nodesCreated()); 31 | } 32 | 33 | public function testStatsAreMerged() 34 | { 35 | $stats = new StatementStatistics([ 36 | 'contains_updates' => true, 37 | 'nodes_created' => 10, 38 | 'properties_set' => 90 39 | ]); 40 | $this->assertTrue($stats->containsUpdates()); 41 | $this->assertEquals(10, $stats->nodesCreated()); 42 | $this->assertEquals(90, $stats->propertiesSet()); 43 | } 44 | 45 | public function testExceptionIsThrownWhenInvalidKey() 46 | { 47 | $this->setExpectedException(InvalidArgumentException::class); 48 | $stats = new StatementStatistics(['inv' => 10]); 49 | } 50 | 51 | public function testStatsReturnsUpdatesTrueIfItHappensButNotProvided() 52 | { 53 | $stats = [ 54 | 'nodes_created' => 1 55 | ]; 56 | $o = new StatementStatistics($stats); 57 | $this->assertTrue($o->containsUpdates()); 58 | } 59 | } -------------------------------------------------------------------------------- /src/Connection/BaseConfiguration.php: -------------------------------------------------------------------------------- 1 | 11 | */ 12 | class BaseConfiguration implements ConfigInterface 13 | { 14 | /** 15 | * @var array 16 | */ 17 | private $data; 18 | 19 | /** 20 | * Init the configuration with some data. 21 | * 22 | * @param array $data 23 | */ 24 | protected function __construct(array $data = []) 25 | { 26 | $this->data = $data; 27 | } 28 | 29 | /** 30 | * Factory function for the configuration. 31 | * 32 | * @return static 33 | */ 34 | public static function create() 35 | { 36 | return new static(); 37 | } 38 | 39 | /** 40 | * @param string $name 41 | * @param mixed $default value when $name is not defined 42 | * 43 | * @return mixed 44 | */ 45 | public function getValue($name, $default = null) 46 | { 47 | if (!isset($this->data[$name])) { 48 | return $default; 49 | } 50 | 51 | return $this->data[$name]; 52 | } 53 | 54 | /** 55 | * Does the data exist in the configuration? 56 | * 57 | * @param string $name 58 | * 59 | * @return bool 60 | */ 61 | public function hasValue($name) 62 | { 63 | return isset($this->data[$name]); 64 | } 65 | 66 | /** 67 | * Create a new Configuration with a new $value for key $name. 68 | * Any existing value for $name will be replaced in the new Configuration instance. 69 | * 70 | * @param string $name 71 | * @param mixed $value 72 | * 73 | * @return static 74 | */ 75 | public function setValue($name, $value) 76 | { 77 | $new = clone $this; 78 | $new->data[$name] = $value; 79 | 80 | return $new; 81 | } 82 | } 83 | -------------------------------------------------------------------------------- /tests/Result/ResultCollectionTest.php: -------------------------------------------------------------------------------- 1 | add($result); 25 | 26 | $this->assertEquals(1, $coll->size()); 27 | $this->assertEquals(null, $coll->get('non existing tag', null)); 28 | } 29 | 30 | public function testCollectionParseTags() 31 | { 32 | $statement1 = Statement::create("MATCH (n) RETURN count(n)", array(), 'tag1'); 33 | $statement2 = Statement::create("CREATE (n:Node)", array(), 'tag2'); 34 | $result1 = new RecordCursor($statement1); 35 | $result2 = new RecordCursor($statement2); 36 | 37 | $coll = new ResultCollection(); 38 | $coll->add($result1); 39 | $coll->add($result2); 40 | 41 | $this->assertEquals("MATCH (n) RETURN count(n)", $coll->get('tag1')->statement()->text()); 42 | } 43 | 44 | public function testCollectionOfResultsIsTraversable() 45 | { 46 | $statement1 = Statement::create("MATCH (n) RETURN count(n)", array(), 'tag1'); 47 | $statement2 = Statement::create("CREATE (n:Node)", array(), 'tag2'); 48 | $result1 = new RecordCursor($statement1); 49 | $result2 = new RecordCursor($statement2); 50 | 51 | $coll = new ResultCollection(); 52 | $coll->add($result1); 53 | $coll->add($result2); 54 | 55 | foreach ($coll as $result) { 56 | $this->assertInstanceOf(RecordCursorInterface::class, $result); 57 | } 58 | } 59 | } -------------------------------------------------------------------------------- /src/Type/MapAccessor.php: -------------------------------------------------------------------------------- 1 | 7 | * 8 | * For the full copyright and license information, please view the LICENSE 9 | * file that was distributed with this source code. 10 | */ 11 | 12 | namespace GraphAware\Common\Type; 13 | 14 | interface MapAccessor 15 | { 16 | /** 17 | * Retrieve the keys of the underlying map. 18 | * 19 | * @return array 20 | */ 21 | public function keys(); 22 | 23 | /** 24 | * Check if the list of keys contains the given key. 25 | * 26 | * @param string $key 27 | * 28 | * @return bool 29 | */ 30 | public function containsKey($key); 31 | 32 | /** 33 | * Retrieve the value of the property with the given key. 34 | * 35 | * @param string $key 36 | * 37 | * @return mixed 38 | */ 39 | public function get($key); 40 | 41 | /** 42 | * Wrapping method for $this->get(). 43 | * 44 | * @param string $name 45 | * 46 | * @return mixed 47 | */ 48 | public function __get($name); 49 | 50 | /** 51 | * Returns whether or not the property map contains the given key. 52 | * 53 | * @param string $key 54 | * 55 | * @return bool 56 | */ 57 | public function hasValue($key); 58 | 59 | /** 60 | * Returns the value for the given key, throws an exception if the key doesn't exist or returns the default given value if passed. 61 | * 62 | * @param string $key 63 | * @param mixed $default 64 | * 65 | * @return mixed 66 | * 67 | * @throws \InvalidArgumentException 68 | */ 69 | public function value($key, $default = null); 70 | 71 | /** 72 | * Retrieve all values of the underlying collection. 73 | * 74 | * @return array 75 | */ 76 | public function values(); 77 | 78 | /** 79 | * Returns a map of key value pairs of the underlying collection. 80 | * 81 | * @return array 82 | */ 83 | public function asArray(); 84 | } 85 | -------------------------------------------------------------------------------- /src/Graph/RelationshipInterface.php: -------------------------------------------------------------------------------- 1 | 7 | * 8 | * For the full copyright and license information, please view the LICENSE 9 | * file that was distributed with this source code. 10 | */ 11 | 12 | namespace GraphAware\Common\Graph; 13 | 14 | interface RelationshipInterface extends PropertyBagInterface 15 | { 16 | /** 17 | * Returns the Relationship internal id. 18 | * 19 | * @return int 20 | */ 21 | public function getId(); 22 | 23 | /** 24 | * Returns the Relationship type. 25 | * 26 | * @return RelationshipType 27 | */ 28 | public function getType(); 29 | 30 | /** 31 | * Returns the start node of the Relationship. 32 | * 33 | * @return NodeInterface 34 | */ 35 | public function getStartNode(); 36 | 37 | /** 38 | * Returns the end node of the Relationship. 39 | * 40 | * @return NodeInterface 41 | */ 42 | public function getEndNode(); 43 | 44 | /** 45 | * Returns the other node of the Relationship, based on the given Node. 46 | * 47 | * @param NodeInterface $node 48 | * 49 | * @return NodeInterface 50 | * 51 | * @throws \InvalidArgumentException When the given node does not make part of the Relationship 52 | */ 53 | public function getOtherNode(NodeInterface $node); 54 | 55 | /** 56 | * Returns the direction of the Relationship for a Node point of view. 57 | * 58 | * @param NodeInterface $node 59 | */ 60 | public function getDirection(NodeInterface $node); 61 | 62 | /** 63 | * Returns the nodes bound to this Relationship. 64 | * 65 | * @return NodeInterface[] 66 | */ 67 | public function getNodes(); 68 | 69 | /** 70 | * Returns whether or not the Relationship is of the given relationshipType. 71 | * 72 | * @param RelationshipType $relationshipType 73 | * 74 | * @return bool 75 | */ 76 | public function isType(RelationshipType $relationshipType); 77 | } 78 | -------------------------------------------------------------------------------- /src/Schema/IndexDefinition.php: -------------------------------------------------------------------------------- 1 | 7 | * 8 | * For the full copyright and license information, please view the LICENSE 9 | * file that was distributed with this source code. 10 | */ 11 | 12 | namespace GraphAware\Common\Schema; 13 | 14 | use GraphAware\Common\Graph\Label; 15 | 16 | class IndexDefinition 17 | { 18 | /** 19 | * @var Label 20 | */ 21 | protected $label; 22 | 23 | /** 24 | * @var string 25 | */ 26 | protected $property; 27 | 28 | /** 29 | * @var null|ConstraintType 30 | */ 31 | protected $constraintType; 32 | 33 | /** 34 | * @param Label $label 35 | * @param string $property 36 | * @param ConstraintType|null $constraintType 37 | */ 38 | public function __construct(Label $label, $property, ConstraintType $constraintType = null) 39 | { 40 | $this->label = $label; 41 | $this->property = (string) $property; 42 | $this->constraintType = $constraintType; 43 | } 44 | 45 | /** 46 | * Returns the label on which the index is created. 47 | * 48 | * @return Label 49 | */ 50 | public function getLabel() 51 | { 52 | return $this->label; 53 | } 54 | 55 | /** 56 | * Returns the property on which the Index is created. 57 | * 58 | * @return string 59 | */ 60 | public function getProperty() 61 | { 62 | return $this->property; 63 | } 64 | 65 | /** 66 | * Returns the ConstraintType of the Index. 67 | * 68 | * @return ConstraintType|null 69 | */ 70 | public function getConstraintType() 71 | { 72 | return $this->constraintType; 73 | } 74 | 75 | /** 76 | * Returns whether or not this index is a Uniqueness Constraint. 77 | * 78 | * @return bool 79 | */ 80 | public function isUnique() 81 | { 82 | return (string) $this->constraintType === ConstraintType::UNIQUENESS; 83 | } 84 | 85 | /** 86 | * Returns whether or not this index is a NodePropertyExistence Constraint. 87 | * 88 | * @return bool 89 | */ 90 | public function isNodePropertyExistence() 91 | { 92 | return $this->constraintType === ConstraintType::NODE_PROPERTY_EXISTENCE; 93 | } 94 | } 95 | -------------------------------------------------------------------------------- /tests/Graph/NodeUnitTest.php: -------------------------------------------------------------------------------- 1 | 7 | * 8 | * For the full copyright and license information, please view the LICENSE 9 | * file that was distributed with this source code. 10 | */ 11 | 12 | namespace GraphAware\Common\Tests\Graph; 13 | 14 | use GraphAware\Common\Graph\Node; 15 | use GraphAware\Common\Graph\Label; 16 | use GraphAware\Common\Graph\NodeInterface; 17 | use GraphAware\Common\Graph\PropertyBagInterface; 18 | use GraphAware\Common\Graph\Relationship; 19 | use GraphAware\Common\Graph\RelationshipType; 20 | use \InvalidArgumentException; 21 | 22 | /** 23 | * @group unit 24 | * @group graph 25 | */ 26 | class NodeUnitTest extends \PHPUnit_Framework_TestCase 27 | { 28 | public function testNewInstance() 29 | { 30 | $node = new Node(1); 31 | $this->assertInstanceOf(NodeInterface::class, $node); 32 | $this->assertInstanceOf(PropertyBagInterface::class, $node); 33 | $this->assertEquals(1, $node->getId()); 34 | $this->assertCount(0, $node->getProperties()); 35 | $this->assertCount(0, $node->getLabels()); 36 | $this->assertCount(0, $node->getRelationships()); 37 | $this->assertFalse($node->hasLabel("Cool")); 38 | $this->assertFalse($node->hasRelationships()); 39 | } 40 | 41 | public function testNodeWithLabels() 42 | { 43 | $node = new Node(1, array("User", "Person")); 44 | $this->assertCount(2, $node->getLabels()); 45 | $this->assertInstanceOf(Label::class, $node->getLabels()[0]); 46 | $this->assertEquals("User", $node->getLabels()[0]); 47 | $this->assertEquals("Person", $node->getLabels()[1]); 48 | $this->assertTrue($node->hasLabel("Person")); 49 | } 50 | 51 | public function testAddRelationships() 52 | { 53 | $node = new Node(1); 54 | $node->addRelationship(new Relationship(1, RelationshipType::withName("RELATES"), $node, $node)); 55 | $this->assertTrue($node->hasRelationships()); 56 | $this->assertCount(1, $node->getRelationships()); 57 | } 58 | 59 | public function testThrowExceptionWhenNonRelsObjectsArePassedOnConstruct() 60 | { 61 | $rels = [1]; 62 | $this->setExpectedException(InvalidArgumentException::class); 63 | $node = new Node(1, array(), $rels); 64 | } 65 | } -------------------------------------------------------------------------------- /src/Graph/Node.php: -------------------------------------------------------------------------------- 1 | 7 | * 8 | * For the full copyright and license information, please view the LICENSE 9 | * file that was distributed with this source code. 10 | */ 11 | 12 | namespace GraphAware\Common\Graph; 13 | 14 | /** 15 | * Node representation class. 16 | */ 17 | class Node extends PropertyBag implements NodeInterface 18 | { 19 | /** 20 | * @var int 21 | */ 22 | protected $id; 23 | 24 | /** 25 | * @var Label[] 26 | */ 27 | protected $labels = []; 28 | 29 | /** 30 | * @var RelationshipInterface[] 31 | */ 32 | protected $relationships = []; 33 | 34 | /** 35 | * @param int $id 36 | * @param array $labels 37 | * @param array $relationships 38 | */ 39 | public function __construct($id, array $labels = array(), array $relationships = array()) 40 | { 41 | $this->id = $id; 42 | 43 | foreach ($labels as $label) { 44 | $this->labels[] = new Label($label); 45 | } 46 | 47 | foreach ($relationships as $relationship) { 48 | if (!$relationship instanceof RelationshipInterface) { 49 | throw new \InvalidArgumentException(sprintf('Relationship must implement RelationshipInterface, "%s" given', json_encode($relationship))); 50 | } 51 | 52 | $this->relationships[] = $relationship; 53 | } 54 | 55 | parent::__construct(); 56 | } 57 | 58 | /** 59 | * {@inheritdoc} 60 | */ 61 | public function getId() 62 | { 63 | return $this->id; 64 | } 65 | 66 | /** 67 | * {@inheritdoc} 68 | */ 69 | public function getLabels() 70 | { 71 | return $this->labels; 72 | } 73 | 74 | /** 75 | * {@inheritdoc} 76 | */ 77 | public function hasLabel($name) 78 | { 79 | return in_array($name, $this->labels); 80 | } 81 | 82 | /** 83 | * {@inheritdoc} 84 | */ 85 | public function getRelationships() 86 | { 87 | return $this->relationships; 88 | } 89 | 90 | /** 91 | * {@inheritdoc} 92 | */ 93 | public function hasRelationships() 94 | { 95 | return !empty($this->relationships); 96 | } 97 | 98 | /** 99 | * {@inheritdoc} 100 | */ 101 | public function addRelationship(RelationshipInterface $relationship) 102 | { 103 | $this->relationships[] = $relationship; 104 | } 105 | } 106 | -------------------------------------------------------------------------------- /src/Schema/SchemaDefinition.php: -------------------------------------------------------------------------------- 1 | 7 | * 8 | * For the full copyright and license information, please view the LICENSE 9 | * file that was distributed with this source code. 10 | */ 11 | 12 | namespace GraphAware\Common\Schema; 13 | 14 | use GraphAware\Common\Graph\Label; 15 | 16 | class SchemaDefinition 17 | { 18 | /** 19 | * @var IndexDefinition[] 20 | */ 21 | protected $indexes = []; 22 | 23 | /** 24 | * @param IndexDefinition $index 25 | */ 26 | public function addIndex(IndexDefinition $index) 27 | { 28 | $this->indexes[] = $index; 29 | } 30 | 31 | /** 32 | * Returns all Indexes of the Schema. 33 | * 34 | * @return IndexDefinition[] 35 | */ 36 | public function getIndexes() 37 | { 38 | return $this->indexes; 39 | } 40 | 41 | /** 42 | * Returns whether or not the Schema contains Indexes. 43 | * 44 | * @return bool 45 | */ 46 | public function hasIndexes() 47 | { 48 | return !empty($this->indexes); 49 | } 50 | 51 | /** 52 | * Returns whether or not the Schema contains an Index for the given Label and property combination. 53 | * 54 | * @param Label $label 55 | * @param string $property 56 | * 57 | * @return bool 58 | */ 59 | public function hasIndex(Label $label, $property) 60 | { 61 | foreach ($this->indexes as $index) { 62 | if ($index->getLabel()->getName() === $label->getName() && $index->getProperty() === $property) { 63 | return true; 64 | } 65 | } 66 | 67 | return false; 68 | } 69 | 70 | /** 71 | * Returns whether or not the Schema contains Indexes with Uniqueness ConstraintType. 72 | * 73 | * @return bool 74 | */ 75 | public function hasUniqueConstraints() 76 | { 77 | foreach ($this->indexes as $index) { 78 | if ($index->isUnique()) { 79 | return true; 80 | } 81 | } 82 | 83 | return false; 84 | } 85 | 86 | /** 87 | * @return IndexDefinition[] 88 | */ 89 | public function getUniqueConstraints() 90 | { 91 | $indexes = []; 92 | foreach ($this->indexes as $index) { 93 | if ($index->isUnique()) { 94 | $indexes[] = $index; 95 | } 96 | } 97 | 98 | return $indexes; 99 | } 100 | } 101 | -------------------------------------------------------------------------------- /tests/Graph/RelationshipUnitTest.php: -------------------------------------------------------------------------------- 1 | 7 | * 8 | * For the full copyright and license information, please view the LICENSE 9 | * file that was distributed with this source code. 10 | */ 11 | 12 | namespace GraphAware\Common\Tests\Graph; 13 | 14 | use GraphAware\Common\Graph\Direction; 15 | use GraphAware\Common\Graph\Node; 16 | use GraphAware\Common\Graph\NodeInterface; 17 | use GraphAware\Common\Graph\Relationship; 18 | use GraphAware\Common\Graph\RelationshipType; 19 | use GraphAware\Common\Graph\RelationshipInterface; 20 | use GraphAware\Common\Graph\PropertyBagInterface; 21 | use \InvalidArgumentException; 22 | 23 | /** 24 | * @group unit 25 | * @group graph 26 | */ 27 | class RelationshipUnitTest extends \PHPUnit_Framework_TestCase 28 | { 29 | public function testNewInstance() 30 | { 31 | $rel = new Relationship(rand(), RelationshipType::withName("RELATES"), $this->getRandomNode(), $this->getRandomNode()); 32 | $this->assertInstanceOf(RelationshipInterface::class, $rel); 33 | $this->assertInstanceOf(PropertyBagInterface::class, $rel); 34 | $this->assertInternalType('integer', $rel->getId()); 35 | $this->assertEquals("RELATES", $rel->getType()); 36 | $this->assertInstanceOf(NodeInterface::class, $rel->getStartNode()); 37 | $this->assertInstanceOf(NodeInterface::class, $rel->getEndNode()); 38 | $this->assertTrue($rel->isType(RelationshipType::withName("RELATES"))); 39 | } 40 | 41 | public function testGetOtherNode() 42 | { 43 | $n1 = $this->getRandomNode(); 44 | $n2 = $this->getRandomNode(); 45 | $n3 = $this->getRandomNode(); 46 | $rel = new Relationship(1, RelationshipType::withName("RELATES"), $n1, $n2); 47 | $this->assertEquals($n2, $rel->getOtherNode($n1)); 48 | $this->assertEquals($n1, $rel->getOtherNode($n2)); 49 | $this->setExpectedException(InvalidArgumentException::class); 50 | $rel->getOtherNode($n3); 51 | } 52 | 53 | public function testGetDirection() 54 | { 55 | $n1 = $this->getRandomNode(); 56 | $n2 = $this->getRandomNode(); 57 | $n3 = $this->getRandomNode(); 58 | $rel = new Relationship(1, RelationshipType::withName("RELATES"), $n1, $n2); 59 | $this->assertEquals(Direction::OUTGOING, $rel->getDirection($n1)); 60 | $this->assertEquals(Direction::INCOMING, $rel->getDirection($n2)); 61 | $this->setExpectedException(InvalidArgumentException::class); 62 | $rel->getDirection($n3); 63 | } 64 | 65 | private function getRandomNode() 66 | { 67 | return new Node(rand()); 68 | } 69 | } -------------------------------------------------------------------------------- /src/Result/AbstractRecordView.php: -------------------------------------------------------------------------------- 1 | 7 | * 8 | * For the full copyright and license information, please view the LICENSE 9 | * file that was distributed with this source code. 10 | */ 11 | 12 | namespace GraphAware\Common\Result; 13 | 14 | abstract class AbstractRecordView implements RecordViewInterface 15 | { 16 | /** 17 | * @var array 18 | */ 19 | protected $keys = []; 20 | 21 | /** 22 | * @var array 23 | */ 24 | protected $values = []; 25 | 26 | /** 27 | * @var array 28 | */ 29 | protected $indexMap = []; 30 | 31 | /** 32 | * @param array $keys 33 | * @param array $values 34 | */ 35 | public function __construct(array $keys, array $values) 36 | { 37 | $this->keys = $keys; 38 | $this->values = $values; 39 | 40 | foreach ($this->values as $k => $value) { 41 | $this->indexMap[] = $k; 42 | } 43 | } 44 | 45 | /** 46 | * {@inheritdoc} 47 | */ 48 | public function keys() 49 | { 50 | return $this->keys; 51 | } 52 | 53 | /** 54 | * @return mixed[]|\GraphAware\Common\Type\Node[]|\GraphAware\Common\Type\Relationship[]|\GraphAware\Common\Type\Path[] 55 | */ 56 | public function values() 57 | { 58 | return $this->values; 59 | } 60 | 61 | /** 62 | * {@inheritdoc} 63 | */ 64 | public function hasValues() 65 | { 66 | return !empty($this->values); 67 | } 68 | 69 | /** 70 | * @param string $key 71 | * 72 | * @return mixed|\GraphAware\Common\Type\Node|\GraphAware\Common\Type\Relationship|\GraphAware\Common\Type\Path| 73 | */ 74 | public function value($key) 75 | { 76 | if (!array_key_exists($key, $this->values)) { 77 | throw new \InvalidArgumentException(sprintf('No value with key "%s" in RecordView, possible keys are %s', $key, json_encode($this->keys))); 78 | } 79 | 80 | return $this->values[$key]; 81 | } 82 | 83 | /** 84 | * @param $index 85 | * 86 | * @return mixed|\GraphAware\Common\Type\Node|\GraphAware\Common\Type\Relationship|\GraphAware\Common\Type\Path 87 | */ 88 | public function valueByIndex($index) 89 | { 90 | if (!array_key_exists($index, $this->indexMap)) { 91 | throw new \InvalidArgumentException(sprintf('No value with index "%d" in RecordView, possible indexes are %s', $index, json_encode($this->keys))); 92 | } 93 | 94 | return $this->values[$this->indexMap[$index]]; 95 | } 96 | 97 | /** 98 | * @return \GraphAware\Common\Result\AbstractRecordView 99 | */ 100 | public function record() 101 | { 102 | return clone $this; 103 | } 104 | } 105 | -------------------------------------------------------------------------------- /tests/Cypher/StatementUnitTest.php: -------------------------------------------------------------------------------- 1 | 7 | * 8 | * For the full copyright and license information, please view the LICENSE 9 | * file that was distributed with this source code. 10 | */ 11 | 12 | namespace GraphAware\Common\Tests\Cypher; 13 | 14 | use GraphAware\Common\Cypher\Statement; 15 | use GraphAware\Common\Cypher\StatementCollection; 16 | use GraphAware\Common\Cypher\StatementType; 17 | use InvalidArgumentException; 18 | 19 | /** 20 | * @group unit 21 | * @group cypher 22 | * @group tck 23 | */ 24 | class StatementUnitTest extends \PHPUnit_Framework_TestCase 25 | { 26 | public function testStatementInstance() 27 | { 28 | $q = $this->text(); 29 | $st = Statement::create($q); 30 | $this->assertInstanceOf(Statement::class, $st); 31 | $this->assertEquals($q, $st->text()); 32 | $this->assertCount(0, $st->parameters()); 33 | $this->assertFalse($st->hasTag()); 34 | } 35 | 36 | public function testStatementWithParams() 37 | { 38 | $st = Statement::create($this->text(), $this->getParams()); 39 | $this->assertCount(1, $st->parameters()); 40 | } 41 | 42 | public function testStatementTagged() 43 | { 44 | $st = Statement::create($this->text(), $this->getParams(), "test"); 45 | $this->assertEquals("test", $st->getTag()); 46 | $this->assertTrue($st->hasTag()); 47 | } 48 | 49 | public function testStatementTypeIsWriteByDefault() 50 | { 51 | $st = Statement::create($this->text()); 52 | $this->assertEquals(StatementType::READ_WRITE, $st->statementType()); 53 | } 54 | 55 | public function testStatementCanBeDefinedAsRead() 56 | { 57 | $st = Statement::create($this->text(), array(), null, StatementType::READ_ONLY); 58 | $this->assertEquals(StatementType::READ_ONLY, $st->statementType()); 59 | } 60 | 61 | public function testExceptionIsThrownWhenInvalidTypeIsGiven() 62 | { 63 | $this->setExpectedException(InvalidArgumentException::class); 64 | Statement::create($this->text(), $this->getParams(), null, "Invalid"); 65 | } 66 | 67 | public function testCanPrepareStatement() 68 | { 69 | $statement = Statement::prepare('text'); 70 | $this->assertInstanceOf(Statement::class, $statement); 71 | } 72 | 73 | public function testImmutableStatementAPI() 74 | { 75 | $statement = Statement::create($this->text()); 76 | $newText = 'CREATE (n:Node) RETURN n'; 77 | $st = $statement->withText($newText); 78 | $this->assertEquals($newText, $st->text()); 79 | $this->assertEquals($this->text(), $statement->text()); 80 | $st2 = $st->withParameters(['name' => 'johndoe', 'company' => 'GraphAware']); 81 | $this->assertCount(0, $st->parameters()); 82 | $this->assertCount(2, $st2->parameters()); 83 | $this->assertEquals($newText, $st2->text()); 84 | $st3 = $st2->withUpdatedParameters(['name' => 'johndoe', 'company' => 'GraphAware Limited']); 85 | $this->assertEquals('GraphAware Limited', $st3->parameters()['company']); 86 | } 87 | 88 | private function text() 89 | { 90 | $q = "MATCH (n) RETURN count(n)"; 91 | 92 | return $q; 93 | } 94 | 95 | private function getParams() 96 | { 97 | return array('id' => 1); 98 | } 99 | } -------------------------------------------------------------------------------- /src/Result/Record.php: -------------------------------------------------------------------------------- 1 | 7 | * 8 | * For the full copyright and license information, please view the LICENSE 9 | * file that was distributed with this source code. 10 | */ 11 | 12 | namespace GraphAware\Common\Result; 13 | 14 | interface Record 15 | { 16 | /** 17 | * Returns the keys of the values. 18 | * 19 | * @return array 20 | */ 21 | public function keys(); 22 | 23 | /** 24 | * Returns whether or not this Record has any value. 25 | * 26 | * @return bool 27 | */ 28 | public function hasValues(); 29 | 30 | /** 31 | * Returns the value for the given key. 32 | * 33 | * @param string $key 34 | * 35 | * @return mixed 36 | * 37 | * @deprecated Use get() instead. 38 | */ 39 | public function value($key); 40 | 41 | /** 42 | * Retrieve the value for the given key. 43 | * 44 | * @param string $key The identifier key 45 | * @param mixed $defaultValue A default value to return in case the record doesn't contains the given key. 46 | * 47 | * @return mixed 48 | */ 49 | public function get($key, $defaultValue = null); 50 | 51 | /** 52 | * Returns whether or not this Record contains a value with the given key. 53 | * 54 | * @param string $key 55 | * 56 | * @return bool 57 | */ 58 | public function hasValue($key); 59 | 60 | /** 61 | * Same as value($key) but will return the value as a NodeInterface object if the type of the 62 | * value is a Node, throws an exception otherwise. 63 | * 64 | * @param string $key 65 | * 66 | * @return \GraphAware\Common\Type\Node 67 | */ 68 | public function nodeValue($key); 69 | 70 | /** 71 | * Same as value($key) but will return the value as a RelationshipInterface object if the type of the 72 | * value is a Relationship, throws an exception otherwise. 73 | * 74 | * @param string $key 75 | * 76 | * @return \GraphAware\Common\Type\Relationship 77 | */ 78 | public function relationshipValue($key); 79 | 80 | /** 81 | * Same as value($key) but will return the value as a PathInterface object if the type of the 82 | * value is a Path, throws an exception otherwise. 83 | * 84 | * @param string $key 85 | * 86 | * @return \GraphAware\Common\Type\Path 87 | */ 88 | public function pathValue($key); 89 | 90 | /** 91 | * Returns all the values of this Record. 92 | * 93 | * @return array 94 | */ 95 | public function values(); 96 | 97 | /** 98 | * @param int $index 99 | * 100 | * @return mixed 101 | */ 102 | public function valueByIndex($index); 103 | 104 | /** 105 | * Retrieve the value at the given field index. 106 | * 107 | * @param int $index 108 | * 109 | * @return mixed 110 | */ 111 | public function getByIndex($index); 112 | 113 | /** 114 | * Returns a copy of this Record. 115 | * 116 | * @return RecordCursorInterface 117 | */ 118 | public function record(); 119 | } 120 | -------------------------------------------------------------------------------- /src/Graph/Relationship.php: -------------------------------------------------------------------------------- 1 | 7 | * 8 | * For the full copyright and license information, please view the LICENSE 9 | * file that was distributed with this source code. 10 | */ 11 | 12 | namespace GraphAware\Common\Graph; 13 | 14 | class Relationship extends PropertyBag implements RelationshipInterface 15 | { 16 | /** 17 | * @var int 18 | */ 19 | protected $id; 20 | 21 | /** 22 | * @var RelationshipType 23 | */ 24 | protected $type; 25 | 26 | /** 27 | * @var NodeInterface 28 | */ 29 | protected $startNode; 30 | 31 | /** 32 | * @var NodeInterface 33 | */ 34 | protected $endNode; 35 | 36 | /** 37 | * @param int $id 38 | * @param RelationshipType $relationshipType 39 | * @param NodeInterface $startNode 40 | * @param NodeInterface $endNode 41 | */ 42 | public function __construct($id, RelationshipType $relationshipType, NodeInterface $startNode, NodeInterface $endNode) 43 | { 44 | $this->id = $id; 45 | $this->type = $relationshipType; 46 | $this->startNode = $startNode; 47 | $this->endNode = $endNode; 48 | 49 | parent::__construct(); 50 | } 51 | 52 | /** 53 | * {@inheritdoc} 54 | */ 55 | public function getId() 56 | { 57 | return $this->id; 58 | } 59 | 60 | /** 61 | * {@inheritdoc} 62 | */ 63 | public function getType() 64 | { 65 | return $this->type; 66 | } 67 | 68 | /** 69 | * {@inheritdoc} 70 | */ 71 | public function getStartNode() 72 | { 73 | return $this->startNode; 74 | } 75 | 76 | /** 77 | * {@inheritdoc} 78 | */ 79 | public function getEndNode() 80 | { 81 | return $this->endNode; 82 | } 83 | 84 | /** 85 | * {@inheritdoc} 86 | */ 87 | public function getOtherNode(NodeInterface $node) 88 | { 89 | if ($node->getId() === $this->startNode->getId()) { 90 | return $this->endNode; 91 | } elseif ($node->getId() === $this->endNode->getId()) { 92 | return $this->startNode; 93 | } 94 | 95 | throw new \InvalidArgumentException(sprintf( 96 | 'The node with ID "%s" is not part of the relationship with ID "%s"', 97 | $node->getId(), 98 | $this->id 99 | )); 100 | } 101 | 102 | /** 103 | * {@inheritdoc} 104 | */ 105 | public function getDirection(NodeInterface $node) 106 | { 107 | if ($node !== $this->startNode && $node !== $this->endNode) { 108 | throw new \InvalidArgumentException(sprintf('The given node is not part of the Relationship')); 109 | } 110 | 111 | $direction = $node === $this->startNode ? Direction::OUTGOING() : Direction::INCOMING(); 112 | 113 | return $direction; 114 | } 115 | 116 | /** 117 | * {@inheritdoc} 118 | */ 119 | public function getNodes() 120 | { 121 | return array($this->startNode, $this->endNode); 122 | } 123 | 124 | /** 125 | * {@inheritdoc} 126 | */ 127 | public function isType(RelationshipType $relationshipType) 128 | { 129 | return $relationshipType->getName() === $this->type->getName(); 130 | } 131 | } 132 | -------------------------------------------------------------------------------- /src/Result/AbstractRecordCursor.php: -------------------------------------------------------------------------------- 1 | 7 | * 8 | * For the full copyright and license information, please view the LICENSE 9 | * file that was distributed with this source code. 10 | */ 11 | 12 | namespace GraphAware\Common\Result; 13 | 14 | use GraphAware\Common\Cypher\StatementInterface; 15 | 16 | abstract class AbstractRecordCursor implements RecordCursorInterface 17 | { 18 | /** 19 | * @var \GraphAware\Common\Cypher\StatementInterface 20 | */ 21 | protected $statement; 22 | 23 | /** 24 | * @var array 25 | */ 26 | protected $records = []; 27 | 28 | /** 29 | * @var ResultSummaryInterface 30 | */ 31 | protected $resultSummary; 32 | 33 | /** 34 | * @var bool 35 | */ 36 | protected $isOpen = true; 37 | 38 | /** 39 | * @var int 40 | */ 41 | protected $position = -1; 42 | 43 | /** 44 | * {@inheritdoc} 45 | */ 46 | public function __construct(StatementInterface $statement) 47 | { 48 | $this->statement = $statement; 49 | } 50 | 51 | /** 52 | * {@inheritdoc} 53 | */ 54 | public function statement() 55 | { 56 | return $this->statement; 57 | } 58 | 59 | /** 60 | * @param RecordViewInterface $record 61 | */ 62 | public function addRecord(RecordViewInterface $record) 63 | { 64 | $this->records[] = $record; 65 | } 66 | 67 | /** 68 | * {@inheritdoc} 69 | */ 70 | public function records() 71 | { 72 | return $this->records; 73 | } 74 | 75 | /** 76 | * @param ResultSummaryInterface $resultSummary 77 | */ 78 | public function setResultSummary(ResultSummaryInterface $resultSummary) 79 | { 80 | $this->resultSummary = $resultSummary; 81 | } 82 | 83 | /** 84 | * {@inheritdoc} 85 | */ 86 | public function summarize() 87 | { 88 | return $this->resultSummary; 89 | } 90 | 91 | /** 92 | * {@inheritdoc} 93 | */ 94 | public function hasSummary() 95 | { 96 | return $this->resultSummary instanceof ResultSummaryInterface; 97 | } 98 | 99 | /** 100 | * {@inheritdoc} 101 | */ 102 | public function isOpen() 103 | { 104 | return $this->isOpen; 105 | } 106 | 107 | /** 108 | * {@inheritdoc} 109 | */ 110 | public function close() 111 | { 112 | $this->isOpen = false; 113 | } 114 | 115 | /** 116 | * @return bool 117 | */ 118 | public function next() 119 | { 120 | if (false !== current($this->records)) { 121 | ++$this->position; 122 | 123 | return true; 124 | } 125 | 126 | return false; 127 | } 128 | 129 | /** 130 | * @return bool 131 | */ 132 | public function first() 133 | { 134 | return -1 === $this->position && $this->next() ? true : false; 135 | } 136 | 137 | /** 138 | * @return bool 139 | */ 140 | public function single() 141 | { 142 | return $this->first() && $this->isLast(); 143 | } 144 | 145 | /** 146 | * @return bool 147 | */ 148 | public function last() 149 | { 150 | while ($this->next()) { 151 | } 152 | 153 | return $this->position !== -1; 154 | } 155 | 156 | /** 157 | * @return bool 158 | */ 159 | public function isLast() 160 | { 161 | return $this->position === count($this->records) - 1; 162 | } 163 | } 164 | -------------------------------------------------------------------------------- /src/Result/ResultCollection.php: -------------------------------------------------------------------------------- 1 | 7 | * 8 | * For the full copyright and license information, please view the LICENSE 9 | * file that was distributed with this source code. 10 | */ 11 | 12 | namespace GraphAware\Common\Result; 13 | 14 | class ResultCollection implements \Iterator 15 | { 16 | /** 17 | * @var int 18 | */ 19 | protected $position = 0; 20 | 21 | /** 22 | * @var RecordCursorInterface[] 23 | */ 24 | protected $results = []; 25 | 26 | /** 27 | * @var array 28 | */ 29 | protected $tagMap = []; 30 | 31 | /** 32 | * Add a Result RecordCursorInterface to the ResultCollection. 33 | * 34 | * @param RecordCursorInterface $recordCursor 35 | * @param null|string $tag 36 | */ 37 | public function add(RecordCursorInterface $recordCursor, $tag = null) 38 | { 39 | $this->results[] = $recordCursor; 40 | 41 | if (null !== $tag) { 42 | $this->tagMap[$tag] = count($this->results) - 1; 43 | } elseif ($recordCursor->statement()->hasTag()) { 44 | $this->tagMap[$recordCursor->statement()->getTag()] = count($this->results()) - 1; 45 | } 46 | } 47 | 48 | /** 49 | * Returns a RecordCursorInterface (Result) for the given tag (tag passed along with the Cypher statement). 50 | * 51 | * @param string $tag 52 | * @param mixed $default 53 | * 54 | * @return RecordCursorInterface|null 55 | */ 56 | public function get($tag, $default = null) 57 | { 58 | if (array_key_exists($tag, $this->tagMap)) { 59 | return $this->results[$this->tagMap[$tag]]; 60 | } 61 | 62 | if (2 === func_num_args()) { 63 | return $default; 64 | } 65 | 66 | throw new \InvalidArgumentException(sprintf('This result collection does not contains a Result for tag "%s"', $tag)); 67 | } 68 | 69 | /** 70 | * @param $tag 71 | * 72 | * @return bool 73 | */ 74 | public function contains($tag) 75 | { 76 | return array_key_exists($tag, $this->tagMap); 77 | } 78 | 79 | /** 80 | * @return RecordCursorInterface[] 81 | */ 82 | public function results() 83 | { 84 | return $this->results; 85 | } 86 | 87 | /** 88 | * Returns the size of the results of this ResultCollection. 89 | * 90 | * @return int 91 | */ 92 | public function size() 93 | { 94 | return count($this->results); 95 | } 96 | 97 | /** 98 | * {@inheritdoc} 99 | */ 100 | public function rewind() 101 | { 102 | $this->position = 0; 103 | } 104 | 105 | /** 106 | * @return RecordCursorInterface 107 | */ 108 | public function current() 109 | { 110 | return $this->results[$this->position]; 111 | } 112 | 113 | /** 114 | * {@inheritdoc} 115 | */ 116 | public function key() 117 | { 118 | return $this->position; 119 | } 120 | 121 | /** 122 | * {@inheritdoc} 123 | */ 124 | public function next() 125 | { 126 | ++$this->position; 127 | } 128 | 129 | /** 130 | * {@inheritdoc} 131 | */ 132 | public function valid() 133 | { 134 | return isset($this->results[$this->position]); 135 | } 136 | 137 | /** 138 | * @return CombinedStatistics 139 | */ 140 | public function updateStatistics() 141 | { 142 | $combinedStats = new CombinedStatistics(); 143 | 144 | foreach ($this->results as $result) { 145 | if (null !== $result->summarize()->updateStatistics()) { 146 | $combinedStats->mergeStats($result->summarize()->updateStatistics()); 147 | } 148 | } 149 | 150 | return $combinedStats; 151 | } 152 | } 153 | -------------------------------------------------------------------------------- /src/Cypher/Statement.php: -------------------------------------------------------------------------------- 1 | 7 | * 8 | * For the full copyright and license information, please view the LICENSE 9 | * file that was distributed with this source code. 10 | */ 11 | 12 | namespace GraphAware\Common\Cypher; 13 | 14 | class Statement implements StatementInterface 15 | { 16 | /** 17 | * @var string 18 | */ 19 | protected $text; 20 | 21 | /** 22 | * @var array 23 | */ 24 | protected $parameters; 25 | 26 | /** 27 | * @var string|null 28 | */ 29 | protected $tag = null; 30 | 31 | /** 32 | * @var StatementType 33 | */ 34 | protected $type; 35 | 36 | /** 37 | * @param string $text 38 | * @param array $parameters 39 | * @param string|null $tag 40 | * @param StatementType 41 | */ 42 | private function __construct($text, array $parameters = array(), $tag = null, StatementType $statementType) 43 | { 44 | $this->text = (string) $text; 45 | $this->parameters = $parameters; 46 | $this->type = $statementType; 47 | 48 | if (null !== $tag) { 49 | $this->tag = (string) $tag; 50 | } 51 | } 52 | 53 | /** 54 | * @param string $text 55 | * @param array $parameters 56 | * @param string|null $tag 57 | * @param string $statementType 58 | * 59 | * @return Statement 60 | */ 61 | public static function create($text, array $parameters = array(), $tag = null, $statementType = StatementType::READ_WRITE) 62 | { 63 | if (!StatementType::isValid($statementType)) { 64 | throw new \InvalidArgumentException(sprintf('Value %s is invalid as statement type, possible values are %s', $statementType, json_encode(StatementType::keys()))); 65 | } 66 | $type = new StatementType($statementType); 67 | 68 | return new self($text, $parameters, $tag, $type); 69 | } 70 | 71 | /** 72 | * @param string $text 73 | * @param array $parameters 74 | * @param string|null $tag 75 | * 76 | * @return \GraphAware\Common\Cypher\Statement 77 | */ 78 | public static function prepare($text, array $parameters = array(), $tag = null) 79 | { 80 | $type = new StatementType(StatementType::READ_WRITE); 81 | 82 | return new self($text, $parameters, $tag, $type); 83 | } 84 | 85 | /** 86 | * {@inheritdoc} 87 | */ 88 | public function text() 89 | { 90 | return $this->text; 91 | } 92 | 93 | /** 94 | * {@inheritdoc} 95 | */ 96 | public function parameters() 97 | { 98 | return $this->parameters; 99 | } 100 | 101 | /** 102 | * {@inheritdoc} 103 | */ 104 | public function withText($text) 105 | { 106 | return new self($text, $this->parameters, $this->tag, $this->type); 107 | } 108 | 109 | /** 110 | * {@inheritdoc} 111 | */ 112 | public function withParameters(array $parameters) 113 | { 114 | return new self($this->text, $parameters, $this->tag, $this->type); 115 | } 116 | 117 | /** 118 | * {@inheritdoc} 119 | */ 120 | public function withUpdatedParameters(array $parameters) 121 | { 122 | $parameters = array_merge($this->parameters, $parameters); 123 | 124 | return new self($this->text, $parameters, $this->tag, $this->type); 125 | } 126 | 127 | /** 128 | * {@inheritdoc} 129 | */ 130 | public function getTag() 131 | { 132 | return $this->tag; 133 | } 134 | 135 | /** 136 | * {@inheritdoc} 137 | */ 138 | public function hasTag() 139 | { 140 | return null !== $this->tag; 141 | } 142 | 143 | /** 144 | * {@inheritdoc} 145 | */ 146 | public function statementType() 147 | { 148 | return $this->type; 149 | } 150 | } 151 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # GraphAware PHP Neo4j Common 2 | 3 | ## Library with common utility classes for Neo4j 4 | 5 | [![Build Status](https://travis-ci.org/graphaware/neo4j-php-commons.svg)](https://travis-ci.org/graphaware/neo4j-php-commons) 6 | [![Latest Stable Version](https://poser.pugx.org/graphaware/neo4j-common/version)](https://packagist.org/packages/graphaware/neo4j-common) 7 | 8 | ### Installation 9 | 10 | Require the dependencies in your application : 11 | 12 | ```bash 13 | composer require graphaware/neo4j-common 14 | ``` 15 | 16 | --- 17 | 18 | ### Graph 19 | 20 | #### Label 21 | 22 | Object representation of a Node Label. 23 | 24 | ```php 25 | 26 | use GraphAware\Common\Graph\Label; 27 | 28 | $label = new Label("User"); 29 | echo $label->getName(); // Returns (string) "User" 30 | 31 | // or static construction 32 | 33 | $label = Label::label("User"); 34 | ``` 35 | 36 | #### Node 37 | 38 | Object Representation of a Node. The node object extends `PropertyBag`. 39 | 40 | ```php 41 | 42 | use GraphAware\Common\Graph\Node; 43 | 44 | $node = new Node(1, array("User", "Person")); 45 | $node->getId(); // Returns (int) 1 46 | $node->getLabels(); // Returns an array of \GraphAware\Common\Graph\Label objects 47 | ``` 48 | 49 | #### Relationship 50 | 51 | Object Representation of a Relationship. The relationship object extends `PropertyBag`. 52 | 53 | ```php 54 | use GraphAware\Common\Graph\Relationship; 55 | 56 | $rel = new Relationship(1, RelationshipType::withName("RELATES"), $node, $node2); 57 | echo $rel->getType(); // Returns (string) "RELATES" 58 | var_dump($rel->isType(RelationshipType::withName("RELATES"))); // Returns (bool) true 59 | ``` 60 | 61 | #### Direction (Enum) : representation of a Relationship Direction 62 | 63 | ```php 64 | 65 | use GraphAware\Common\Graph\Direction; 66 | 67 | $direction = new Direction(Direction::INCOMING); 68 | echo $direction; // Returns (string) "INCOMING" 69 | 70 | // Or static call construction 71 | 72 | $direction = Direction::OUTGOING; 73 | echo $direction; // Returns (string) "OUTGOING" 74 | ``` 75 | 76 | Valid values are `INCOMING`, `OUTGOING` and `BOTH`. 77 | 78 | #### RelationshipType 79 | 80 | Object representation of a relationship type. 81 | 82 | ```php 83 | use GraphAware\Common\Graph\RelationshipType; 84 | 85 | $relType = RelationshipType::withName("FOLLOWS"); 86 | echo $relType->getName(); // Returns (string) "FOLLOWS" 87 | echo (string) $relType; // implements __toString method : Returns (string) "FOLLOWS" 88 | ``` 89 | --- 90 | 91 | ### Cypher 92 | 93 | #### Statement and StatementCollection 94 | 95 | Utility classes representing Cypher's statements. Both `Statement` and `StatementCollection` classes are 96 | `taggable`. 97 | 98 | Contains also `StatementInterface` and `StatementCollectionInterface` used in most GraphAware's PHP libraries. 99 | 100 | ##### Statement 101 | 102 | Represents a Cypher statement with a query and an array of parameters. Also the Statement accepts a `tag` argument default to null; 103 | 104 | ```php 105 | 106 | use GraphAware\Common\Cypher\Statement; 107 | 108 | $statement = Statement::create("MATCH (n) WHERE id(n) = {id} RETURN n", array("id" => 324)); 109 | 110 | echo $statement->getQuery(); // Returns (string) "MATCH (n) WHERE id(n) = {id} RETURN n" 111 | echo count($statement->getParameters()); // Returns (int) 1 112 | ``` 113 | 114 | ##### StatementCollection 115 | 116 | Represents a collection of `Statement` objects. Is also Taggable. 117 | 118 | ```php 119 | 120 | use GraphAware\Common\Cypher\Statement 121 | GraphAware\Common\Cypher\StatementCollection; 122 | 123 | $collection = new StatementCollection(); 124 | $collection->add(Statement::create("MATCH (n) RETURN count(n)")); 125 | 126 | print_r($collection->getStatements()); 127 | echo $collection->isEmpty(); 128 | ``` 129 | 130 | --- 131 | 132 | ## License 133 | 134 | ### Apache License 2.0 135 | 136 | ``` 137 | Copyright 2015 Graphaware Limited 138 | 139 | Licensed under the Apache License, Version 2.0 (the "License"); 140 | you may not use this file except in compliance with the License. 141 | You may obtain a copy of the License at 142 | 143 | http://www.apache.org/licenses/LICENSE-2.0 144 | 145 | Unless required by applicable law or agreed to in writing, software 146 | distributed under the License is distributed on an "AS IS" BASIS, 147 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 148 | See the License for the specific language governing permissions and 149 | limitations under the License. 150 | ``` 151 | 152 | --- 153 | 154 | ## Support 155 | 156 | Standard Community Support through the Github Issues and PR's workflow. 157 | 158 | Enterprise support via your first level support contact. -------------------------------------------------------------------------------- /src/Result/CombinedStatistics.php: -------------------------------------------------------------------------------- 1 | 7 | * 8 | * For the full copyright and license information, please view the LICENSE 9 | * file that was distributed with this source code. 10 | */ 11 | 12 | namespace GraphAware\Common\Result; 13 | 14 | class CombinedStatistics implements StatementStatisticsInterface 15 | { 16 | /** 17 | * @var bool 18 | */ 19 | protected $containsUpdates = false; 20 | 21 | /** 22 | * @var int 23 | */ 24 | protected $nodesCreated = 0; 25 | 26 | /** 27 | * @var int 28 | */ 29 | protected $nodesDeleted = 0; 30 | 31 | /** 32 | * @var int 33 | */ 34 | protected $relationshipsCreated = 0; 35 | 36 | /** 37 | * @var int 38 | */ 39 | protected $relationshipsDeleted = 0; 40 | 41 | /** 42 | * @var int 43 | */ 44 | protected $propertiesSet = 0; 45 | 46 | /** 47 | * @var int 48 | */ 49 | protected $labelsAdded = 0; 50 | 51 | /** 52 | * @var int 53 | */ 54 | protected $labelsRemoved = 0; 55 | 56 | /** 57 | * @var int 58 | */ 59 | protected $indexesAdded = 0; 60 | 61 | /** 62 | * @var int 63 | */ 64 | protected $indexesRemoved = 0; 65 | 66 | /** 67 | * @var int 68 | */ 69 | protected $constraintsAdded = 0; 70 | 71 | /** 72 | * @var int 73 | */ 74 | protected $constraintsRemoved = 0; 75 | 76 | /** 77 | * @param \GraphAware\Common\Result\StatementStatisticsInterface $resultStats 78 | */ 79 | public function mergeStats(StatementStatisticsInterface $resultStats) 80 | { 81 | if (!$this->containsUpdates) { 82 | $this->containsUpdates = $resultStats->containsUpdates(); 83 | } 84 | $this->nodesCreated += $resultStats->nodesCreated(); 85 | $this->nodesDeleted += $resultStats->nodesDeleted(); 86 | $this->relationshipsCreated += $resultStats->relationshipsCreated(); 87 | $this->relationshipsDeleted += $resultStats->relationshipsDeleted(); 88 | $this->propertiesSet += $resultStats->propertiesSet(); 89 | $this->labelsAdded += $resultStats->labelsAdded(); 90 | $this->labelsRemoved += $resultStats->labelsRemoved(); 91 | $this->indexesAdded += $resultStats->indexesAdded(); 92 | $this->indexesRemoved += $resultStats->indexesRemoved(); 93 | $this->constraintsAdded += $resultStats->constraintsAdded(); 94 | $this->constraintsRemoved += $resultStats->constraintsRemoved(); 95 | } 96 | 97 | /** 98 | * {@inheritdoc} 99 | */ 100 | public function containsUpdates() 101 | { 102 | return $this->containsUpdates; 103 | } 104 | 105 | /** 106 | * {@inheritdoc} 107 | */ 108 | public function nodesCreated() 109 | { 110 | return $this->nodesCreated; 111 | } 112 | 113 | /** 114 | * {@inheritdoc} 115 | */ 116 | public function nodesDeleted() 117 | { 118 | return $this->nodesDeleted; 119 | } 120 | 121 | /** 122 | * {@inheritdoc} 123 | */ 124 | public function relationshipsCreated() 125 | { 126 | return $this->relationshipsCreated; 127 | } 128 | 129 | /** 130 | * {@inheritdoc} 131 | */ 132 | public function relationshipsDeleted() 133 | { 134 | return $this->relationshipsDeleted; 135 | } 136 | 137 | /** 138 | * {@inheritdoc} 139 | */ 140 | public function propertiesSet() 141 | { 142 | return $this->propertiesSet; 143 | } 144 | 145 | /** 146 | * {@inheritdoc} 147 | */ 148 | public function labelsAdded() 149 | { 150 | return $this->labelsAdded; 151 | } 152 | 153 | /** 154 | * {@inheritdoc} 155 | */ 156 | public function labelsRemoved() 157 | { 158 | return $this->labelsRemoved; 159 | } 160 | 161 | /** 162 | * {@inheritdoc} 163 | */ 164 | public function indexesAdded() 165 | { 166 | return $this->indexesAdded; 167 | } 168 | 169 | /** 170 | * {@inheritdoc} 171 | */ 172 | public function indexesRemoved() 173 | { 174 | return $this->labelsRemoved; 175 | } 176 | 177 | /** 178 | * {@inheritdoc} 179 | */ 180 | public function constraintsAdded() 181 | { 182 | return $this->constraintsAdded; 183 | } 184 | 185 | /** 186 | * {@inheritdoc} 187 | */ 188 | public function constraintsRemoved() 189 | { 190 | return $this->constraintsRemoved; 191 | } 192 | } 193 | -------------------------------------------------------------------------------- /src/Result/StatementStatistics.php: -------------------------------------------------------------------------------- 1 | 7 | * 8 | * For the full copyright and license information, please view the LICENSE 9 | * file that was distributed with this source code. 10 | */ 11 | 12 | namespace GraphAware\Common\Result; 13 | 14 | class StatementStatistics implements StatementStatisticsInterface 15 | { 16 | /** 17 | * @var int 18 | */ 19 | protected $nodesCreated = 0; 20 | 21 | /** 22 | * @var int 23 | */ 24 | protected $nodesDeleted = 0; 25 | 26 | /** 27 | * @var int 28 | */ 29 | protected $relationshipsCreated = 0; 30 | 31 | /** 32 | * @var int 33 | */ 34 | protected $relationshipsDeleted = 0; 35 | 36 | /** 37 | * @var int 38 | */ 39 | protected $propertiesSet = 0; 40 | 41 | /** 42 | * @var int 43 | */ 44 | protected $labelsAdded = 0; 45 | 46 | /** 47 | * @var int 48 | */ 49 | protected $labelsRemoved = 0; 50 | 51 | /** 52 | * @var int 53 | */ 54 | protected $indexesAdded = 0; 55 | 56 | /** 57 | * @var int 58 | */ 59 | protected $indexesRemoved = 0; 60 | 61 | /** 62 | * @var int 63 | */ 64 | protected $constraintsAdded = 0; 65 | 66 | /** 67 | * @var int 68 | */ 69 | protected $constraintsRemoved = 0; 70 | 71 | /** 72 | * @var bool 73 | */ 74 | protected $containsUpdates = false; 75 | 76 | /** 77 | * @param array $statistics 78 | */ 79 | public function __construct(array $statistics = array()) 80 | { 81 | $keys = [ 82 | 'contains_updates', 'nodes_created', 'nodes_deleted', 'properties_set', 'labels_added', 'labels_removed', 83 | 'indexes_added', 'indexes_removed', 'constraints_added', 'constraints_removed', 'relationships_deleted', 84 | 'relationships_created', 85 | ]; 86 | 87 | foreach ($statistics as $key => $value) { 88 | if (!in_array($key, $keys)) { 89 | throw new \InvalidArgumentException(sprintf('Key %s is invalid in statement statistics', $key)); 90 | } 91 | $k = $this->toCamelCase($key); 92 | $this->$k = $value; 93 | } 94 | 95 | foreach ($statistics as $stat => $value) { 96 | if ($stat !== 'contains_updates' && $value > 0) { 97 | $this->containsUpdates = true; 98 | } 99 | } 100 | } 101 | 102 | /** 103 | * {@inheritdoc} 104 | */ 105 | public function containsUpdates() 106 | { 107 | return (bool) $this->containsUpdates; 108 | } 109 | 110 | /** 111 | * {@inheritdoc} 112 | */ 113 | public function nodesCreated() 114 | { 115 | return $this->nodesCreated; 116 | } 117 | 118 | /** 119 | * {@inheritdoc} 120 | */ 121 | public function nodesDeleted() 122 | { 123 | return $this->nodesDeleted; 124 | } 125 | 126 | /** 127 | * {@inheritdoc} 128 | */ 129 | public function relationshipsCreated() 130 | { 131 | return $this->relationshipsCreated; 132 | } 133 | 134 | /** 135 | * {@inheritdoc} 136 | */ 137 | public function relationshipsDeleted() 138 | { 139 | return $this->relationshipsDeleted; 140 | } 141 | 142 | /** 143 | * {@inheritdoc} 144 | */ 145 | public function propertiesSet() 146 | { 147 | return $this->propertiesSet; 148 | } 149 | 150 | /** 151 | * {@inheritdoc} 152 | */ 153 | public function labelsAdded() 154 | { 155 | return $this->labelsAdded; 156 | } 157 | 158 | /** 159 | * {@inheritdoc} 160 | */ 161 | public function labelsRemoved() 162 | { 163 | return $this->labelsRemoved; 164 | } 165 | 166 | /** 167 | * {@inheritdoc} 168 | */ 169 | public function indexesAdded() 170 | { 171 | return $this->indexesAdded; 172 | } 173 | 174 | /** 175 | * {@inheritdoc} 176 | */ 177 | public function indexesRemoved() 178 | { 179 | return $this->labelsRemoved; 180 | } 181 | 182 | /** 183 | * {@inheritdoc} 184 | */ 185 | public function constraintsAdded() 186 | { 187 | return $this->constraintsAdded; 188 | } 189 | 190 | /** 191 | * {@inheritdoc} 192 | */ 193 | public function constraintsRemoved() 194 | { 195 | return $this->constraintsRemoved; 196 | } 197 | 198 | /** 199 | * @param string $key 200 | * 201 | * @return string 202 | */ 203 | private function toCamelCase($key) 204 | { 205 | list($start, $end) = explode('_', $key); 206 | $str = strtolower($start).ucfirst($end); 207 | 208 | return $str; 209 | } 210 | } 211 | --------------------------------------------------------------------------------