Skip to content

Commit ee5f2d4

Browse files
authored
Merge branch 'name-generation' into name-generation
2 parents 0042cae + 9dc5091 commit ee5f2d4

21 files changed

+208
-70
lines changed

src/Alias.php

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,25 +5,31 @@
55
use WikibaseSolutions\CypherDSL\Types\AnyType;
66
use function sprintf;
77

8+
/**
9+
* Represents aliasing an expression or variable.
10+
*
11+
* @see https://neo4j.com/docs/cypher-manual/current/clauses/return/#return-column-alias
12+
* @see https://neo4j.com/docs/cypher-manual/current/clauses/with/#with-introduce-variables
13+
*/
814
class Alias implements AnyType
915
{
1016
/**
11-
* @var QueryConvertable The original item to be aliased
17+
* @var AnyType The original item to be aliased
1218
*/
13-
private QueryConvertable $original;
19+
private AnyType $original;
1420

1521
/**
1622
* @var Variable The new variable aliasing the original
1723
*/
1824
private Variable $variable;
1925

2026
/**
21-
* BinaryOperator constructor.
27+
* Alias constructor.
2228
*
23-
* @param QueryConvertable $original The original item to be aliased
29+
* @param AnyType $original The original item to be aliased
2430
* @param Variable $variable The new variable aliasing the original
2531
*/
26-
public function __construct(QueryConvertable $original, Variable $variable)
32+
public function __construct(AnyType $original, Variable $variable)
2733
{
2834
$this->original = $original;
2935
$this->variable = $variable;
@@ -40,9 +46,9 @@ public function toQuery(): string
4046
/**
4147
* Gets the original item of the alias.
4248
*
43-
* @return QueryConvertable
49+
* @return AnyType
4450
*/
45-
public function getOriginal(): QueryConvertable
51+
public function getOriginal(): AnyType
4652
{
4753
return $this->original;
4854
}

src/AndOperator.php

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

2222
namespace WikibaseSolutions\CypherDSL;
2323

24-
use WikibaseSolutions\CypherDSL\Traits\AliasableTrait;
2524
use WikibaseSolutions\CypherDSL\Traits\BooleanTypeTrait;
2625
use WikibaseSolutions\CypherDSL\Types\PropertyTypes\BooleanType;
2726

src/Literals/Boolean.php

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,6 @@
3131
class Boolean implements BooleanType
3232
{
3333
use BooleanTypeTrait;
34-
use AliasableTrait;
3534

3635
/**
3736
* @var bool The value

src/Literals/Decimal.php

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,6 @@
3131
class Decimal implements NumeralType
3232
{
3333
use NumeralTypeTrait;
34-
use AliasableTrait;
3534

3635
/**
3736
* @var string The value

src/Literals/StringLiteral.php

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,6 @@
3434
class StringLiteral implements StringType
3535
{
3636
use StringTypeTrait;
37-
use AliasableTrait;
3837

3938
/**
4039
* @var string

src/Patterns/Node.php

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,8 @@ public function getLabels(): array
6767
}
6868

6969
/**
70+
* Adds a label to the node.
71+
*
7072
* @param string $label
7173
* @return Node
7274
*/
@@ -108,6 +110,13 @@ public function toQuery(): string
108110
return "($nodeInner)";
109111
}
110112

113+
/**
114+
* Returns a property on the node.
115+
*
116+
* @param string $property The name of the property.
117+
*
118+
* @return Property
119+
*/
111120
public function property(string $property): Property
112121
{
113122
return new Property($this->getName(), $property);

src/Patterns/Path.php

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,18 +39,32 @@ public function __construct($nodes = null, $relationships = null)
3939
$this->relationships = is_array($relationships) ? array_values($relationships) : [$relationships];
4040
}
4141

42+
/**
43+
* @inheritDoc
44+
*/
4245
public function toQuery(): string
4346
{
47+
// If there are no nodes in the path, it must be empty.
4448
if (count($this->nodes) === 0) {
4549
return '';
4650
}
4751

4852
$cql = '';
53+
// If a variable exists, we need to assign following the expression to it
4954
if ($this->getVariable() !== null) {
5055
$cql = $this->getName()->toQuery() . ' = ';
5156
}
5257

58+
// We use the relationships as a reference point to iterate over.
59+
// The nodes position will be calculated using the index of the current relationship.
60+
// If R is the position of the relationship, N is the position of te node, and x the amount of relationships
61+
// in the path, then the positional structure is like this:
62+
// N0 - R0 - N1 - R1 - N2 - ... - Nx - Rx - N(x + 1)
5363
foreach ($this->relationships as $i => $relationship) {
64+
// To avoid a future crash, we already look for the node at the end of the relationship.
65+
// If the following node does not exist, we must break the loop early.
66+
// This case will be triggered if the amount of nodes is equal or less than the amount of relationships
67+
// and is thus very unlikely.
5468
if (!array_key_exists($i + 1, $this->nodes)) {
5569
--$i;
5670
break;
@@ -59,6 +73,10 @@ public function toQuery(): string
5973
$cql .= $relationship->toQuery();
6074
}
6175

76+
// Since the iteration leaves us at the final relationship, we still need to add the final node.
77+
// If the path is simply a single node, $i won't be defined, hence the null coalescing operator with -1. By
78+
// coalescing with -1 instead of 0, we remove the need of a separate if check, making both cases valid when they
79+
// are incremented by 1.
6280
$cql .= $this->nodes[($i ?? -1) + 1]->toQuery();
6381

6482
return $cql;
@@ -84,6 +102,9 @@ public function getRelationships(): array
84102
return $this->relationships;
85103
}
86104

105+
/**
106+
* @inheritDoc
107+
*/
87108
public function relationship(RelationshipType $relationship, NodeType $node): Path
88109
{
89110
$this->relationships[] = $relationship;
@@ -92,20 +113,29 @@ public function relationship(RelationshipType $relationship, NodeType $node): Pa
92113
return $this;
93114
}
94115

116+
/**
117+
* @inheritDoc
118+
*/
95119
public function relationshipTo(NodeType $node, ?string $type = null, $properties = null, $name = null): Path
96120
{
97121
$relationship = $this->buildRelationship(Relationship::DIR_RIGHT, $type, $properties, $name);
98122

99123
return $this->relationship($relationship, $node);
100124
}
101125

126+
/**
127+
* @inheritDoc
128+
*/
102129
public function relationshipFrom(NodeType $node, ?string $type = null, $properties = null, $name = null): Path
103130
{
104131
$relationship = $this->buildRelationship(Relationship::DIR_LEFT, $type, $properties, $name);
105132

106133
return $this->relationship($relationship, $node);
107134
}
108135

136+
/**
137+
* @inheritDoc
138+
*/
109139
public function relationshipUni(NodeType $node, ?string $type = null, $properties = null, $name = null): Path
110140
{
111141
$relationship = $this->buildRelationship(Relationship::DIR_UNI, $type, $properties, $name);

src/Patterns/Relationship.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@
2626
use LogicException;
2727
use WikibaseSolutions\CypherDSL\Property;
2828
use WikibaseSolutions\CypherDSL\Traits\EscapeTrait;
29-
use WikibaseSolutions\CypherDSL\Traits\RelationshipTrait;
29+
use WikibaseSolutions\CypherDSL\Traits\RelationshipTypeTrait;
3030
use WikibaseSolutions\CypherDSL\Types\StructuralTypes\RelationshipType;
3131

3232
/**
@@ -38,7 +38,7 @@
3838
class Relationship implements RelationshipType
3939
{
4040
use EscapeTrait;
41-
use RelationshipTrait;
41+
use RelationshipTypeTrait;
4242

4343
public const DIR_RIGHT = ["-", "->"];
4444
public const DIR_LEFT = ["<-", "-"];

src/RawExpression.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@
3131
use WikibaseSolutions\CypherDSL\Traits\MapTypeTrait;
3232
use WikibaseSolutions\CypherDSL\Traits\NodeTypeTrait;
3333
use WikibaseSolutions\CypherDSL\Traits\NumeralTypeTrait;
34-
use WikibaseSolutions\CypherDSL\Traits\RelationshipTrait;
34+
use WikibaseSolutions\CypherDSL\Traits\RelationshipTypeTrait;
3535
use WikibaseSolutions\CypherDSL\Traits\PointTrait;
3636
use WikibaseSolutions\CypherDSL\Traits\StringTypeTrait;
3737
use WikibaseSolutions\CypherDSL\Traits\TimeTrait;
@@ -78,7 +78,7 @@ class RawExpression implements
7878
use PointTrait;
7979
use NodeTypeTrait;
8080
use NumeralTypeTrait;
81-
use RelationshipTrait;
81+
use RelationshipTypeTrait;
8282
use StringTypeTrait;
8383
use TimeTrait;
8484
use AliasableTrait;
@@ -90,7 +90,7 @@ class RawExpression implements
9090

9191
/**
9292
* RawExpression constructor.
93-
*\
93+
*
9494
* @param string $expression The raw expression
9595
*/
9696
public function __construct(string $expression)

src/Traits/BooleanTypeTrait.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@
3333
trait BooleanTypeTrait
3434
{
3535
use PropertyTypeTrait;
36+
use AliasableTrait;
3637

3738
/**
3839
* Create a conjunction between this expression and the given expression.

0 commit comments

Comments
 (0)