Skip to content

Commit ca43936

Browse files
committed
added path pattern
1 parent a12ac4b commit ca43936

File tree

4 files changed

+87
-2
lines changed

4 files changed

+87
-2
lines changed

src/Patterns/Path.php

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
<?php
2+
3+
namespace WikibaseSolutions\CypherDSL\Patterns;
4+
5+
use WikibaseSolutions\CypherDSL\Traits\PathTrait;
6+
use WikibaseSolutions\CypherDSL\Types\StructuralTypes\PathType;
7+
use WikibaseSolutions\CypherDSL\Types\StructuralTypes\StructuralType;
8+
9+
class Path implements PathType
10+
{
11+
use PathTrait;
12+
13+
/** @var Relationship[] */
14+
private array $relationships = [];
15+
16+
public function __construct()
17+
{
18+
}
19+
20+
public function toQuery(): string
21+
{
22+
$cql = '';
23+
if ($this->getVariable() !== null) {
24+
$cql = $this->getName()->toQuery() . ' = ';
25+
}
26+
27+
foreach ($this->relationships as $relationship) {
28+
$cql .= $relationship->getLeft()->toQuery();
29+
$cql .= $relationship->toRelationshipQuery();
30+
}
31+
32+
if (isset($relationship)) {
33+
$cql .= $relationship->getRight()->toQuery();
34+
}
35+
36+
return $cql;
37+
}
38+
39+
public function relationshipTo(StructuralType $pattern): Path
40+
{
41+
$this->relationships[] = $this->getLatestNode()->relationshipUni($pattern);
42+
43+
return $this;
44+
}
45+
46+
public function relationshipFrom(StructuralType $pattern): Path
47+
{
48+
$this->relationships[] = $this->getLatestNode()->relationshipUni($pattern);
49+
50+
return $this;
51+
}
52+
53+
public function relationshipUni(StructuralType $pattern): Path
54+
{
55+
$this->relationships[] = $this->getLatestNode()->relationshipUni($pattern);
56+
57+
return $this;
58+
}
59+
60+
private function getLatestNode(): StructuralType
61+
{
62+
return $this->relationships[count($this->relationships) - 1]->getRight();
63+
}
64+
}

src/Patterns/Relationship.php

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
use DomainException;
2525
use InvalidArgumentException;
2626
use LogicException;
27+
use WikibaseSolutions\CypherDSL\Property;
2728
use WikibaseSolutions\CypherDSL\Traits\EscapeTrait;
2829
use WikibaseSolutions\CypherDSL\Traits\RelationshipTrait;
2930
use WikibaseSolutions\CypherDSL\Types\StructuralTypes\RelationshipType;
@@ -201,7 +202,7 @@ public function toQuery(): string
201202
$a = $this->left->toQuery();
202203
$b = $this->right->toQuery();
203204

204-
return $a . $this->direction[0] . $this->conditionToString() . $this->direction[1] . $b;
205+
return $a . $this->toRelationshipQuery() . $b;
205206
}
206207

207208
/**
@@ -327,4 +328,14 @@ public function property(string $property): Property
327328
{
328329
return new Property($this->getName(), $property);
329330
}
331+
332+
/**
333+
* Returns the relationship part of the pattern without the nodes.
334+
*
335+
* @return string
336+
*/
337+
public function toRelationshipQuery(): string
338+
{
339+
return $this->direction[0] . $this->conditionToString() . $this->direction[1];
340+
}
330341
}

src/Traits/PathTrait.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
<?php
2+
3+
namespace WikibaseSolutions\CypherDSL\Traits;
4+
5+
trait PathTrait
6+
{
7+
use HasVariableTrait;
8+
}

src/Types/StructuralTypes/PathType.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,9 +21,11 @@
2121

2222
namespace WikibaseSolutions\CypherDSL\Types\StructuralTypes;
2323

24+
use WikibaseSolutions\CypherDSL\Types\AnyType;
25+
2426
/**
2527
* Represents the type "node".
2628
*/
27-
interface PathType extends StructuralType
29+
interface PathType extends AnyType
2830
{
2931
}

0 commit comments

Comments
 (0)