Skip to content

Commit 8f3bf29

Browse files
Move Alias and PropertyReplacement to Syntax namespace
1 parent 8632524 commit 8f3bf29

File tree

14 files changed

+127
-137
lines changed

14 files changed

+127
-137
lines changed

src/Clauses/SetClause.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,8 @@
2222
namespace WikibaseSolutions\CypherDSL\Clauses;
2323

2424
use WikibaseSolutions\CypherDSL\Expressions\Label;
25-
use WikibaseSolutions\CypherDSL\PropertyReplacement;
2625
use WikibaseSolutions\CypherDSL\QueryConvertible;
26+
use WikibaseSolutions\CypherDSL\Syntax\PropertyReplacement;
2727
use WikibaseSolutions\CypherDSL\Traits\ErrorTrait;
2828

2929
/**

src/Expressions/Property.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121

2222
namespace WikibaseSolutions\CypherDSL\Expressions;
2323

24-
use WikibaseSolutions\CypherDSL\PropertyReplacement;
24+
use WikibaseSolutions\CypherDSL\Syntax\PropertyReplacement;
2525
use WikibaseSolutions\CypherDSL\Traits\TypeTraits\CompositeTypeTraits\ListTypeTrait;
2626
use WikibaseSolutions\CypherDSL\Traits\TypeTraits\CompositeTypeTraits\MapTypeTrait;
2727
use WikibaseSolutions\CypherDSL\Traits\TypeTraits\PropertyTypeTraits\BooleanTypeTrait;

src/Patterns/Relationship.php

Lines changed: 74 additions & 89 deletions
Original file line numberDiff line numberDiff line change
@@ -25,13 +25,9 @@
2525
use InvalidArgumentException;
2626
use LogicException;
2727
use WikibaseSolutions\CypherDSL\Expressions\Literals\Map;
28-
use WikibaseSolutions\CypherDSL\Expressions\Variable;
29-
use WikibaseSolutions\CypherDSL\Traits\CastTrait;
30-
use WikibaseSolutions\CypherDSL\Traits\ErrorTrait;
3128
use WikibaseSolutions\CypherDSL\Traits\EscapeTrait;
3229
use WikibaseSolutions\CypherDSL\Traits\HasPropertiesTrait;
3330
use WikibaseSolutions\CypherDSL\Traits\HasVariableTrait;
34-
use WikibaseSolutions\CypherDSL\Types\CompositeTypes\MapType;
3531

3632
/**
3733
* This class represents an arbitrary relationship.
@@ -41,9 +37,8 @@
4137
*/
4238
final class Relationship
4339
{
44-
use CastTrait;
45-
use ErrorTrait;
4640
use EscapeTrait;
41+
4742
use HasPropertiesTrait;
4843
use HasVariableTrait;
4944

@@ -76,16 +71,16 @@ final class Relationship
7671
*/
7772
private ?int $exactHops = null;
7873

79-
/**
80-
* @var bool Whether to allow arbitrary hops between nodes
81-
*/
82-
private bool $arbitraryHops = false;
74+
/**
75+
* @var bool Whether to allow arbitrary hops between nodes
76+
*/
77+
private bool $arbitraryHops = false;
8378

84-
/**
79+
/**
8580
* @param array $direction The direction of the relationship, should be either:
8681
* - Relationship::DIR_RIGHT (for a relation of (a)-->(b))
87-
* - Relationship::DIR_LEFT (for a relation of (a)<--(b))
88-
* - Relationship::DIR_UNI (for a relation of (a)--(b))
82+
* - Relationship::DIR_LEFT (for a relation of (a)<--(b))
83+
* - Relationship::DIR_UNI (for a relation of (a)--(b))
8984
*/
9085
public function __construct(array $direction)
9186
{
@@ -173,39 +168,39 @@ public function withExactHops(int $exactHops): self
173168
return $this;
174169
}
175170

176-
/**
177-
* Set the number of hops to be an arbitrary number (wildcard).
178-
*
179-
* @param bool $arbitraryHops
180-
* @return $this
181-
*/
182-
public function setArbitraryHops(bool $arbitraryHops = true): self
183-
{
184-
if (isset($this->minHops) || isset($this->maxHops) || isset($this->exactHops)) {
185-
throw new LogicException("arbitraryHops cannot be used in combination with minHops, maxHops or exactHops");
186-
}
187-
188-
$this->arbitraryHops = $arbitraryHops;
189-
190-
return $this;
191-
}
192-
193-
/**
194-
* The types to require for this relationship. Will overwrite any previously set types.
195-
*
196-
* @param string[] $types
197-
* @return $this
198-
*/
199-
public function withTypes(array $types): self
200-
{
201-
$this->types = $types;
202-
203-
return $this;
204-
}
171+
/**
172+
* Set the number of hops to be an arbitrary number (wildcard).
173+
*
174+
* @param bool $arbitraryHops
175+
* @return $this
176+
*/
177+
public function setArbitraryHops(bool $arbitraryHops = true): self
178+
{
179+
if (isset($this->minHops) || isset($this->maxHops) || isset($this->exactHops)) {
180+
throw new LogicException("arbitraryHops cannot be used in combination with minHops, maxHops or exactHops");
181+
}
182+
183+
$this->arbitraryHops = $arbitraryHops;
184+
185+
return $this;
186+
}
205187

206188
/**
207-
* Add one or more types to require for this relationship.
208-
*
189+
* The types to require for this relationship. Will overwrite any previously set types.
190+
*
191+
* @param string[] $types
192+
* @return $this
193+
*/
194+
public function withTypes(array $types): self
195+
{
196+
$this->types = $types;
197+
198+
return $this;
199+
}
200+
201+
/**
202+
* Add one or more types to require for this relationship.
203+
*
209204
* @param string ...$type
210205
* @return $this
211206
*/
@@ -227,54 +222,44 @@ public function getDirection(): array
227222
}
228223

