Skip to content

Commit 16ba2d2

Browse files
Rewrite CALL clause
1 parent 7852a36 commit 16ba2d2

File tree

2 files changed

+28
-54
lines changed

2 files changed

+28
-54
lines changed

src/Clauses/CallClause.php

Lines changed: 26 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323

2424
use WikibaseSolutions\CypherDSL\Expressions\Variable;
2525
use WikibaseSolutions\CypherDSL\Query;
26+
use WikibaseSolutions\CypherDSL\Traits\CastTrait;
2627
use WikibaseSolutions\CypherDSL\Traits\ErrorTrait;
2728

2829
/**
@@ -34,8 +35,9 @@
3435
* @see https://neo4j.com/docs/cypher-manual/current/clauses/call-subquery/
3536
* @see Query::call() for a more convenient method to construct this class
3637
*/
37-
class CallClause extends Clause
38+
final class CallClause extends Clause
3839
{
40+
use CastTrait;
3941
use ErrorTrait;
4042

4143
/**
@@ -61,47 +63,30 @@ public function withSubQuery(Query $subQuery): self
6163
return $this;
6264
}
6365

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(...$variables): self
73-
{
74-
$res = [];
66+
/**
67+
* Add one or more variables to include in the WITH clause.
68+
*
69+
* @param Variable|string ...$variables
70+
* @return $this
71+
*
72+
* @see https://neo4j.com/docs/cypher-manual/current/clauses/call-subquery/#subquery-correlated-importing
73+
*/
74+
public function addVariable(...$variables): self
75+
{
76+
$res = [];
7577

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

81-
$this->withVariables = $res;
82+
$this->withVariables = array_merge($this->withVariables, $res);
8283

8384
return $this;
8485
}
8586

86-
/**
87-
* Add a variable to include in the WITH clause.
88-
*
89-
* @param Variable|string $variable A variable or a string to cast to a variable
90-
* @return $this
91-
*
92-
* @see https://neo4j.com/docs/cypher-manual/current/clauses/call-subquery/#subquery-correlated-importing
93-
*/
94-
public function addVariable($variable): self
95-
{
96-
$this->assertClass('variable', [Variable::class, 'string'], $variable);
97-
$this->withVariables[] = is_string($variable) ? new Variable($variable) : $variable;
98-
99-
return $this;
100-
}
101-
10287
/**
10388
* Returns the query that is being called. This query does not include the WITH clause that is inserted
104-
* if there are any correlated variables.
89+
* if there are any correlated variables.
10590
*
10691
* @return Query|null
10792
*/
@@ -110,15 +95,15 @@ public function getSubQuery(): ?Query
11095
return $this->subQuery;
11196
}
11297

113-
/**
114-
* Returns the variables that will be included in the WITH clause.
115-
*
116-
* @return Variable[]
117-
*/
118-
public function getWithVariables(): array
119-
{
120-
return $this->withVariables;
121-
}
98+
/**
99+
* Returns the variables that will be included in the WITH clause.
100+
*
101+
* @return Variable[]
102+
*/
103+
public function getWithVariables(): array
104+
{
105+
return $this->withVariables;
106+
}
122107

123108
/**
124109
* @inheritDoc
@@ -138,7 +123,6 @@ protected function getSubject(): string
138123
if ($this->withVariables !== []) {
139124
$withClause = new WithClause();
140125
$withClause->setEntries($this->withVariables);
141-
142126
$subQuery = $withClause->toQuery() . ' ' . $subQuery;
143127
}
144128

src/Query.php

Lines changed: 2 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -345,7 +345,7 @@ public static function exists($match, $where = null, bool $insertParentheses = f
345345
* Creates a CALL sub query clause and adds it to the query.
346346
*
347347
* @param Query|callable(Query):void $query A callable decorating a query, or the actual CALL subquery
348-
* @param Variable|Variable[]|string $variables The variables to include in the WITH clause for correlation
348+
* @param Variable|Variable[]|string|string[] $variables The variables to include in the WITH clause for correlation
349349
*
350350
* @return Query
351351
*
@@ -367,19 +367,9 @@ public function call($query = null, $variables = []): self
367367
$variables = [$variables];
368368
}
369369

370-
$variables = array_map(function ($variable): Variable {
371-
self::assertClass();
372-
373-
if (is_string($variable)) {
374-
return self::variable($variable);
375-
}
376-
377-
return $variable->getVariable();
378-
}, $variables);
379-
380370
$callClause = new CallClause();
381371
$callClause->withSubQuery($subQuery);
382-
$callClause->withVariables($variables);
372+
$callClause->addVariable(...$variables);
383373

384374
$this->clauses[] = $callClause;
385375

0 commit comments

Comments
 (0)