Skip to content

Commit 96bf611

Browse files
author
Wout Gevaert
committed
Add RelationshipType and NodeType to Variable
1 parent cd1302e commit 96bf611

File tree

6 files changed

+88
-18
lines changed

6 files changed

+88
-18
lines changed

src/Traits/HasPropertiesTrait.php

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
use function is_array;
66
use WikibaseSolutions\CypherDSL\PropertyMap;
77
use WikibaseSolutions\CypherDSL\Types\AnyType;
8+
use WikibaseSolutions\CypherDSL\Types\StructuralTypes\HasPropertiesType;
89

910
trait HasPropertiesTrait
1011
{
@@ -18,9 +19,9 @@ trait HasPropertiesTrait
1819
* @param string $key The name of the property
1920
* @param AnyType $value The value of the property
2021
*
21-
* @return static
22+
* @return HasPropertiesType
2223
*/
23-
public function withProperty(string $key, AnyType $value): self
24+
public function withProperty(string $key, AnyType $value): HasPropertiesType
2425
{
2526
$this->initialiseProperties();
2627

@@ -34,9 +35,9 @@ public function withProperty(string $key, AnyType $value): self
3435
*
3536
* @param PropertyMap|array $properties
3637
*
37-
* @return static
38+
* @return HasPropertiesType
3839
*/
39-
public function withProperties($properties): self
40+
public function withProperties($properties): HasPropertiesType
4041
{
4142
self::assertClass('properties', [PropertyMap::class, 'array'], $properties);
4243

src/Traits/NodeTypeTrait.php

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

2424
use WikibaseSolutions\CypherDSL\Patterns\Path;
2525
use WikibaseSolutions\CypherDSL\Types\StructuralTypes\RelationshipType;
26-
use WikibaseSolutions\CypherDSL\Types\StructuralTypes\StructuralType;
26+
use WikibaseSolutions\CypherDSL\Types\StructuralTypes\HasRelationshipsType;
2727

2828
/**
2929
* This trait should be used by any expression that returns a node.
@@ -36,31 +36,31 @@ trait NodeTypeTrait
3636
/**
3737
* @inheritDoc
3838
*/
39-
public function relationship(RelationshipType $relationship, StructuralType $nodeOrPath): Path
39+
public function relationship(RelationshipType $relationship, HasRelationshipsType $nodeOrPath): Path
4040
{
4141
return (new Path($this))->relationship($relationship, $nodeOrPath);
4242
}
4343

4444
/**
4545
* @inheritDoc
4646
*/
47-
public function relationshipTo(StructuralType $nodeOrPath, ?string $type = null, $properties = null, $name = null): Path
47+
public function relationshipTo(HasRelationshipsType $nodeOrPath, ?string $type = null, $properties = null, $name = null): Path
4848
{
4949
return (new Path($this))->relationshipTo($nodeOrPath, $type, $properties, $name);
5050
}
5151

5252
/**
5353
* @inheritDoc
5454
*/
55-
public function relationshipFrom(StructuralType $nodeOrPath, ?string $type = null, $properties = null, $name = null): Path
55+
public function relationshipFrom(HasRelationshipsType $nodeOrPath, ?string $type = null, $properties = null, $name = null): Path
5656
{
5757
return (new Path($this))->relationshipFrom($nodeOrPath, $type, $properties, $name);
5858
}
5959

6060
/**
6161
* @inheritDoc
6262
*/
63-
public function relationshipUni(StructuralType $nodeOrPath, ?string $type = null, $properties = null, $name = null): Path
63+
public function relationshipUni(HasRelationshipsType $nodeOrPath, ?string $type = null, $properties = null, $name = null): Path
6464
{
6565
return (new Path($this))->relationshipUni($nodeOrPath, $type, $properties, $name);
6666
}

src/Types/StructuralTypes/HasPropertiesType.php

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

2424
use WikibaseSolutions\CypherDSL\PropertyMap;
2525
use WikibaseSolutions\CypherDSL\Types\AnyType;
26+
use WikibaseSolutions\CypherDSL\Types\PropertyTypes\PropertyType;
2627

2728
interface HasPropertiesType extends StructuralType
2829
{
@@ -32,16 +33,16 @@ interface HasPropertiesType extends StructuralType
3233
* @param string $key The name of the property
3334
* @param AnyType $value The value of the property
3435
*
35-
* @return static
36+
* @return HasPropertiesType
3637
*/
37-
public function withProperty(string $key, AnyType $value): self;
38+
public function withProperty(string $key, PropertyType $value): HasPropertiesType;
3839

3940
/**
4041
* Add the given properties to the properties of this object.
4142
*
4243
* @param PropertyMap|array $properties
4344
*
44-
* @return static
45+
* @return HasPropertiesType
4546
*/
46-
public function withProperties($properties): self;
47+
public function withProperties($properties): HasPropertiesType;
4748
}

src/Types/StructuralTypes/HasRelationshipsType.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ interface HasRelationshipsType extends StructuralType
4545
* @param Node|Path $nodeOrPath
4646
* @return Path
4747
*/
48-
public function relationship(RelationshipType $relationship, StructuralType $nodeOrPath): Path;
48+
public function relationship(RelationshipType $relationship, HasRelationshipsType $nodeOrPath): Path;
4949

5050
/**
5151
* Adds a new relationship to the node pattern at the end of the structural type to form a path.
@@ -57,7 +57,7 @@ public function relationship(RelationshipType $relationship, StructuralType $nod
5757
*
5858
* @return Path
5959
*/
60-
public function relationshipTo(StructuralType $nodeOrPath, ?string $type = null, $properties = null, $name = null): Path;
60+
public function relationshipTo(HasRelationshipsType $nodeOrPath, ?string $type = null, $properties = null, $name = null): Path;
6161

6262
/**
6363
* Adds a new relationship from the node pattern at the end of the structural type to form a path.
@@ -69,7 +69,7 @@ public function relationshipTo(StructuralType $nodeOrPath, ?string $type = null,
6969
*
7070
* @return Path
7171
*/
72-
public function relationshipFrom(StructuralType $nodeOrPath, ?string $type = null, $properties = null, $name = null): Path;
72+
public function relationshipFrom(HasRelationshipsType $nodeOrPath, ?string $type = null, $properties = null, $name = null): Path;
7373

7474
/**
7575
* Adds a new unidirectional relationship to the node pattern at the end of the structural type to form a path.
@@ -81,5 +81,5 @@ public function relationshipFrom(StructuralType $nodeOrPath, ?string $type = nul
8181
*
8282
* @return Path
8383
*/
84-
public function relationshipUni(StructuralType $nodeOrPath, ?string $type = null, $properties = null, $name = null): Path;
84+
public function relationshipUni(HasRelationshipsType $nodeOrPath, ?string $type = null, $properties = null, $name = null): Path;
8585
}

src/Variable.php

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,9 +35,11 @@
3535
use WikibaseSolutions\CypherDSL\Traits\PointTrait;
3636
use WikibaseSolutions\CypherDSL\Traits\StringTypeTrait;
3737
use WikibaseSolutions\CypherDSL\Traits\TimeTrait;
38+
use WikibaseSolutions\CypherDSL\Patterns\Path;
3839
use WikibaseSolutions\CypherDSL\Types\AnyType;
3940
use WikibaseSolutions\CypherDSL\Types\CompositeTypes\ListType;
4041
use WikibaseSolutions\CypherDSL\Types\CompositeTypes\MapType;
42+
use WikibaseSolutions\CypherDSL\Types\PropertyTypes\PropertyType;
4143
use WikibaseSolutions\CypherDSL\Types\PropertyTypes\BooleanType;
4244
use WikibaseSolutions\CypherDSL\Types\PropertyTypes\DateTimeType;
4345
use WikibaseSolutions\CypherDSL\Types\PropertyTypes\DateType;
@@ -47,6 +49,12 @@
4749
use WikibaseSolutions\CypherDSL\Types\PropertyTypes\PointType;
4850
use WikibaseSolutions\CypherDSL\Types\PropertyTypes\StringType;
4951
use WikibaseSolutions\CypherDSL\Types\PropertyTypes\TimeType;
52+
use WikibaseSolutions\CypherDSL\Types\StructuralTypes\NodeType;
53+
use WikibaseSolutions\CypherDSL\Types\StructuralTypes\RelationshipType;
54+
use WikibaseSolutions\CypherDSL\Types\StructuralTypes\StructuralType;
55+
use WikibaseSolutions\CypherDSL\Types\StructuralTypes\HasPropertiesType;
56+
use WikibaseSolutions\CypherDSL\Types\StructuralTypes\HasRelationshipsType;
57+
5058

5159
/**
5260
* Represents a variable.
@@ -61,8 +69,10 @@ class Variable implements
6169
LocalDateTimeType,
6270
LocalTimeType,
6371
MapType,
72+
NodeType,
6473
NumeralType,
6574
PointType,
75+
RelationshipType,
6676
StringType,
6777
TimeType
6878
{
@@ -137,4 +147,57 @@ public function toQuery(): string
137147
{
138148
return self::escape($this->getName());
139149
}
150+
151+
private function toNode(): NodeType
152+
{
153+
return Query::node()->named($this);
154+
}
155+
156+
/**
157+
* @inheritdoc
158+
*/
159+
public function relationship(RelationshipType $relationship, HasRelationshipsType $nodeOrPath): Path
160+
{
161+
return $this->toNode()->relationship($relationship, $nodeOrPath);
162+
}
163+
164+
/**
165+
* @inheritdoc
166+
*/
167+
public function relationshipTo(HasRelationshipsType $nodeOrPath, ?string $type = null, $properties = null, $name = null): Path
168+
{
169+
return $this->toNode()->relationshipTo($nodeOrPath, $type, $properties, $name);
170+
}
171+
172+
/**
173+
* @inheritdoc
174+
*/
175+
public function relationshipFrom(HasRelationshipsType $nodeOrPath, ?string $type = null, $properties = null, $name = null): Path
176+
{
177+
return $this->toNode()->relationshipFrom($nodeOrPath, $type, $properties, $name);
178+
}
179+
180+
/**
181+
* @inheritdoc
182+
*/
183+
public function relationshipUni(HasRelationshipsType $nodeOrPath, ?string $type = null, $properties = null, $name = null): Path
184+
{
185+
return $this->toNode()->relationshipUni($nodeOrPath, $type, $properties, $name);
186+
}
187+
188+
/**
189+
* @inheritdoc
190+
*/
191+
public function withProperty(string $key, PropertyType $value): HasPropertiesType
192+
{
193+
return $this->toNode()->withProperties($key, $value);
194+
}
195+
196+
/**
197+
* @inheritdoc
198+
*/
199+
public function withProperties($properties): HasPropertiesType
200+
{
201+
return $this->toNode()->withProperties($properties);
202+
}
140203
}

tests/Unit/Traits/HasPropertiesTraitTest.php

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
use PHPUnit\Framework\TestCase;
66
use WikibaseSolutions\CypherDSL\PropertyMap;
77
use WikibaseSolutions\CypherDSL\Query;
8+
use WikibaseSolutions\CypherDSL\Types\StructuralTypes\HasPropertiesType;
89
use WikibaseSolutions\CypherDSL\Traits\HasPropertiesTrait;
910

1011
class HasPropertiesTraitTest extends TestCase
@@ -13,10 +14,14 @@ class HasPropertiesTraitTest extends TestCase
1314

1415
public function setUp(): void
1516
{
16-
$this->propertyTrait = new class () {
17+
$this->propertyTrait = new class () implements HasPropertiesType {
1718
use HasPropertiesTrait {
1819
initialiseProperties as public;
1920
}
21+
22+
public function toQuery(): string {
23+
return '';
24+
}
2025
};
2126
}
2227

0 commit comments

Comments
 (0)