Skip to content

Commit 2e1da1e

Browse files
author
Wout Gevaert
committed
Several improvements for the 5.0.0 branch
1 parent d37c9fa commit 2e1da1e

File tree

10 files changed

+99
-106
lines changed

10 files changed

+99
-106
lines changed

src/Clauses/CallClause.php

Lines changed: 33 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -36,17 +36,17 @@
3636
*/
3737
class CallClause extends Clause
3838
{
39-
use ErrorTrait;
39+
use ErrorTrait;
4040

4141
/**
4242
* @var Query|null The subquery to call, or NULL if no subquery has been set yet
4343
*/
4444
private ?Query $subQuery = null;
4545

46-
/**
47-
* @var Variable[] The variables to include in the WITH clause (for correlated queries)
48-
*/
49-
private array $withVariables = [];
46+
/**
47+
* @var Variable[] The variables to include in the WITH clause (for correlated queries)
48+
*/
49+
private array $withVariables = [];
5050

5151
/**
5252
* Sets the query to call. This overwrites any previously set subquery.
@@ -61,27 +61,27 @@ public function withSubQuery(Query $subQuery): self
6161
return $this;
6262
}
6363

64-
/**
65-
* Sets the variables to include in the WITH clause. This overwrites any previously set variables.
66-
*
67-
* @param Variable[]|string[] $variables A list of variable objects, or strings to cast to variables
68-
* @return $this
69-
*
70-
* @see https://neo4j.com/docs/cypher-manual/current/clauses/call-subquery/#subquery-correlated-importing
71-
*/
72-
public function withVariables(array $variables): self
73-
{
74-
$res = [];
64+
/**
65+
* Sets the variables to include in the WITH clause. This overwrites any previously set variables.
66+
*
67+
* @param Variable[]|string[] $variables A list of variable objects, or strings to cast to variables
68+
* @return $this
69+
*
70+
* @see https://neo4j.com/docs/cypher-manual/current/clauses/call-subquery/#subquery-correlated-importing
71+
*/
72+
public function withVariables(array $variables): self
73+
{
74+
$res = [];
7575

76-
foreach ($variables as $variable) {
77-
$this->assertClass('variables', [Variable::class, 'string'], $variable);
78-
$res[] = is_string($variable) ? new Variable($variable) : $variable;
79-
}
76+
foreach ($variables as $variable) {
77+
$this->assertClass('variables', [Variable::class, 'string'], $variable);
78+
$res[] = is_string($variable) ? new Variable($variable) : $variable;
79+
}
8080

81-
$this->withVariables = $res;
81+
$this->withVariables = $res;
8282

83-
return $this;
84-
}
83+
return $this;
84+
}
8585

8686
/**
8787
* Add a variable to include in the WITH clause.
@@ -129,23 +129,20 @@ protected function getSubject(): string
129129
return '';
130130
}
131131

132-
$subQuery = '';
133-
134-
if ($this->withVariables !== []) {
135-
$withClause = new WithClause();
136-
$withClause->setEntries($this->withVariables);
132+
$subQuery = $this->subQuery->build();
137133

138-
$subQuery .= $withClause->toQuery();
139-
}
134+
if ($subQuery === '') {
135+
return '';
136+
}
140137

141-
if ($subQuery !== '') {
142-
// Add some padding for the next structure
143-
$subQuery .= ' ';
144-
}
138+
if ($this->withVariables !== []) {
139+
$withClause = new WithClause();
140+
$withClause->setEntries($this->withVariables);
145141

146-
$subQuery .= $this->subQuery->build();
142+
$subQuery = $withClause->toQuery() . ' ' . $subQuery;
143+
}
147144

148-
return sprintf('{ %s }', $subQuery);
145+
return sprintf('{ %s }', $subQuery);
149146
}
150147

151148
/**

src/Clauses/DeleteClause.php

Lines changed: 23 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -21,9 +21,8 @@
2121

2222
namespace WikibaseSolutions\CypherDSL\Clauses;
2323

24-
use WikibaseSolutions\CypherDSL\Expressions\Variable;
2524
use WikibaseSolutions\CypherDSL\Traits\ErrorTrait;
26-
use WikibaseSolutions\CypherDSL\Types\StructuralTypes\NodeType;
25+
use WikibaseSolutions\CypherDSL\Types\StructuralTypes\StructuralType;
2726

2827
/**
2928
* This class represents a DELETE clause.
@@ -41,14 +40,14 @@ class DeleteClause extends Clause
4140
*/
4241
private bool $detach = false;
4342

44-
/**
45-
* @var NodeType[] The nodes to delete
46-
*/
47-
private array $nodes = [];
43+
/**
44+
* @var StructuralType[] The structures to delete
45+
*/
46+
private array $structures = [];
4847

49-
/**
50-
* Sets the clause to DETACH DELETE. Without DETACH DELETE, all relationships need to be explicitly
51-
* deleted.
48+
/**
49+
* Sets the clause to DETACH DELETE. Without DETACH DELETE,
50+
* all relationships connected to the nodes/paths need to be explicitly deleted.
5251
*
5352
* @param bool $detach Whether to use DETACH DELETE
5453
* @return $this
@@ -61,31 +60,31 @@ public function setDetach(bool $detach = true): self
6160
}
6261

6362
/**
64-
* Sets the variables to be deleted. This overwrites previous variables if they exist.
63+
* Sets the structures to be deleted. This overwrites previous structures if they exist.
6564
*
66-
* @param NodeType[] $nodes The nodes to delete
65+
* @param StructuralType[] $structures The structures to delete
6766
* @return $this
6867
*/
69-
public function setNodes(array $nodes): self
68+
public function setStructures(array $structures): self
7069
{
71-
foreach ($nodes as $node) {
72-
$this->assertClass('nodes', NodeType::class, $node);
70+
foreach ($structures as $structure) {
71+
$this->assertClass('structure', StructuralType::class, $structure);
7372
}
7473

75-
$this->nodes = $nodes;
74+
$this->structures = $structures;
7675

7776
return $this;
7877
}
7978

8079
/**
81-
* Add a node to be deleted.
80+
* Add a structure to be deleted.
8281
*
83-
* @param NodeType $node The node that should be deleted
82+
* @param StructuralType $structure The structure that should be deleted
8483
* @return $this
8584
*/
86-
public function addNode(NodeType $node): self
85+
public function addStructure(StructuralType $structure): self
8786
{
88-
$this->nodes[] = $node;
87+
$this->structures[] = $structure;
8988

9089
return $this;
9190
}
@@ -101,13 +100,13 @@ public function detachesDeletion(): bool
101100
}
102101

103102
/**
104-
* Returns the nodes to delete.
103+
* Returns the structures to delete.
105104
*
106-
* @return NodeType[]
105+
* @return StructuralType[]
107106
*/
108-
public function getNodes(): array
107+
public function getStructures(): array
109108
{
110-
return $this->nodes;
109+
return $this->structures;
111110
}
112111

113112
/**
@@ -129,7 +128,7 @@ protected function getSubject(): string
129128
{
130129
return implode(
131130
", ",
132-
array_map(fn (Variable $variable) => $variable->toQuery(), $this->nodes)
131+
array_map(fn (StructuralType $structure) => $structure->toQuery(), $this->structures)
133132
);
134133
}
135134
}

src/Clauses/MatchClause.php

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,6 @@
2323

2424
use WikibaseSolutions\CypherDSL\Patterns\MatchablePattern;
2525
use WikibaseSolutions\CypherDSL\Traits\ErrorTrait;
26-
use WikibaseSolutions\CypherDSL\Types\StructuralTypes\NodeType;
27-
use WikibaseSolutions\CypherDSL\Types\StructuralTypes\PathType;
2826

2927
/**
3028
* This class represents a MATCH clause.
@@ -36,12 +34,12 @@ class MatchClause extends Clause
3634
use ErrorTrait;
3735

3836
/**
39-
* @var PathType[]|NodeType[] List of patterns
37+
* @var MatchablePattern[] List of patterns
4038
*/
4139
private array $patterns = [];
4240

4341
/**
44-
* Sets the pattern of the MATCH clause. This overwrites any previously added patterns.
42+
* Sets the patterns of the MATCH clause. This overwrites any previously added patterns.
4543
*
4644
* @param MatchablePattern[] $patterns
4745
* @return $this
@@ -70,7 +68,7 @@ public function addPattern(MatchablePattern ...$pattern): self
7068
/**
7169
* Returns the patterns to match.
7270
*
73-
* @return PathType[]|NodeType[]
71+
* @return MatchablePattern[]
7472
*/
7573
public function getPatterns(): array
7674
{
@@ -92,7 +90,7 @@ protected function getSubject(): string
9290
{
9391
return implode(
9492
", ",
95-
array_map(fn ($pattern): string => $pattern->toQuery(), $this->patterns)
93+
array_map(fn (MatchablePattern $pattern): string => $pattern->toQuery(), $this->patterns)
9694
);
9795
}
9896
}

src/Clauses/OptionalMatchClause.php

Lines changed: 9 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,7 @@
2222
namespace WikibaseSolutions\CypherDSL\Clauses;
2323

2424
use WikibaseSolutions\CypherDSL\Traits\ErrorTrait;
25-
use WikibaseSolutions\CypherDSL\Types\StructuralTypes\NodeType;
26-
use WikibaseSolutions\CypherDSL\Types\StructuralTypes\PathType;
25+
use WikibaseSolutions\CypherDSL\Patterns\MatchablePattern;
2726

2827
/**
2928
* This class represents an OPTIONAL MATCH clause.
@@ -35,14 +34,14 @@ class OptionalMatchClause extends Clause
3534
use ErrorTrait;
3635

3736
/**
38-
* @var (PathType|NodeType)[] List of patterns
37+
* @var MatchablePattern[] List of patterns
3938
*/
4039
private array $patterns = [];
4140

4241
/**
4342
* Returns the patterns to optionally match.
4443
*
45-
* @return (PathType|NodeType)[]
44+
* @return MatchablePattern[]
4645
*/
4746
public function getPatterns(): array
4847
{
@@ -52,13 +51,13 @@ public function getPatterns(): array
5251
/**
5352
* Sets the pattern of the OPTIONAL MATCH clause. This overwrites any previously added patterns.
5453
*
55-
* @param (PathType|NodeType)[] $patterns
54+
* @param MatchablePattern[] $patterns
5655
* @return $this
5756
*/
5857
public function setPatterns(array $patterns): self
5958
{
6059
foreach ($patterns as $pattern) {
61-
$this->assertClass('pattern', [PathType::class, NodeType::class], $pattern);
60+
$this->assertClass('pattern', MatchablePattern::class, $pattern);
6261
}
6362

6463
$this->patterns = $patterns;
@@ -69,12 +68,11 @@ public function setPatterns(array $patterns): self
6968
/**
7069
* Add a pattern to the OPTIONAL MATCH clause.
7170
*
72-
* @param PathType|NodeType $pattern
73-
* @return OptionalMatchClause
71+
* @param MatchablePattern $pattern
72+
* @return $this
7473
*/
75-
public function addPattern($pattern): self
74+
public function addPattern(MatchablePattern $pattern): self
7675
{
77-
$this->assertClass('pattern', [NodeType::class, PathType::class], $pattern);
7876
$this->patterns[] = $pattern;
7977

8078
return $this;
@@ -95,7 +93,7 @@ protected function getSubject(): string
9593
{
9694
return implode(
9795
", ",
98-
array_map(fn ($pattern): string => $pattern->toQuery(), $this->patterns)
96+
array_map(fn (MatchablePattern $pattern): string => $pattern->toQuery(), $this->patterns)
9997
);
10098
}
10199
}

src/Clauses/SetClause.php

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -41,12 +41,12 @@ class SetClause extends Clause
4141
private array $expressions = [];
4242

4343
/**
44-
* Sets the assignments of this SET clause. This will overwrite any previously added assignments.
44+
* Sets the subjects of this SET clause. This will overwrite any previously added expressions.
4545
*
46-
* @param (Assignment|Label)[] $expressions The assignments to execute
47-
* @return SetClause
46+
* @param (PropertyReplacement|Label)[] $expressions The expressions to set
47+
* @return $this
4848
*/
49-
public function setAssignments(array $expressions): self
49+
public function setExpressions(array $expressions): self
5050
{
5151
foreach ($expressions as $expression) {
5252
$this->assertClass('expressions', [PropertyReplacement::class, Label::class], $expression);
@@ -58,12 +58,12 @@ public function setAssignments(array $expressions): self
5858
}
5959

6060
/**
61-
* Add an assignment.
61+
* Add an expression.
6262
*
63-
* @param PropertyReplacement|Label $expression The assignment to execute
64-
* @return SetClause
63+
* @param PropertyReplacement|Label $expression The expression to add to this set clause
64+
* @return $this
6565
*/
66-
public function addAssignment($expression): self
66+
public function add($expression): self
6767
{
6868
$this->assertClass('expression', [PropertyReplacement::class, Label::class], $expression);
6969
$this->expressions[] = $expression;
@@ -74,7 +74,7 @@ public function addAssignment($expression): self
7474
/**
7575
* Returns the expressions to SET.
7676
*
77-
* @return PropertyReplacement[]|Label[]
77+
* @return (PropertyReplacement|Label)[]
7878
*/
7979
public function getExpressions(): array
8080
{

0 commit comments

Comments
 (0)