Skip to content

Commit 859edd9

Browse files
committed
wip
1 parent 12df67c commit 859edd9

File tree

6 files changed

+113
-11
lines changed

6 files changed

+113
-11
lines changed

src/Helper/ToStringHelper.php

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@
66

77
use Exception;
88
use Stringable;
9+
use Syndesi\CypherDataStructures\Contract\NodeInterface;
10+
use Syndesi\CypherDataStructures\Contract\RelationInterface;
911
use Syndesi\CypherDataStructures\Exception\InvalidArgumentException;
1012

1113
class ToStringHelper
@@ -159,4 +161,64 @@ public static function labelsToString(array $labels, bool $escapeAllLabels = fal
159161

160162
return implode($parts);
161163
}
164+
165+
/**
166+
* Turns a node to its Cypher string variant.
167+
*
168+
* @note relations are not included.
169+
*/
170+
public static function nodeToString(NodeInterface $node, bool $identifying = false): string
171+
{
172+
$parts = [];
173+
$parts[] = self::labelsToString($node->getLabels());
174+
$properties = $node->getProperties();
175+
if ($identifying) {
176+
$properties = $node->getIdentifiers();
177+
}
178+
$propertyString = self::propertyArrayToString($properties);
179+
if (strlen($propertyString) > 0) {
180+
$parts[] = sprintf("{%s}", $propertyString);
181+
}
182+
183+
return sprintf(
184+
"(%s)",
185+
implode(' ', $parts)
186+
);
187+
}
188+
189+
public static function relationToString(RelationInterface $relation, bool $identifying = false, bool $withNodes = true): string
190+
{
191+
$parts = [];
192+
if ($withNodes) {
193+
if (($startNode = $relation->getStartNode()) === null) {
194+
throw new InvalidArgumentException('Start node can not be null');
195+
}
196+
$parts[] = self::nodeToString($startNode, true);
197+
$parts[] = '-';
198+
}
199+
200+
$relationParts = [];
201+
if ($type = $relation->getType()) {
202+
$relationParts[] = sprintf(':%s', $type);
203+
}
204+
$properties = $relation->getProperties();
205+
if ($identifying) {
206+
$properties = $relation->getIdentifiers();
207+
}
208+
$propertyString = self::propertyArrayToString($properties);
209+
if (strlen($propertyString) > 0) {
210+
$relationParts[] = sprintf("{%s}", $propertyString);
211+
}
212+
$parts[] = sprintf("[%s]", implode(' ', $relationParts));
213+
214+
if ($withNodes) {
215+
if (($endNode = $relation->getEndNode()) === null) {
216+
throw new InvalidArgumentException('End node can not be null');
217+
}
218+
$parts[] = '->';
219+
$parts[] = self::nodeToString($endNode, true);
220+
}
221+
222+
return implode('', $parts);
223+
}
162224
}

src/Type/Node.php

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
use Syndesi\CypherDataStructures\Contract\RelationInterface;
99
use Syndesi\CypherDataStructures\Exception\InvalidArgumentException;
1010
use Syndesi\CypherDataStructures\Helper\ToCypherHelper;
11+
use Syndesi\CypherDataStructures\Helper\ToStringHelper;
1112
use Syndesi\CypherDataStructures\Trait\IdentifiersTrait;
1213

1314
class Node implements NodeInterface
@@ -25,7 +26,7 @@ class Node implements NodeInterface
2526

2627
public function __toString()
2728
{
28-
return ToCypherHelper::nodeToCypherString($this);
29+
return ToStringHelper::nodeToString($this);
2930
}
3031

3132
public function addLabel(string $label): self
@@ -82,9 +83,9 @@ public function addRelation(RelationInterface $relation): self
8283
throw new InvalidArgumentException("End node must be set");
8384
}
8485

85-
$ownIdentifyingString = ToCypherHelper::nodeToIdentifyingCypherString($this);
86-
if (ToCypherHelper::nodeToIdentifyingCypherString($startNode) !== $ownIdentifyingString &&
87-
ToCypherHelper::nodeToIdentifyingCypherString($endNode) !== $ownIdentifyingString
86+
$ownIdentifyingString = ToStringHelper::nodeToString($this, true);
87+
if (ToStringHelper::nodeToString($startNode, true) !== $ownIdentifyingString &&
88+
ToStringHelper::nodeToString($endNode, true) !== $ownIdentifyingString
8889
) {
8990
throw new InvalidArgumentException("Adding a relation to a node requires that either the start node or the end node must be the same as the node itself.");
9091
}
@@ -149,6 +150,6 @@ public function isEqualTo(mixed $element): bool
149150
return false;
150151
}
151152

152-
return ToCypherHelper::nodeToIdentifyingCypherString($this) === ToCypherHelper::nodeToIdentifyingCypherString($element);
153+
return ToStringHelper::nodeToString($this, true) === ToStringHelper::nodeToString($element, true);
153154
}
154155
}

src/Type/Relation.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66

