Skip to content

Commit 4b55785

Browse files
committed
allowed relationships to paths and nodes
1 parent da6d208 commit 4b55785

File tree

5 files changed

+65
-32
lines changed

5 files changed

+65
-32
lines changed

src/Patterns/Path.php

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
use WikibaseSolutions\CypherDSL\Types\StructuralTypes\NodeType;
1010
use WikibaseSolutions\CypherDSL\Types\StructuralTypes\PathType;
1111
use WikibaseSolutions\CypherDSL\Types\StructuralTypes\RelationshipType;
12+
use WikibaseSolutions\CypherDSL\Types\StructuralTypes\StructuralType;
1213
use WikibaseSolutions\CypherDSL\Variable;
1314
use function array_key_exists;
1415
use function is_array;
@@ -105,42 +106,49 @@ public function getRelationships(): array
105106
/**
106107
* @inheritDoc
107108
*/
108-
public function relationship(RelationshipType $relationship, NodeType $node): Path
109+
public function relationship(RelationshipType $relationship, StructuralType $nodeOrPath): Path
109110
{
111+
self::assertClass('nodeOrPath', [__CLASS__, Node::class], $nodeOrPath);
112+
110113
$this->relationships[] = $relationship;
111-
$this->nodes[] = $node;
114+
if ($nodeOrPath instanceof self) {
115+
$this->relationships = array_merge($this->relationships, $nodeOrPath->getRelationships());
116+
$this->nodes = array_merge($this->nodes, $nodeOrPath->getNodes());
117+
} else {
118+
$this->nodes []= $nodeOrPath;
119+
}
112120

113121
return $this;
114122
}
115123

116124
/**
117125
* @inheritDoc
118126
*/
119-
public function relationshipTo(NodeType $node, ?string $type = null, $properties = null, $name = null): Path
127+
public function relationshipTo(StructuralType $nodeOrPath, ?string $type = null, $properties = null, $name = null): Path
120128
{
121129
$relationship = $this->buildRelationship(Relationship::DIR_RIGHT, $type, $properties, $name);
122130

123-
return $this->relationship($relationship, $node);
131+
return $this->relationship($relationship, $nodeOrPath);
124132
}
125133

126134
/**
127135
* @inheritDoc
128136
*/
129-
public function relationshipFrom(NodeType $node, ?string $type = null, $properties = null, $name = null): Path
137+
public function relationshipFrom(StructuralType $nodeOrPath, ?string $type = null, $properties = null, $name = null): Path
130138
{
131139
$relationship = $this->buildRelationship(Relationship::DIR_LEFT, $type, $properties, $name);
132140

133-
return $this->relationship($relationship, $node);
141+
return $this->relationship($relationship, $nodeOrPath);
134142
}
135143

136144
/**
137145
* @inheritDoc
138146
*/
139-
public function relationshipUni(NodeType $node, ?string $type = null, $properties = null, $name = null): Path
147+
public function relationshipUni(StructuralType $nodeOrPath, ?string $type = null, $properties = null, $name = null): Path
140148
{
141149
$relationship = $this->buildRelationship(Relationship::DIR_UNI, $type, $properties, $name);
142150

143-
return $this->relationship($relationship, $node);
151+
return $this->relationship($relationship, $nodeOrPath);
144152
}
145153

146154
/**

src/Traits/NodeTypeTrait.php

Lines changed: 21 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
use WikibaseSolutions\CypherDSL\Patterns\Path;
2525
use WikibaseSolutions\CypherDSL\Types\StructuralTypes\NodeType;
2626
use WikibaseSolutions\CypherDSL\Types\StructuralTypes\RelationshipType;
27+
use WikibaseSolutions\CypherDSL\Types\StructuralTypes\StructuralType;
2728

2829
/**
2930
* This trait should be used by any expression that returns a node.
@@ -33,23 +34,35 @@ trait NodeTypeTrait
3334
use HasPropertiesTrait;
3435
use HasVariableTrait;
3536

36-
public function relationship(RelationshipType $relationship, NodeType $node): Path
37+
/**
38+
* @inheritDoc
39+
*/
40+
public function relationship(RelationshipType $relationship, StructuralType $nodeOrPath): Path
3741
{
38-
return (new Path($this))->relationship($relationship, $node);
42+
return (new Path($this))->relationship($relationship, $nodeOrPath);
3943
}
4044