229224
/**
230-
* Returns the properties of this node.
225+
* Returns the exact amount of hops configured.
231226
*
232-
* @return MapType
227+
* @return int|null
233228
*/
234-
public function getProperties(): ?MapType
229+
public function getExactHops(): ?int
235230
{
236-
return $this->properties;
231+
return $this->exactHops;
237232
}
238233

239-
/**
240-
* Returns the exact amount of hops configured.
241-
*
242-
* @return int|null
243-
*/
244-
public function getExactHops(): ?int
245-
{
246-
return $this->exactHops;
247-
}
248-
249-
/**
250-
* Returns the maximum amount of hops configured
251-
*
252-
* @return int|null
253-
*/
254-
public function getMaxHops(): ?int
255-
{
256-
return $this->maxHops;
257-
}
258-
259-
/**
260-
* Returns the minimum amount of hops configured.
261-
*
262-
* @return int|null
263-
*/
264-
public function getMinHops(): ?int
265-
{
266-
return $this->minHops;
267-
}
268-
269-
/**
270-
* Returns the types of the relationship.
271-
*
272-
* @return string[]
273-
*/
274-
public function getTypes(): array
275-
{
276-
return $this->types;
277-
}
234+
/**
235+
* Returns the maximum amount of hops configured
236+
*
237+
* @return int|null
238+
*/
239+
public function getMaxHops(): ?int
240+
{
241+
return $this->maxHops;
242+
}
243+
244+
/**
245+
* Returns the minimum amount of hops configured.
246+
*
247+
* @return int|null
248+
*/
249+
public function getMinHops(): ?int
250+
{
251+
return $this->minHops;
252+
}
253+
254+
/**
255+
* Returns the types of the relationship.
256+
*
257+
* @return string[]
258+
*/
259+
public function getTypes(): array
260+
{
261+
return $this->types;
262+
}
278263

279264
/**
280265
* Returns the string representation of this relationship that can be used directly
@@ -324,8 +309,8 @@ private function relationshipDetailToString(): string
324309
$conditionInner .= '*' . $this->exactHops;
325310
} elseif ($this->arbitraryHops) {
326311
// We have an arbitrary number of hops
327-
$conditionInner .= '*';
328-
}
312+
$conditionInner .= '*';
313+
}
329314

330315
if (isset($this->properties)) {
331316
// Only add the properties if they're not empty

src/Query.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@
5454
use WikibaseSolutions\CypherDSL\Expressions\Variable;
5555
use WikibaseSolutions\CypherDSL\Patterns\Node;
5656
use WikibaseSolutions\CypherDSL\Patterns\Relationship;
57+
use WikibaseSolutions\CypherDSL\Syntax\PropertyReplacement;
5758
use WikibaseSolutions\CypherDSL\Traits\ErrorTrait;
5859
use WikibaseSolutions\CypherDSL\Traits\EscapeTrait;
5960
use WikibaseSolutions\CypherDSL\Types\AnyType;

src/Alias.php renamed to src/Syntax/Alias.php

Lines changed: 21 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,10 @@
1919
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
2020
*/
2121

22-
namespace WikibaseSolutions\CypherDSL;
22+
namespace WikibaseSolutions\CypherDSL\Syntax;
2323

2424
use WikibaseSolutions\CypherDSL\Expressions\Variable;
25+
use WikibaseSolutions\CypherDSL\QueryConvertible;
2526
use WikibaseSolutions\CypherDSL\Types\AnyType;
2627
use function sprintf;
2728

