Skip to content

Commit 04bb158

Browse files
Merge branch 'main' into improve-name-escape
2 parents 12c52a4 + 90abe78 commit 04bb158

18 files changed

+489
-32
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,3 +4,4 @@ phpunit.xml
44
vendor/
55
*.idea
66
.phpunit.result.cache
7+
.php-cs-fixer.cache

README.md

Lines changed: 6 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -27,30 +27,13 @@ composer require "wikibase-solutions/php-cypher-dsl"
2727
To construct a query to find all of Tom Hanks' co-actors, you can use the following code:
2828

2929
```php
30-
$tom = Query::variable("tom");
31-
$tomNode = Query::node("Person")->withProperties([
32-
"name" => Query::literal("Tom Hanks")
33-
])->named($tom);
34-
35-
$movie = Query::variable("m");
36-
$movieNode = Query::node()->named($movie);
37-
38-
$coActors = Query::variable("coActors");
39-
$coActorsNode = Query::node()->named($coActors);
30+
$tom = Query::node("Person")->withProperties(["name" => Query::literal("Tom Hanks")]);
31+
$coActors = Query::node();
4032

4133
$statement = Query::new()
42-
->match($tomNode->relationshipTo($movieNode)->withType("ACTED_IN")->relationshipFrom($coActorsNode)->withType("ACTED_IN"))
43-
->returning($coActors->property("name"))
44-
->build();
34+
->match($tom->relationshipTo(Query::node(), "ACTED_IN")->relationshipFrom($coActors, "ACTED_IN"))
35+
->returning($coActors->property("name"))
36+
->build();
4537

46-
$this->assertSame("MATCH (tom:Person {name: 'Tom Hanks'})-[:`ACTED_IN`]->(m)<-[:`ACTED_IN`]-(coActors) RETURN coActors.name", $statement);
38+
$this->assertStringMatchesFormat("MATCH (:Person {name: 'Tom Hanks'})-[:`ACTED_IN`]->()<-[:`ACTED_IN`]-(%s) RETURN %s.name", $statement);
4739
```
48-
49-
## Roadmap
50-
51-
Below are some things that still need to be implemented.
52-
53-
- Add missing clauses
54-
- Add missing function definitions
55-
- Add missing expressions
56-
- Improve documentation

src/Alias.php

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,24 @@
11
<?php
22

3+
/*
4+
* Cypher DSL
5+
* Copyright (C) 2021 Wikibase Solutions
6+
*
7+
* This program is free software; you can redistribute it and/or
8+
* modify it under the terms of the GNU General Public License
9+
* as published by the Free Software Foundation; either version 2
10+
* of the License, or (at your option) any later version.
11+
*
12+
* This program is distributed in the hope that it will be useful,
13+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
14+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15+
* GNU General Public License for more details.
16+
*
17+
* You should have received a copy of the GNU General Public License
18+
* along with this program; if not, write to the Free Software
19+
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
20+
*/
21+
322
namespace WikibaseSolutions\CypherDSL;
423

524
use function sprintf;

src/IsNotNull.php

Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
<?php
2+
3+
/*
4+
* Cypher DSL
5+
* Copyright (C) 2021 Wikibase Solutions
6+
*
7+
* This program is free software; you can redistribute it and/or
8+
* modify it under the terms of the GNU General Public License
9+
* as published by the Free Software Foundation; either version 2
10+
* of the License, or (at your option) any later version.
11+
*
12+
* This program is distributed in the hope that it will be useful,
13+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
14+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15+
* GNU General Public License for more details.
16+
*
17+
* You should have received a copy of the GNU General Public License
18+
* along with this program; if not, write to the Free Software
19+
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
20+
*/
21+
22+
namespace WikibaseSolutions\CypherDSL;
23+
24+
use function sprintf;
25+
use WikibaseSolutions\CypherDSL\Traits\BooleanTypeTrait;
26+
use WikibaseSolutions\CypherDSL\Types\AnyType;
27+
use WikibaseSolutions\CypherDSL\Types\PropertyTypes\BooleanType;
28+
29+
/**
30+
* Represents the IS NOT NULL comparison operator.
31+
*
32+
* @see https://neo4j.com/docs/cypher-manual/current/syntax/operators/#query-operators-comparison
33+
*/
34+
class IsNotNull implements BooleanType
35+
{
36+
use BooleanTypeTrait;
37+
38+
/**
39+
* @var AnyType The type to test against null
40+
*/
41+
private AnyType $expression;
42+
private bool $insertParentheses;
43+
44+
/**
45+
* IS NOT NULL constructor.
46+
*
47+
* @param AnyType $expression The type to test against null.
48+
*/
49+
public function __construct(AnyType $expression, bool $insertParentheses = true)
50+
{
51+
$this->expression = $expression;
52+
$this->insertParentheses = $insertParentheses;
53+
}
54+
55+
/**
56+
* Returns the expression to test against null.
57+
*
58+
* @return AnyType
59+
*/
60+
public function getExpression(): AnyType
61+
{
62+
return $this->expression;
63+
}
64+
65+
/**
66+
* @inheritDoc
67+
*/
68+
public function toQuery(): string
69+
{
70+
return sprintf($this->insertParentheses ? "(%s IS NOT NULL)" : "%s IS NOT NULL", $this->expression->toQuery());
71+
}
72+
73+
/**
74+
* Returns whether or not the operator inserts parenthesis.
75+
*
76+
* @return bool
77+
*/
78+
public function insertsParentheses(): bool
79+
{
80+
return $this->insertParentheses;
81+
}
82+
}