77
use Syndesi\CypherDataStructures\Contract\NodeInterface;
88
use Syndesi\CypherDataStructures\Contract\RelationInterface;
9-
use Syndesi\CypherDataStructures\Helper\ToCypherHelper;
9+
use Syndesi\CypherDataStructures\Helper\ToStringHelper;
1010
use Syndesi\CypherDataStructures\Trait\IdentifiersTrait;
1111

1212
/**
@@ -24,7 +24,7 @@ class Relation implements RelationInterface
2424

2525
public function __toString()
2626
{
27-
return ToCypherHelper::relationToCypherString($this);
27+
return ToStringHelper::relationToString($this);
2828
}
2929

3030
public function getStartNode(): ?NodeInterface
@@ -69,6 +69,6 @@ public function isEqualTo(mixed $element): bool
6969
return false;
7070
}
7171

72-
return ToCypherHelper::relationToIdentifyingCypherString($this) === ToCypherHelper::relationToIdentifyingCypherString($element);
72+
return ToStringHelper::relationToString($this, identifying: true) === ToStringHelper::relationToString($element, identifying: true);
7373
}
7474
}

tests/Helper/ToStringHelperTest.php

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@
77
use PHPUnit\Framework\TestCase;
88
use Syndesi\CypherDataStructures\Exception\InvalidArgumentException;
99
use Syndesi\CypherDataStructures\Helper\ToStringHelper;
10+
use Syndesi\CypherDataStructures\Type\Node;
11+
use Syndesi\CypherDataStructures\Type\Relation;
1012

1113
class ToStringHelperTest extends TestCase
1214
{
@@ -102,4 +104,42 @@ public function testLabelsToString(): void
102104
$labels = ['a', 'z', 'b', 'E', '_c', '012', 'problematic label', '#label'];
103105
$this->assertSame(":`#label`:`012`:E:_c:a:b:`problematic label`:z", ToStringHelper::labelsToString($labels));
104106
}
107+
108+
public function testNodeToString(): void
109+
{
110+
$node = (new Node())
111+
->addLabels(['NodeA', 'NodeZ', 'NodeC', '_node', '01235'])
112+
->addProperties([
113+
'id' => 123,
114+
'hello' => 'world :D',
115+
])
116+
->addIdentifier('id');
117+
$this->assertSame("(:`01235`:NodeA:NodeC:NodeZ:_node {hello: 'world :D', id: 123})", ToStringHelper::nodeToString($node));
118+
$this->assertSame("(:`01235`:NodeA:NodeC:NodeZ:_node {id: 123})", ToStringHelper::nodeToString($node, true));
119+
}
120+
121+
public function testRelationToString(): void
122+
{
123+
$nodeA = (new Node())
124+
->addLabel('NodeA')
125+
->addProperty('id', 123)
126+
->addIdentifier('id');
127+
$nodeB = (new Node())
128+
->addLabel('NodeB')
129+
->addProperty('id', 321)
130+
->addIdentifier('id');
131+
$relation = (new Relation())
132+
->setStartNode($nodeA)
133+
->setEndNode($nodeB)
134+
->setType('RELATION')
135+
->addProperties([
136+
'id' => 123,
137+
'hello' => 'world :D',
138+
])
139+
->addIdentifier('id');
140+
141+
$this->assertSame("(:NodeA {id: 123})-[:RELATION {hello: 'world :D', id: 123}]->(:NodeB {id: 321})", ToStringHelper::relationToString($relation));
142+
$this->assertSame("[:RELATION {hello: 'world :D', id: 123}]", ToStringHelper::relationToString($relation, withNodes: false));
143+
$this->assertSame("[:RELATION {id: 123}]", ToStringHelper::relationToString($relation, identifying: true, withNodes: false));
144+
}
105145
}

tests/Type/NodeTest.php

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -132,8 +132,7 @@ public function testToString(): void
132132
->addLabels(['LabelA', 'LabelZ', 'LabelC', 'discouraged Style'])
133133
->addProperty('id', 123)
134134
->addIdentifier('id');
135-
// todo fix backticks in ToCypherHelper class
136-
$this->assertSame("(:LabelA:LabelC:LabelZ:discouraged Style {id: '123'})", (string) $node);
135+
$this->assertSame("(:LabelA:LabelC:LabelZ:`discouraged Style` {id: 123})", (string) $node);
137136
}
138137

139138
public function testIsEqualTo(): void

tests/Type/RelationTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ public function testToString(): void
7272
(new Node())
7373
->addLabel('EndNode')
7474
);
75-
$this->assertSame("(:StartNode)-[:SOME_TYPE {hello: 'world', id: '123'}]->(:EndNode)", (string) $relation);
75+
$this->assertSame("(:StartNode)-[:SOME_TYPE {hello: 'world', id: 123}]->(:EndNode)", (string) $relation);
7676
}
7777

7878
public function testIsEqualTo(): void

0 commit comments

Comments
 (0)