Skip to content

Commit dbd7a8a

Browse files
committed
Added automatic identifier generation
1 parent dcf03db commit dbd7a8a

File tree

3 files changed

+42
-0
lines changed

3 files changed

+42
-0
lines changed

src/Patterns/Node.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -148,6 +148,14 @@ public function getName(): ?Variable
148148
return $this->variable;
149149
}
150150

151+
152+
public function hasName(): bool {
153+
if (!isset($this->variable)) {
154+
return false;
155+
}
156+
return ($this->variable !== null);
157+
}
158+
151159
/**
152160
* Returns the string representation of this relationship that can be used directly
153161
* in a query.
@@ -178,4 +186,6 @@ public function toQuery(): string
178186

179187
return "($nodeInner)";
180188
}
189+
190+
181191
}

src/Query.php

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -270,6 +270,20 @@ public function returning($expressions, bool $distinct = false): self
270270
throw new TypeError("\$expressions should only consist of AnyType objects");
271271
}
272272

273+
274+
// check if expression is node, then replace with variable
275+
if ($expression instanceof Node) {
276+
print_r(["test" => $expression instanceof Node, "hasname" => $expression->hasName() === true, "expression" => $expression]);
277+
// check if Node has Name setted
278+
if (!($expression->hasName())) {
279+
$expression = $expression->named(uniqid())->getName();
280+
} else {
281+
$expression = $expression->getName();
282+
}
283+
print_r(["test" => $expression instanceof AnyType, "expression" => $expression]);
284+
}
285+
286+
273287
$alias = is_integer($maybeAlias) ? "" : $maybeAlias;
274288
$returnClause->addColumn($expression, $alias);
275289
}
@@ -584,6 +598,15 @@ public function with($expressions): self
584598
throw new TypeError("\$expressions should only consist of AnyType objects");
585599
}
586600

601+
// check if expression is node, then replace with variable
602+
if ($expression instanceof Node) {
603+
// check if Node has Name setted
604+
if (!$expression->hasName()) {
605+
$expression->named(uniqid());
606+
}
607+
$expression = $expression->getName();
608+
}
609+
587610
$alias = is_integer($maybeAlias) ? "" : $maybeAlias;
588611
$withClause->addEntry($expression, $alias);
589612
}

tests/Unit/QueryTest.php

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -228,6 +228,15 @@ public function testReturning()
228228
$this->assertSame("RETURN (m:Movie) AS n", $statement);
229229
}
230230

231+
public function testReturningWithNode()
232+
{
233+
$node = Query::node("(m)");
234+
235+
$statement = (new Query())->returning($node)->build();
236+
237+
$this->assertMatchesRegularExpression("/(RETURN [0-Z])\w+/", $statement);
238+
}
239+
231240
public function testCreate()
232241
{
233242
$m = $this->getQueryConvertableMock(PathType::class, "(m:Movie)->(b)");

0 commit comments

Comments
 (0)