src/IsNull.php

Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
<?php
2+
3+
/*
4+
* Cypher DSL
5+
* Copyright (C) 2021 Wikibase Solutions
6+
*
7+
* This program is free software; you can redistribute it and/or
8+
* modify it under the terms of the GNU General Public License
9+
* as published by the Free Software Foundation; either version 2
10+
* of the License, or (at your option) any later version.
11+
*
12+
* This program is distributed in the hope that it will be useful,
13+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
14+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15+
* GNU General Public License for more details.
16+
*
17+
* You should have received a copy of the GNU General Public License
18+
* along with this program; if not, write to the Free Software
19+
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
20+
*/
21+
22+
namespace WikibaseSolutions\CypherDSL;
23+
24+
use function sprintf;
25+
use WikibaseSolutions\CypherDSL\Traits\BooleanTypeTrait;
26+
use WikibaseSolutions\CypherDSL\Types\AnyType;
27+
use WikibaseSolutions\CypherDSL\Types\PropertyTypes\BooleanType;
28+
29+
/**
30+
* Represents the IS NULL comparison operator.
31+
*
32+
* @see https://neo4j.com/docs/cypher-manual/current/syntax/operators/#query-operators-comparison
33+
*/
34+
class IsNull implements BooleanType
35+
{
36+
use BooleanTypeTrait;
37+
38+
/**
39+
* @var AnyType The type to test against null
40+
*/
41+
private AnyType $expression;
42+
private bool $insertParentheses;
43+
44+
/**
45+
* IS NULL constructor.
46+
*
47+
* @param AnyType $expression The type to test against null.
48+
*/
49+
public function __construct(AnyType $expression, bool $insertParentheses = true)
50+
{
51+
$this->expression = $expression;
52+
$this->insertParentheses = $insertParentheses;
53+
}
54+
55+
/**
56+
* Returns the expression to test against null.
57+
*
58+
* @return AnyType
59+
*/
60+
public function getExpression(): AnyType
61+
{
62+
return $this->expression;
63+
}
64+
65+
/**
66+
* @inheritDoc
67+
*/
68+
public function toQuery(): string
69+
{
70+
return sprintf($this->insertParentheses ? "(%s IS NULL)" : "%s IS NULL", $this->expression->toQuery());
71+
}
72+
73+
/**
74+
* Returns whether or not the operator inserts parenthesis.
75+
*
76+
* @return bool
77+
*/
78+
public function insertsParentheses(): bool
79+
{
80+
return $this->insertParentheses;
81+
}
82+
}

src/Patterns/Path.php

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,24 @@
11
<?php
22

3+
/*
4+
* Cypher DSL
5+
* Copyright (C) 2021 Wikibase Solutions
6+
*
7+
* This program is free software; you can redistribute it and/or
8+
* modify it under the terms of the GNU General Public License
9+
* as published by the Free Software Foundation; either version 2
10+
* of the License, or (at your option) any later version.
11+
*
12+
* This program is distributed in the hope that it will be useful,
13+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
14+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15+
* GNU General Public License for more details.
16+
*
17+
* You should have received a copy of the GNU General Public License
18+
* along with this program; if not, write to the Free Software
19+
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
20+
*/
21+
322
namespace WikibaseSolutions\CypherDSL\Patterns;
423

