Skip to content

Commit ab76017

Browse files
Minor tweaks
1 parent 464679f commit ab76017

File tree

4 files changed

+59
-38
lines changed

4 files changed

+59
-38
lines changed

src/Patterns/Node.php

Lines changed: 9 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -128,17 +128,6 @@ public function named($variable): self
128128
return $this;
129129
}
130130

131-
/**
132-
* @param string $label
133-
* @return Node
134-
*/
135-
public function labeled(string $label): self
136-
{
137-
$this->labels[] = $label;
138-
139-
return $this;
140-
}
141-
142131
/**
143132
* Returns the name of this node. This function automatically generates a name if the node does not have a
144133
* name yet.
@@ -148,7 +137,7 @@ public function labeled(string $label): self
148137
public function getName(): ?Variable
149138
{
150139
if (!isset($this->variable)) {
151-
$this->named($this->generateUUID());
140+
$this->named(new Variable());
152141
}
153142

154143
return $this->variable;
@@ -167,20 +156,19 @@ public function setName($variable): self
167156
}
168157

169158
/**
170-
* Returns true if and only if this node has a name.
171-
*
172-
* @return bool
159+
* @param string $label
160+
* @return Node
173161
*/
174-
public function hasName(): bool
162+
public function labeled(string $label): self
175163
{
176-
return isset($this->variable);
164+
$this->labels[] = $label;
165+
166+
return $this;
177167
}
178168

179169
/**
180-
* Returns the property of the given name for this expression. For instance, if this expression is the
181-
* variable "foo", a function call like $expression->property("bar") would yield "foo.bar".
182-
*
183-
* TODO: Maybe move this function to the NodeType trait and add it to the interface?
170+
* Returns the property of the given name for this node. For instance, if this node is "(foo:PERSON)", a function call
171+
* like $node->property("bar") would yield "foo.bar".
184172
*
185173
* @param string $property
186174
* @return Property
@@ -220,18 +208,4 @@ public function toQuery(): string
220208

221209
return "($nodeInner)";
222210
}
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-
}
237211
}

src/Query.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -120,10 +120,10 @@ public static function relationship(StructuralType $a, StructuralType $b, array
120120
/**
121121
* Creates a variable.
122122
*
123-
* @param string $variable
123+
* @param string $variable The name of the variable; leave empty to automatically generate a variable name
124124
* @return Variable
125125
*/
126-
public static function variable(string $variable): Variable
126+
public static function variable(?string $variable = null): Variable
127127
{
128128
return new Variable($variable);
129129
}

src/Variable.php

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,8 @@ class Variable implements
6969
StringType,
7070
TimeType
7171
{
72+
public const AUTOMATIC_VARIABLE_LENGTH = 32;
73+
7274
use EscapeTrait;
7375
use DateTrait;
7476
use DateTimeTrait;
@@ -93,8 +95,12 @@ class Variable implements
9395
*
9496
* @param string $variable The variable
9597
*/
96-
public function __construct(string $variable)
98+
public function __construct(?string $variable = null)
9799
{
100+
if ($variable === null) {
101+
$variable = $this->generateUUID(self::AUTOMATIC_VARIABLE_LENGTH);
102+
}
103+
98104
$this->variable = $variable;
99105
}
100106

@@ -139,4 +145,18 @@ public function toQuery(): string
139145
{
140146
return $this->escape($this->variable);
141147
}
148+
149+
/**
150+
* Generates a unique random identifier.
151+
*
152+
* @note It is not entirely guaranteed that this function gives a truly unique identifier. However, because the
153+
* number of possible IDs is so huge, it should not be a problem.
154+
*
155+
* @param int $length
156+
* @return string
157+
*/
158+
private static function generateUUID(int $length): string
159+
{
160+
return substr(bin2hex(openssl_random_pseudo_bytes(ceil($length / 2))), 0, $length);
161+
}
142162
}

tests/Unit/QueryTest.php

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,13 @@ public function testVariable()
9292
$this->assertInstanceOf(Variable::class, Query::variable("foo"));
9393
}
9494

95+
public function testVariableEmpty()
96+
{
97+
$this->assertInstanceOf(Variable::class, Query::variable());
98+
99+
$this->assertMatchesRegularExpression('/[0-9a-f]+/', Query::variable()->toQuery());
100+
}
101+
95102
public function testParameter()
96103
{
97104
$this->assertInstanceOf(Parameter::class, Query::parameter("foo"));
@@ -875,6 +882,26 @@ public function testWikiExamples()
875882
$this->assertSame("((nineties.released >= 1990) AND (nineties IS NOT NULL))", $expression->toQuery());
876883
}
877884

885+
public function testAutomaticIdentifierGeneration()
886+
{
887+
$node = Query::node();
888+
889+
$this->assertMatchesRegularExpression('/[0-9a-f]+\.foo/', $node->property('foo')->toQuery());
890+
891+
$node->named('foo');
892+
893+
$this->assertSame('foo.bar', $node->property('bar')->toQuery());
894+
895+
$node = Query::node();
896+
$statement = Query::new()->match($node)->returning($node)->build();
897+
898+
$this->assertMatchesRegularExpression('/MATCH \([0-9a-f]+\) RETURN [0-9a-f]+/', $statement);
899+
900+
$node = Query::node();
901+
902+
$this->assertInstanceOf(Variable::class, $node->getName());
903+
}
904+
878905
public function provideLiteralData(): array
879906
{
880907
return [

0 commit comments

Comments
 (0)