Skip to content

Commit 464679f

Browse files
Move UUID generation to Node class
1 parent 4e99cb8 commit 464679f

File tree

3 files changed

+33
-48
lines changed

3 files changed

+33
-48
lines changed

src/Patterns/Node.php

Lines changed: 23 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,6 @@
2525
use WikibaseSolutions\CypherDSL\Property;
2626
use WikibaseSolutions\CypherDSL\PropertyMap;
2727
use WikibaseSolutions\CypherDSL\Traits\EscapeTrait;
28-
use WikibaseSolutions\CypherDSL\Traits\IdentifierGenerationTrait;
2928
use WikibaseSolutions\CypherDSL\Traits\NodeTypeTrait;
3029
use WikibaseSolutions\CypherDSL\Types\AnyType;
3130
use WikibaseSolutions\CypherDSL\Types\CompositeTypes\MapType;
@@ -41,7 +40,6 @@ class Node implements NodeType
4140
{
4241
use EscapeTrait;
4342
use NodeTypeTrait;
44-
use IdentifierGenerationTrait;
4543

4644
/**
4745
* @var string[]
@@ -142,12 +140,17 @@ public function labeled(string $label): self
142140
}
143141

144142
/**
145-
* Returns the name of this node.
143+
* Returns the name of this node. This function automatically generates a name if the node does not have a
144+
* name yet.
146145
*
147146
* @return Variable|null The name of this node, or NULL if this node does not have a name
148147
*/
149148
public function getName(): ?Variable
150149
{
150+
if (!isset($this->variable)) {
151+
$this->named($this->generateUUID());
152+
}
153+
151154
return $this->variable;
152155
}
153156

@@ -177,12 +180,14 @@ public function hasName(): bool
177180
* Returns the property of the given name for this expression. For instance, if this expression is the
178181
* variable "foo", a function call like $expression->property("bar") would yield "foo.bar".
179182
*
183+
* TODO: Maybe move this function to the NodeType trait and add it to the interface?
184+
*
180185
* @param string $property
181186
* @return Property
182187
*/
183188
public function property(string $property): Property
184189
{
185-
return new Property($this->variableIfNode($this), $property);
190+
return new Property($this->getName(), $property);
186191
}
187192

188193
/**
@@ -215,4 +220,18 @@ public function toQuery(): string
215220

216221
return "($nodeInner)";
217222
}
223+
224+
/**
225+
* Generates a unique random identifier.
226+
*
227+
* @note It is not entirely guaranteed that this function gives a truly unique identifier. However, because the
228+
* number of possible IDs is so huge, it should not be a problem.
229+
*
230+
* @param int $length
231+
* @return string
232+
*/
233+
private function generateUUID(int $length = 32): string
234+
{
235+
return substr(bin2hex(openssl_random_pseudo_bytes(ceil($length / 2))), 0, $length);
236+
}
218237
}

src/Query.php

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,6 @@
6262
class Query implements QueryConvertable
6363
{
6464
use EscapeTrait;
65-
use IdentifierGenerationTrait;
6665

6766
// A reference to the Literal class
6867
const literal = Literal::class;
@@ -272,8 +271,12 @@ public function returning($expressions, bool $distinct = false): self
272271
throw new TypeError("\$expressions should only consist of AnyType objects");
273272
}
274273

274+
if ($expression instanceof Node) {
275+
$expression = $expression->getName();
276+
}
277+
275278
$alias = is_integer($maybeAlias) ? "" : $maybeAlias;
276-
$returnClause->addColumn($this->variableIfNode($expression), $alias);
279+
$returnClause->addColumn($expression, $alias);
277280
}
278281

279282
$returnClause->setDistinct($distinct);
@@ -586,8 +589,12 @@ public function with($expressions): self
586589
throw new TypeError("\$expressions should only consist of AnyType objects");
587590
}
588591

592+
if ($expression instanceof Node) {
593+
$expression = $expression->getName();
594+
}
595+
589596
$alias = is_integer($maybeAlias) ? "" : $maybeAlias;
590-
$withClause->addEntry($this->variableIfNode($expression), $alias);
597+
$withClause->addEntry($expression, $alias);
591598
}
592599

593600
$this->clauses[] = $withClause;

src/Traits/IdentifierGenerationTrait.php

Lines changed: 0 additions & 41 deletions
This file was deleted.

0 commit comments

Comments
 (0)