@@ -31,7 +32,7 @@
3132
* @see https://neo4j.com/docs/cypher-manual/current/clauses/return/#return-column-alias
3233
* @see https://neo4j.com/docs/cypher-manual/current/clauses/with/#with-introduce-variables
3334
*/
34-
final class Alias
35+
final class Alias implements QueryConvertible
3536
{
3637
/**
3738
* @var AnyType The original item to be aliased
@@ -55,25 +56,25 @@ public function __construct(AnyType $original, Variable $variable)
5556
$this->variable = $variable;
5657
}
5758

58-
/**
59-
* Gets the original item of the alias.
60-
*
61-
* @return AnyType
62-
*/
63-
public function getOriginal(): AnyType
64-
{
65-
return $this->original;
66-
}
59+
/**
60+
* Gets the original item of the alias.
61+
*
62+
* @return AnyType
63+
*/
64+
public function getOriginal(): AnyType
65+
{
66+
return $this->original;
67+
}
6768

68-
/**
69-
* Gets the variable from the alias.
70-
*
71-
* @return Variable
72-
*/
73-
public function getVariable(): Variable
74-
{
75-
return $this->variable;
76-
}
69+
/**
70+
* Gets the variable from the alias.
71+
*
72+
* @return Variable
73+
*/
74+
public function getVariable(): Variable
75+
{
76+
return $this->variable;
77+
}
7778

7879
/**
7980
* @inheritDoc

src/PropertyReplacement.php renamed to src/Syntax/PropertyReplacement.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,10 +19,11 @@
1919
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
2020
*/
2121

22-
namespace WikibaseSolutions\CypherDSL;
22+
namespace WikibaseSolutions\CypherDSL\Syntax;
2323

2424
use WikibaseSolutions\CypherDSL\Expressions\Property;
2525
use WikibaseSolutions\CypherDSL\Expressions\Variable;
26+
use WikibaseSolutions\CypherDSL\QueryConvertible;
2627
use WikibaseSolutions\CypherDSL\Traits\ErrorTrait;
2728
use WikibaseSolutions\CypherDSL\Types\AnyType;
2829

src/Traits/HasPropertiesTrait.php

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

2424
use TypeError;
2525
use WikibaseSolutions\CypherDSL\Expressions\Literals\Map;
26-
use WikibaseSolutions\CypherDSL\Query;
2726
use WikibaseSolutions\CypherDSL\Types\CompositeTypes\MapType;
2827

2928
/**
@@ -65,10 +64,12 @@ public function addProperty(string $key, $property): self
6564
{
6665
if (!isset($this->properties)) {
6766
$this->properties = new Map();
68-
}
69-
70-
if (!$this->properties instanceof Map) {
71-
throw new TypeError('$this->properties must be of type PropertyMap to support "addProperty"');
67+
} elseif (!$this->properties instanceof Map) {
68+
// Adding to a map is not natively supported by the MapType, but it is supported by Map. Syntactically, it
69+
// is not possible to add new items to, for instance, a Variable, eventhough it implements MapType. It is
70+
// however still useful to be able to add items to objects where a Map is used (that is, an object of
71+
// MapType with the {} syntax).
72+
throw new TypeError('$this->properties must be of type Map to support "addProperty"');
7273
}
7374

7475
$this->properties->addProperty($key, $property);
@@ -86,15 +87,16 @@ public function addProperty(string $key, $property): self
8687
*/
8788
public function addProperties($properties): self
8889
{
90+
self::assertClass('properties', [Map::class, 'array'], $properties);
91+
8992
if (!isset($this->properties)) {
9093
$this->properties = new Map();
91-
}
92-
93-
if (!$this->properties instanceof Map) {
94+
} elseif (!$this->properties instanceof Map) {
9495
throw new TypeError('$this->properties must be of type PropertyMap to support "addProperty"');
9596
}
9697

97-
if (!$properties instanceof Map) {
98+
if (is_array($properties)) {
99+
// Cast the array to a Map
98100
$properties = new Map($properties);
99101
}
100102

@@ -112,4 +114,4 @@ public function getProperties(): ?MapType
112114
{
113115
return $this->properties;
114116
}
115-
}
117+
}

src/Traits/StringGenerationTrait.php

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -29,13 +29,13 @@ trait StringGenerationTrait
2929
/**
3030
* Generates a unique random identifier.
3131
*
32-
* @note It is not entirely guaranteed that this function gives a truly unique string. However, because the
33-
* number of possible strings is so huge, it should not be a problem. The returned string will consist of
34-
* $length (default: 32) hexadecimal digits, prepended with $prefix. Since the prefix is predetermined, it does
35-
* not count towards the entropy of the returned string. Therefore, given the default length of 32, the change
36-
* of any two strings generated by this function being the same is 1 / 16^32, or about 1 / 3.4 * 10^38.
37-
*
38-
* @note The returned string is not cryptographically secure.
32+
* It is not entirely guaranteed that this function gives a truly unique string. However, because the
33+
* number of possible strings is so huge, it should not be a problem. The returned string will consist of
34+
* $length (default: 32) hexadecimal digits, prepended with $prefix. Since the prefix is predetermined, it does
35+
* not count towards the entropy of the returned string. Therefore, given the default length of 32, the change
36+
* of any two strings generated by this function being the same is 1 / 16^32, or about 1 / 3.4 * 10^38.
37+
*
38+
* @note The returned string is not cryptographically secure.
3939
*
4040
* @param string $prefix The prefix to put before the name. Must start with a letter to adhere to cypher namings.
4141
* @param int|null $length The length of the generated name in bytes.

0 commit comments

Comments
 (0)