Skip to content

Commit 2446bd2

Browse files
Minor changes to Clauses and update Query class
1 parent 6b2e727 commit 2446bd2

File tree

7 files changed

+60
-46
lines changed

7 files changed

+60
-46
lines changed

src/Clauses/CallProcedureClause.php

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,6 @@
2828
final class CallProcedureClause extends Clause
2929
{
3030
use CastTrait;
31-
use EscapeTrait;
32-
use ErrorTrait;
3331

3432
/**
3533
* @var Procedure|null The procedure to call

src/Clauses/MergeClause.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -56,10 +56,10 @@ public function setPattern(CompletePattern $pattern): self
5656
*
5757
* @see https://neo4j.com/docs/cypher-manual/current/clauses/merge/#merge-merge-with-on-create
5858
*
59-
* @param SetClause $createClause
59+
* @param SetClause|null $createClause
6060
* @return $this
6161
*/
62-
public function setOnCreate(SetClause $createClause): self
62+
public function setOnCreate(?SetClause $createClause): self
6363
{
6464
$this->createClause = $createClause;
6565

@@ -71,10 +71,10 @@ public function setOnCreate(SetClause $createClause): self
7171
*
7272
* @see https://neo4j.com/docs/cypher-manual/current/clauses/merge/#merge-merge-with-on-match
7373
*
74-
* @param SetClause $matchClause
74+
* @param SetClause|null $matchClause
7575
* @return $this
7676
*/
77-
public function setOnMatch(SetClause $matchClause): self
77+
public function setOnMatch(?SetClause $matchClause): self
7878
{
7979
$this->matchClause = $matchClause;
8080

src/Clauses/OrderByClause.php

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,6 @@
2727
*/
2828
final class OrderByClause extends Clause
2929
{
30-
use EscapeTrait;
31-
3230
/**
3331
* @var Property[] The expressions to include in the clause
3432
*/

src/Clauses/WhereClause.php

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
namespace WikibaseSolutions\CypherDSL\Clauses;
1111

1212
use InvalidArgumentException;
13+
use WikibaseSolutions\CypherDSL\Traits\CastTrait;
1314
use WikibaseSolutions\CypherDSL\Types\PropertyTypes\BooleanType;
1415

1516
/**
@@ -23,6 +24,8 @@
2324
*/
2425
final class WhereClause extends Clause
2526
{
27+
use CastTrait;
28+
2629
public const AND = 'and';
2730
public const OR = 'or';
2831
public const XOR = 'xor';
@@ -35,13 +38,15 @@ final class WhereClause extends Clause
3538
/**
3639
* Add an expression to this WHERE clause.
3740
*
38-
* @param BooleanType $expression The expression to add to the WHERE clause
41+
* @param BooleanType|bool $expression The expression to add to the WHERE clause
3942
* @param string $operator The operator to use to combine the given expression with the existing expression, should
4043
* be one of WhereClause::AND, WhereClause::OR or WhereClause::XOR
4144
* @return $this
4245
*/
43-
public function addExpression(BooleanType $expression, string $operator = self::AND): self
46+
public function addExpression($expression, string $operator = self::AND): self
4447
{
48+
$expression = self::toBooleanType($expression);
49+
4550
if ($operator !== self::AND && $operator !== self::OR && $operator !== self::XOR) {
4651
throw new InvalidArgumentException('$operator must either be "and", "xor" or "or"');
4752
}

src/Query.php

Lines changed: 40 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -46,11 +46,13 @@
4646
use WikibaseSolutions\CypherDSL\Patterns\Node;
4747
use WikibaseSolutions\CypherDSL\Patterns\Pattern;
4848
use WikibaseSolutions\CypherDSL\Patterns\Relationship;
49+
use WikibaseSolutions\CypherDSL\Syntax\Alias;
4950
use WikibaseSolutions\CypherDSL\Syntax\PropertyReplacement;
5051
use WikibaseSolutions\CypherDSL\Traits\ErrorTrait;
5152
use WikibaseSolutions\CypherDSL\Traits\EscapeTrait;
5253
use WikibaseSolutions\CypherDSL\Types\AnyType;
5354
use WikibaseSolutions\CypherDSL\Types\PropertyTypes\BooleanType;
55+
use WikibaseSolutions\CypherDSL\Types\PropertyTypes\IntegerType;
5456
use WikibaseSolutions\CypherDSL\Types\PropertyTypes\NumeralType;
5557
use WikibaseSolutions\CypherDSL\Types\StructuralTypes\StructuralType;
5658

@@ -319,13 +321,14 @@ public static function exists($match, $where = null, bool $insertParentheses = f
319321
/**
320322
* Creates a CALL sub query clause and adds it to the query.
321323
*
322-
* @param Query|callable(Query):void $query A callable decorating a query, or the actual CALL subquery
324+
* @note This feature is not part of the openCypher standard. For more information, see https://github.com/opencypher/openCypher/blob/a507292d35280aca9e37bf938cdec4fdd1e64ba9/docs/standardisation-scope.adoc.
325+
*
326+
* @param Query|callable(Query):void $query A callable decorating a query, or the actual subquery
323327
* @param Variable|Pattern|string|Variable[]|Pattern[]|string[] $variables The variables to include in the WITH clause for correlation
324328
*
325-
* @return Query
329+
* @return $this
326330
*
327331
* @see https://neo4j.com/docs/cypher-manual/current/clauses/call-subquery/
328-
* @see CallClause
329332
*/
330333
public function call($query = null, $variables = []): self
331334
{
@@ -355,8 +358,9 @@ public function call($query = null, $variables = []): self
355358
* Creates the CALL procedure clause.
356359
*
357360
* @param Procedure $procedure The procedure to call
358-
* @param string|Variable|string[]|Variable[] $yields The result fields that should be returned
359-
* @return Query
361+
* @param string|Variable|Alias|string[]|Variable[]|Alias[] $yields The result fields that should be returned
362+
*
363+
* @return $this
360364
*
361365
* @see https://neo4j.com/docs/cypher-manual/current/clauses/call/
362366
* @see https://s3.amazonaws.com/artifacts.opencypher.org/openCypher9.pdf (page 122)
@@ -403,14 +407,13 @@ public function match($patterns): self
403407
/**
404408
* Creates the RETURN clause.
405409
*
406-
* @param AnyType|AnyType[] $expressions The expressions to return; if the array-key is
407-
* non-numerical, it is used as the alias
408-
* @param bool $distinct
410+
* @param AnyType|Alias|Pattern|int|float|string|bool|array|AnyType[]|Alias[]|Pattern[]|int[]|float[]|string[]|bool[]|array[] $expressions The expressions to return
411+
* @param bool $distinct Whether to be a RETURN DISTINCT query
409412
*
410413
* @return $this
411414
*
412-
* @see https://neo4j.com/docs/cypher-manual/current/clauses/return/#return-column-alias
413415
* @see https://neo4j.com/docs/cypher-manual/current/clauses/return/
416+
* @see https://s3.amazonaws.com/artifacts.opencypher.org/openCypher9.pdf (page 74)
414417
*/
415418
public function returning($expressions, bool $distinct = false): self
416419
{
@@ -435,6 +438,7 @@ public function returning($expressions, bool $distinct = false): self
435438
* @return $this
436439
*
437440
* @see https://neo4j.com/docs/cypher-manual/current/clauses/create/
441+
* @see https://s3.amazonaws.com/artifacts.opencypher.org/openCypher9.pdf (page 99)
438442
*/
439443
public function create($patterns): self
440444
{
@@ -482,6 +486,7 @@ public function delete($structures, bool $detach = false): self
482486
*
483487
* @return $this
484488
* @see https://neo4j.com/docs/cypher-manual/current/clauses/delete/
489+
* @see https://s3.amazonaws.com/artifacts.opencypher.org/openCypher9.pdf (page 105)
485490
* @deprecated Use Query::delete(..., true) instead
486491
*/
487492
public function detachDelete($variables): self
@@ -512,10 +517,12 @@ public function limit($limit): self
512517
/**
513518
* Creates the SKIP clause.
514519
*
515-
* @param NumeralType|int|float $amount The amount to skip
520+
* @param IntegerType|int $amount The amount to skip
516521
*
517522
* @return $this
523+
*
518524
* @see https://neo4j.com/docs/cypher-manual/current/clauses/skip/
525+
* @see https://s3.amazonaws.com/artifacts.opencypher.org/openCypher9.pdf (page 96)
519526
*/
520527
public function skip($amount): self
521528
{
@@ -533,6 +540,7 @@ public function skip($amount): self
533540
* @param CompletePattern $pattern The pattern to merge
534541
* @param SetClause|null $createClause The clause to execute when the pattern is created
535542
* @param SetClause|null $matchClause The clause to execute when the pattern is matched
543+
*
536544
* @return $this
537545
*
538546
* @see https://neo4j.com/docs/cypher-manual/current/clauses/merge/
@@ -542,14 +550,8 @@ public function merge(CompletePattern $pattern, SetClause $createClause = null,
542550
{
543551
$mergeClause = new MergeClause();
544552
$mergeClause->setPattern($pattern);
545-
546-
if (isset($createClause)) {
547-
$mergeClause->setOnCreate($createClause);
548-
}
549-
550-
if (isset($matchClause)) {
551-
$mergeClause->setOnMatch($matchClause);
552-
}
553+
$mergeClause->setOnCreate($createClause);
554+
$mergeClause->setOnMatch($matchClause);
553555

554556
$this->clauses[] = $mergeClause;
555557

@@ -560,6 +562,7 @@ public function merge(CompletePattern $pattern, SetClause $createClause = null,
560562
* Creates the OPTIONAL MATCH clause.
561563
*
562564
* @param CompletePattern|CompletePattern[] $patterns A single pattern or a list of patterns
565+
*
563566
* @return $this
564567
*
565568
* @see https://neo4j.com/docs/cypher-manual/current/clauses/optional-match/
@@ -586,8 +589,9 @@ public function optionalMatch($patterns): self
586589
* @param bool $descending Whether to order in descending order
587590
*
588591
* @return $this
589-
* @see https://neo4j.com/docs/cypher-manual/current/clauses/order-by/
590592
*
593+
* @see https://neo4j.com/docs/cypher-manual/current/clauses/order-by/
594+
* @see https://s3.amazonaws.com/artifacts.opencypher.org/openCypher9.pdf (page 93)
591595
*/
592596
public function orderBy($properties, bool $descending = false): self
593597
{
@@ -610,8 +614,9 @@ public function orderBy($properties, bool $descending = false): self
610614
* @param Property|Label|Property[]|Label[] $expressions The expressions to remove (should either be a Node or a Property)
611615
*
612616
* @return $this
613-
* @see https://neo4j.com/docs/cypher-manual/current/clauses/remove/
614617
*
618+
* @see https://neo4j.com/docs/cypher-manual/current/clauses/remove/
619+
* @see https://s3.amazonaws.com/artifacts.opencypher.org/openCypher9.pdf (page 113)
615620
*/
616621
public function remove($expressions): self
617622
{
@@ -633,8 +638,9 @@ public function remove($expressions): self
633638
* @param PropertyReplacement|Label|PropertyReplacement[]|Label[] $expressions A single expression or a list of expressions
634639
*
635640
* @return $this
636-
* @see https://neo4j.com/docs/cypher-manual/current/clauses/set/
637641
*
642+
* @see https://neo4j.com/docs/cypher-manual/current/clauses/set/
643+
* @see https://s3.amazonaws.com/artifacts.opencypher.org/openCypher9.pdf (page 107)
638644
*/
639645
public function set($expressions): self
640646
{
@@ -653,12 +659,14 @@ public function set($expressions): self
653659
/**
654660
* Creates the WHERE clause.
655661
*
656-
* @param BooleanType|BooleanType[] $expressions The expression to match
662+
* @param BooleanType|bool|BooleanType[]|bool[] $expressions The expression to match
657663
* @param string $operator The operator with which to unify the given expressions, should be either WhereClause::OR,
658664
* WhereClause::AND or WhereClause::XOR
659665
*
660666
* @return $this
667+
*
661668
* @see https://neo4j.com/docs/cypher-manual/current/clauses/where/
669+
* @see https://s3.amazonaws.com/artifacts.opencypher.org/openCypher9.pdf (page 83)
662670
*/
663671
public function where($expressions, string $operator = WhereClause::AND): self
664672
{
@@ -680,10 +688,12 @@ public function where($expressions, string $operator = WhereClause::AND): self
680688
/**
681689
* Creates the WITH clause.
682690
*
683-
* @param AnyType[]|AnyType $expressions The entries to add; if the array-key is non-numerical, it is used as the alias
691+
* @param AnyType|Alias|Pattern|int|float|string|bool|array|AnyType[]|Alias[]|Pattern[]|int[]|float[]|string[]|bool[]|array[] $expressions The entries to add; if the array-key is non-numerical, it is used as the alias
692+
*
693+
* @return $this
684694
*
685-
* @return Query
686695
* @see https://neo4j.com/docs/cypher-manual/current/clauses/with/
696+
* @see https://s3.amazonaws.com/artifacts.opencypher.org/openCypher9.pdf (page 78)
687697
*/
688698
public function with($expressions): self
689699
{
@@ -704,7 +714,8 @@ public function with($expressions): self
704714
*
705715
* @param string $clause The name of the clause; for instance "MATCH"
706716
* @param string $subject The subject/body of the clause
707-
* @return Query
717+
*
718+
* @return $this
708719
*/
709720
public function raw(string $clause, string $subject): self
710721
{
@@ -719,9 +730,10 @@ public function raw(string $clause, string $subject): self
719730
* @param Query|callable(Query):void $queryOrCallable The callable decorating a fresh query instance or the query instance to be attached after the union clause.
720731
* @param bool $all Whether the union should include all results or remove the duplicates instead.
721732
*
722-
* @return Query
733+
* @return $this
723734
*
724735
* @see https://neo4j.com/docs/cypher-manual/current/clauses/union/
736+
* @see https://s3.amazonaws.com/artifacts.opencypher.org/openCypher9.pdf (page 128)
725737
*/
726738
public function union($queryOrCallable, bool $all = false): self
727739
{
@@ -748,7 +760,8 @@ public function union($queryOrCallable, bool $all = false): self
748760
* Add a clause to the query.
749761
*
750762
* @param Clause $clause The clause to add to the query
751-
* @return Query
763+
*
764+
* @return $this
752765
*/
753766
public function addClause(Clause $clause): self
754767
{

tests/Unit/Clauses/WhereClauseTest.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ public function testExpression(): void
4646
$where = new WhereClause();
4747
$expression = $this->getQueryConvertibleMock(AnyType::class, "(a)");
4848

49-
$where->setExpression($expression);
49+
$where->addExpression($expression);
5050

5151
$this->assertSame("WHERE (a)", $where->toQuery());
5252
$this->assertEquals($expression, $where->getExpression());
@@ -60,7 +60,7 @@ public function testAcceptsAnyType(): void
6060
$where = new WhereClause();
6161
$expression = $this->getQueryConvertibleMock(AnyType::class, "(a)");
6262

63-
$where->setExpression($expression);
63+
$where->addExpression($expression);
6464

6565
$where->toQuery();
6666
}

tests/Unit/Clauses/WithClauseTest.php

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ public function testEmptyClause(): void
3838
$return = new WithClause();
3939

4040
$this->assertSame("", $return->toQuery());
41-
$this->assertEquals([], $return->getExpressions());
41+
$this->assertEquals([], $return->getEntries());
4242
}
4343

4444
public function testSingleEntry(): void
@@ -48,7 +48,7 @@ public function testSingleEntry(): void
4848
$return->addEntry($entry);
4949

5050
$this->assertSame("WITH a", $return->toQuery());
51-
$this->assertEquals([$entry], $return->getExpressions());
51+
$this->assertEquals([$entry], $return->getEntries());
5252
}
5353

5454
public function testMultipleEntries(): void
@@ -63,7 +63,7 @@ public function testMultipleEntries(): void
6363
$return->addEntry($entryC);
6464

6565
$this->assertSame("WITH a, b, c", $return->toQuery());
66-
$this->assertEquals([$entryA, $entryB, $entryC], $return->getExpressions());
66+
$this->assertEquals([$entryA, $entryB, $entryC], $return->getEntries());
6767
}
6868

6969
public function testSingleAlias(): void
@@ -73,7 +73,7 @@ public function testSingleAlias(): void
7373
$return->addEntry($entry, "b");
7474

7575
$this->assertSame("WITH a AS b", $return->toQuery());
76-
$this->assertEquals(['b' => $entry], $return->getExpressions());
76+
$this->assertEquals(['b' => $entry], $return->getEntries());
7777
}
7878

7979
public function testMultipleAliases(): void
@@ -86,7 +86,7 @@ public function testMultipleAliases(): void
8686
$return->addEntry($entryB, "c");
8787

8888
$this->assertSame("WITH a AS b, b AS c", $return->toQuery());
89-
$this->assertEquals(['b' => $entryA, 'c' => $entryB], $return->getExpressions());
89+
$this->assertEquals(['b' => $entryA, 'c' => $entryB], $return->getEntries());
9090
}
9191

9292
public function testMixedAliases(): void
@@ -101,7 +101,7 @@ public function testMixedAliases(): void
101101
$return->addEntry($entryC, "c");
102102

103103
$this->assertSame("WITH a AS b, c, b AS c", $return->toQuery());
104-
$this->assertEquals(['b' => $entryA, $entryB, 'c' => $entryC], $return->getExpressions());
104+
$this->assertEquals(['b' => $entryA, $entryB, 'c' => $entryC], $return->getEntries());
105105
}
106106

107107
public function testAliasIsEscaped(): void
@@ -111,7 +111,7 @@ public function testAliasIsEscaped(): void
111111
$return->addEntry($entry, ":");
112112

113113
$this->assertSame("WITH a AS `:`", $return->toQuery());
114-
$this->assertEquals([':' => $entry], $return->getExpressions());
114+
$this->assertEquals([':' => $entry], $return->getEntries());
115115
}
116116

117117
/**

0 commit comments

Comments
 (0)