25
25
use WikibaseSolutions \CypherDSL \Property ;
26
26
use WikibaseSolutions \CypherDSL \PropertyMap ;
27
27
use WikibaseSolutions \CypherDSL \Traits \EscapeTrait ;
28
- use WikibaseSolutions \CypherDSL \Traits \IdentifierGenerationTrait ;
29
28
use WikibaseSolutions \CypherDSL \Traits \NodeTypeTrait ;
30
29
use WikibaseSolutions \CypherDSL \Types \AnyType ;
31
30
use WikibaseSolutions \CypherDSL \Types \CompositeTypes \MapType ;
@@ -41,7 +40,6 @@ class Node implements NodeType
41
40
{
42
41
use EscapeTrait;
43
42
use NodeTypeTrait;
44
- use IdentifierGenerationTrait;
45
43
46
44
/**
47
45
* @var string[]
@@ -142,12 +140,17 @@ public function labeled(string $label): self
142
140
}
143
141
144
142
/**
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.
146
145
*
147
146
* @return Variable|null The name of this node, or NULL if this node does not have a name
148
147
*/
149
148
public function getName (): ?Variable
150
149
{
150
+ if (!isset ($ this ->variable )) {
151
+ $ this ->named ($ this ->generateUUID ());
152
+ }
153
+
151
154
return $ this ->variable ;
152
155
}
153
156
@@ -177,12 +180,14 @@ public function hasName(): bool
177
180
* Returns the property of the given name for this expression. For instance, if this expression is the
178
181
* variable "foo", a function call like $expression->property("bar") would yield "foo.bar".
179
182
*
183
+ * TODO: Maybe move this function to the NodeType trait and add it to the interface?
184
+ *
180
185
* @param string $property
181
186
* @return Property
182
187
*/
183
188
public function property (string $ property ): Property
184
189
{
185
- return new Property ($ this ->variableIfNode ( $ this ), $ property );
190
+ return new Property ($ this ->getName ( ), $ property );
186
191
}
187
192
188
193
/**
@@ -215,4 +220,18 @@ public function toQuery(): string
215
220
216
221
return "( $ nodeInner) " ;
217
222
}
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
+ }
218
237
}
0 commit comments