Skip to content

Commit 878e18d

Browse files
committed
add node and relation variable to ToCypherHelper
1 parent c5d353c commit 878e18d

File tree

3 files changed

+26
-8
lines changed

3 files changed

+26
-8
lines changed

docs/helper.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,10 @@ $node->addIdentifier(new PropertyName('id'));
6565
echo(ToCypherHelper::nodeToCypherString($node));
6666
// > (:Label {id: '1234', someKey: 'some value'})
6767

68+
// print whole node with variable
69+
echo(ToCypherHelper::nodeToCypherString($node, nodeVariable: 'node'));
70+
// > (node:Label {id: '1234', someKey: 'some value'})
71+
6872
// print node with only identifying properties
6973
echo(ToCypherHelper::nodeToCypherString($node, true));
7074
// > (:Label {id: '1234'})
@@ -112,6 +116,10 @@ $relation->addIdentifier(new PropertyName('id'));
112116
echo(ToCypherHelper::relationToCypherString($relation));
113117
// > (:StartNode {id: '1234'})-[:TYPE {id: '123', key: 'value'}]->(:EndNode {id: 4321})
114118

119+
// print relation with all properties & nodes with variable
120+
echo(ToCypherHelper::relationToCypherString($relation, relationVariable: 'relation'));
121+
// > (:StartNode {id: '1234'})-[relation:TYPE {id: '123', key: 'value'}]->(:EndNode {id: 4321})
122+
115123
// print relation with identifying properties & nodes
116124
echo(ToCypherHelper::relationToCypherString($relation, true));
117125
// > (:StartNode {id: '1234'})-[:TYPE {id: '123'}]->(:EndNode {id: 4321})

src/Helper/ToCypherHelper.php

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,7 @@ public static function nodeLabelStorageToCypherLabelString(NodeLabelStorageInter
8888
* ones.
8989
* Return example: (:Label {property: 'value'}).
9090
*/
91-
public static function nodeToCypherString(?NodeInterface $node, bool $identifying = false): ?string
91+
public static function nodeToCypherString(?NodeInterface $node, bool $identifying = false, ?string $nodeVariable = null): ?string
9292
{
9393
if (null === $node) {
9494
return null;
@@ -106,16 +106,19 @@ public static function nodeToCypherString(?NodeInterface $node, bool $identifyin
106106
if ('' !== $propertyString) {
107107
$parts[] = '{'.$propertyString.'}';
108108
}
109+
if (!$nodeVariable) {
110+
$nodeVariable = '';
111+
}
109112

110-
return '('.implode(' ', $parts).')';
113+
return '('.$nodeVariable.implode(' ', $parts).')';
111114
}
112115

113-
public static function nodeToIdentifyingCypherString(?NodeInterface $node): ?string
116+
public static function nodeToIdentifyingCypherString(?NodeInterface $node, ?string $nodeVariable = null): ?string
114117
{
115-
return self::nodeToCypherString($node, true);
118+
return self::nodeToCypherString($node, true, $nodeVariable);
116119
}
117120

118-
public static function relationToCypherString(RelationInterface $relation, bool $identifying = false, bool $withNodes = true): string
121+
public static function relationToCypherString(RelationInterface $relation, bool $identifying = false, bool $withNodes = true, ?string $relationVariable = null): string
119122
{
120123
$parts = [];
121124
if ($withNodes) {
@@ -124,8 +127,11 @@ public static function relationToCypherString(RelationInterface $relation, bool
124127

125128
$relationParts = [];
126129
if ($relation->getRelationType()) {
130+
if (!$relationVariable) {
131+
$relationVariable = '';
132+
}
127133
/** @psalm-suppress PossiblyNullReference */
128-
$relationParts[] = ':'.$relation->getRelationType()->getRelationType();
134+
$relationParts[] = $relationVariable.':'.$relation->getRelationType()->getRelationType();
129135
}
130136
$propertyStorage = $relation->getProperties();
131137
if ($identifying) {
@@ -148,9 +154,9 @@ public static function relationToCypherString(RelationInterface $relation, bool
148154
return implode('', $parts);
149155
}
150156

151-
public static function relationToIdentifyingCypherString(RelationInterface $relation, bool $withNodes = true): string
157+
public static function relationToIdentifyingCypherString(RelationInterface $relation, bool $withNodes = true, ?string $relationVariable = null): string
152158
{
153-
return self::relationToCypherString($relation, true, $withNodes);
159+
return self::relationToCypherString($relation, true, $withNodes, $relationVariable);
154160
}
155161

156162
/**

tests/Helper/ToCypherHelperTest.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,8 @@ public function testNodeToCypherString(): void
100100
$node->addIdentifier(new PropertyName('propertyC'));
101101
$this->assertSame("(:SomeNode {propertyA: 'value A', propertyB: 'value B', propertyC: 'value C', propertyD: 'value D'})", ToCypherHelper::nodeToCypherString($node));
102102
$this->assertSame("(:SomeNode {propertyA: 'value A', propertyC: 'value C'})", ToCypherHelper::nodeToIdentifyingCypherString($node));
103+
$this->assertSame("(node:SomeNode {propertyA: 'value A', propertyB: 'value B', propertyC: 'value C', propertyD: 'value D'})", ToCypherHelper::nodeToCypherString($node, nodeVariable: 'node'));
104+
$this->assertSame("(node:SomeNode {propertyA: 'value A', propertyC: 'value C'})", ToCypherHelper::nodeToIdentifyingCypherString($node, nodeVariable: 'node'));
103105
$node->clearNodeLabels();
104106
$this->assertSame("({propertyA: 'value A', propertyB: 'value B', propertyC: 'value C', propertyD: 'value D'})", ToCypherHelper::nodeToCypherString($node));
105107
$this->assertSame("({propertyA: 'value A', propertyC: 'value C'})", ToCypherHelper::nodeToIdentifyingCypherString($node));
@@ -132,6 +134,8 @@ public function testRelationToCypherString(): void
132134

133135
$this->assertSame("(:StartNode {id: '1234'})-[:SOME_TYPE {id: '123', somethingElse: 'some non id value'}]->(:EndNode {id: '4321'})", ToCypherHelper::relationToCypherString($relation));
134136
$this->assertSame("[:SOME_TYPE {id: '123', somethingElse: 'some non id value'}]", ToCypherHelper::relationToCypherString($relation, withNodes: false));
137+
$this->assertSame("(:StartNode {id: '1234'})-[relation:SOME_TYPE {id: '123', somethingElse: 'some non id value'}]->(:EndNode {id: '4321'})", ToCypherHelper::relationToCypherString($relation, relationVariable: 'relation'));
138+
$this->assertSame("[relation:SOME_TYPE {id: '123', somethingElse: 'some non id value'}]", ToCypherHelper::relationToCypherString($relation, withNodes: false, relationVariable: 'relation'));
135139
$this->assertSame("(:StartNode {id: '1234'})-[:SOME_TYPE {id: '123'}]->(:EndNode {id: '4321'})", ToCypherHelper::relationToIdentifyingCypherString($relation));
136140
$this->assertSame("[:SOME_TYPE {id: '123'}]", ToCypherHelper::relationToIdentifyingCypherString($relation, false));
137141
$this->assertSame("[:SOME_TYPE {id: '123'}]", ToCypherHelper::relationToIdentifyingCypherString($relation, false));

0 commit comments

Comments
 (0)