41-
public function relationshipTo(NodeType $node, ?string $type = null, $properties = null, $name = null): Path
45+
/**
46+
* @inheritDoc
47+
*/
48+
public function relationshipTo(StructuralType $nodeOrPath, ?string $type = null, $properties = null, $name = null): Path
4249
{
43-
return (new Path($this))->relationshipTo($node, $type, $properties, $name);
50+
return (new Path($this))->relationshipTo($nodeOrPath, $type, $properties, $name);
4451
}
4552

46-
public function relationshipFrom(NodeType $node, ?string $type = null, $properties = null, $name = null): Path
53+
/**
54+
* @inheritDoc
55+
*/
56+
public function relationshipFrom(StructuralType $nodeOrPath, ?string $type = null, $properties = null, $name = null): Path
4757
{
48-
return (new Path($this))->relationshipFrom($node, $type, $properties, $name);
58+
return (new Path($this))->relationshipFrom($nodeOrPath, $type, $properties, $name);
4959
}
5060

51-
public function relationshipUni(NodeType $node, ?string $type = null, $properties = null, $name = null): Path
61+
/**
62+
* @inheritDoc
63+
*/
64+
public function relationshipUni(StructuralType $nodeOrPath, ?string $type = null, $properties = null, $name = null): Path
5265
{
53-
return (new Path($this))->relationshipUni($node, $type, $properties, $name);
66+
return (new Path($this))->relationshipUni($nodeOrPath, $type, $properties, $name);
5467
}
5568
}

src/Types/StructuralTypes/StructuralType.php

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121

2222
namespace WikibaseSolutions\CypherDSL\Types\StructuralTypes;
2323

24+
use WikibaseSolutions\CypherDSL\Patterns\Node;
2425
use WikibaseSolutions\CypherDSL\Patterns\Path;
2526
use WikibaseSolutions\CypherDSL\PropertyMap;
2627
use WikibaseSolutions\CypherDSL\Types\AnyType;
@@ -49,44 +50,44 @@ interface StructuralType extends AnyType
4950
/**
5051
* Adds a new relationship from the end of the structural type to the node pattern.
5152
* @param RelationshipType $relationship
52-
* @param NodeType $node
53+
* @param Node|Path $nodeOrPath
5354
* @return Path
5455
*/
55-
public function relationship(RelationshipType $relationship, NodeType $node): Path;
56+
public function relationship(RelationshipType $relationship, StructuralType $nodeOrPath): Path;
5657

5758
/**
5859
* Adds a new relationship to the node pattern at the end of the structural type to form a path.
5960
*
60-
* @param NodeType $node The node to attach to the end of the structural type
61+
* @param NodeType|Path $nodeOrPath The node to attach to the end of the structural type
6162
* @param string|null $type The type of the relationship
6263
* @param array|PropertyMap|null $properties The properties to attach to the relationship
6364
* @param string|Variable|null $name The name fo the relationship
6465
*
6566
* @return Path
6667
*/
67-
public function relationshipTo(NodeType $node, ?string $type = null, $properties = null, $name = null): Path;
68+
public function relationshipTo(StructuralType $nodeOrPath, ?string $type = null, $properties = null, $name = null): Path;
6869

6970
/**
7071
* Adds a new relationship from the node pattern at the end of the structural type to form a path.
7172
*
72-
* @param NodeType $node The node to attach to the end of the structural type.
73+
* @param NodeType|Path $nodeOrPath The node to attach to the end of the structural type.
7374
* @param string|null $type The type of the relationship
7475
* @param array|PropertyMap|null $properties The properties to attach to the relationship
7576
* @param string|Variable|null $name The name fo the relationship
7677
*
7778
* @return Path
7879
*/
79-
public function relationshipFrom(NodeType $node, ?string $type = null, $properties = null, $name = null): Path;
80+
public function relationshipFrom(StructuralType $nodeOrPath, ?string $type = null, $properties = null, $name = null): Path;
8081