524
use function array_key_exists;

src/Query.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -313,7 +313,7 @@ public function create($patterns): self
313313
/**
314314
* Creates the DELETE clause.
315315
*
316-
* @param Variable|Variable[] $nodes The nodes to delete
316+
* @param Variable|Variable[] $variables The nodes to delete
317317
*
318318
* @return $this
319319
* @see https://neo4j.com/docs/cypher-manual/current/clauses/delete/
@@ -608,7 +608,7 @@ public function with($expressions): self
608608
* @param string $subject The subject/body of the clause
609609
* @return Query
610610
*/
611-
public function raw(string $clause, string $subject)
611+
public function raw(string $clause, string $subject): self
612612
{
613613
$this->clauses[] = new RawClause($clause, $subject);
614614

src/Traits/AliasableTrait.php

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,24 @@
11
<?php
22

3+
/*
4+
* Cypher DSL
5+
* Copyright (C) 2021 Wikibase Solutions
6+
*
7+
* This program is free software; you can redistribute it and/or
8+
* modify it under the terms of the GNU General Public License
9+
* as published by the Free Software Foundation; either version 2
10+
* of the License, or (at your option) any later version.
11+
*
12+
* This program is distributed in the hope that it will be useful,
13+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
14+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15+
* GNU General Public License for more details.
16+
*
17+
* You should have received a copy of the GNU General Public License
18+
* along with this program; if not, write to the Free Software
19+
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
20+
*/
21+
322
namespace WikibaseSolutions\CypherDSL\Traits;
423

524
use function is_string;

src/Traits/HasNameTrait.php

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,24 @@
11
<?php
22

3+
/*
4+
* Cypher DSL
5+
* Copyright (C) 2021 Wikibase Solutions
6+
*
7+
* This program is free software; you can redistribute it and/or
8+
* modify it under the terms of the GNU General Public License
9+
* as published by the Free Software Foundation; either version 2
10+
* of the License, or (at your option) any later version.
11+
*
12+
* This program is distributed in the hope that it will be useful,
13+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
14+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15+
* GNU General Public License for more details.
16+
*
17+
* You should have received a copy of the GNU General Public License
18+
* along with this program; if not, write to the Free Software
19+
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
20+
*/
21+
322
namespace WikibaseSolutions\CypherDSL\Traits;
423

524
use function mt_rand;

src/Traits/HasPropertiesTrait.php

Lines changed: 23 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,29 @@
11
<?php
22

3+
/*
4+
* Cypher DSL
5+
* Copyright (C) 2021 Wikibase Solutions
6+
*
7+
* This program is free software; you can redistribute it and/or
8+
* modify it under the terms of the GNU General Public License
9+
* as published by the Free Software Foundation; either version 2
10+
* of the License, or (at your option) any later version.
11+
*
12+
* This program is distributed in the hope that it will be useful,
13+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
14+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15+
* GNU General Public License for more details.
16+
*
17+
* You should have received a copy of the GNU General Public License
18+
* along with this program; if not, write to the Free Software
19+
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
20+
*/
21+
322
namespace WikibaseSolutions\CypherDSL\Traits;
423

524
use function is_array;
625
use WikibaseSolutions\CypherDSL\PropertyMap;
726
use WikibaseSolutions\CypherDSL\Types\AnyType;
8-
use WikibaseSolutions\CypherDSL\Types\StructuralTypes\HasPropertiesType;
927

1028
trait HasPropertiesTrait
1129
{
@@ -19,9 +37,9 @@ trait HasPropertiesTrait
1937
* @param string $key The name of the property
2038
* @param AnyType $value The value of the property
2139
*
22-
* @return HasPropertiesType
40+
* @return static
2341
*/
24-
public function withProperty(string $key, AnyType $value): HasPropertiesType
42+
public function withProperty(string $key, AnyType $value): self
2543
{
2644
$this->initialiseProperties();
2745

@@ -35,9 +53,9 @@ public function withProperty(string $key, AnyType $value): HasPropertiesType
3553
*
3654
* @param PropertyMap|array $properties
3755
*
38-
* @return HasPropertiesType
56+
* @return static
3957
*/
40-
public function withProperties($properties): HasPropertiesType
58+
public function withProperties($properties): self
4159
{
4260
self::assertClass('properties', [PropertyMap::class, 'array'], $properties);
4361

0 commit comments

Comments
 (0)