8182
/**
8283
* Adds a new unidirectional relationship to the node pattern at the end of the structural type to form a path.
8384
*
84-
* @param NodeType $node The node to attach to the end of the structural type.
85+
* @param NodeType|Path $nodeOrPath The node to attach to the end of the structural type.
8586
* @param string|null $type The type of the relationship
8687
* @param array|PropertyMap|null $properties The properties to attach to the relationship
8788
* @param string|Variable|null $name The name fo the relationship
8889
*
8990
* @return Path
9091
*/
91-
public function relationshipUni(NodeType $node, ?string $type = null, $properties = null, $name = null): Path;
92+
public function relationshipUni(StructuralType $nodeOrPath, ?string $type = null, $properties = null, $name = null): Path;
9293
}

src/Variable.php

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@
5353
use WikibaseSolutions\CypherDSL\Types\StructuralTypes\NodeType;
5454
use WikibaseSolutions\CypherDSL\Types\StructuralTypes\PathType;
5555
use WikibaseSolutions\CypherDSL\Types\StructuralTypes\RelationshipType;
56+
use WikibaseSolutions\CypherDSL\Types\StructuralTypes\StructuralType;
5657

5758
/**
5859
* Represents a variable.
@@ -149,32 +150,32 @@ public function toQuery(): string
149150
/**
150151
* @inheritDoc
151152
*/
152-
public function relationship(RelationshipType $relationship, NodeType $node): Path
153+
public function relationship(RelationshipType $relationship, StructuralType $nodeOrPath): Path
153154
{
154-
return (new Path((new Node())->named($this)))->relationship($relationship, $node);
155+
return (new Path((new Node())->named($this)))->relationship($relationship, $nodeOrPath);
155156
}
156157

157158
/**
158159
* @inheritDoc
159160
*/
160-
public function relationshipTo(NodeType $node, ?string $type = null, $properties = null, $name = null): Path
161+
public function relationshipTo(StructuralType $nodeOrPath, ?string $type = null, $properties = null, $name = null): Path
161162
{
162-
return (new Path((new Node())->named($this)))->relationshipTo($node, $type, $properties, $name);
163+
return (new Path((new Node())->named($this)))->relationshipTo($nodeOrPath, $type, $properties, $name);
163164
}
164165

165166
/**
166167
* @inheritDoc
167168
*/
168-
public function relationshipFrom(NodeType $node, ?string $type = null, $properties = null, $name = null): Path
169+
public function relationshipFrom(StructuralType $nodeOrPath, ?string $type = null, $properties = null, $name = null): Path
169170
{
170-
return (new Path((new Node())->named($this)))->relationshipFrom($node, $type, $properties, $name);
171+
return (new Path((new Node())->named($this)))->relationshipFrom($nodeOrPath, $type, $properties, $name);
171172
}
172173

173174
/**
174175
* @inheritDoc
175176
*/
176-
public function relationshipUni(NodeType $node, ?string $type = null, $properties = null, $name = null): Path
177+
public function relationshipUni(StructuralType $nodeOrPath, ?string $type = null, $properties = null, $name = null): Path
177178
{
178-
return (new Path((new Node())->named($this)))->relationshipUni($node, $type, $properties, $name);
179+
return (new Path((new Node())->named($this)))->relationshipUni($nodeOrPath, $type, $properties, $name);
179180
}
180181
}

tests/Unit/Patterns/PathTest.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,16 @@ public function testSingleNoRel(): void
4747
self::assertEquals('()', $path->toQuery());
4848
}
4949

50+
public function testPathMerge(): void
51+
{
52+
$pathX = new Path([new Node(), new Node()], [new Relationship(Relationship::DIR_UNI)]);
53+
$pathY = new Path([new Node(), new Node()], [new Relationship(Relationship::DIR_UNI)]);
54+
55+
$pathX->named('x')->relationshipTo($pathY->named('y'));
56+
57+
self::assertEquals('x = ()-[]-()-[]->()-[]-()', $pathX->toQuery());
58+
}
59+
5060
public function testRelationshipLong(): void
5161
{
5262
$path = new Path(new Node());

0 commit comments

Comments
 (0)