25
25
use Laudis \Neo4j \Types \UnboundRelationship ;
26
26
use Laudis \Neo4j \Types \WGS843DPoint ;
27
27
use Laudis \Neo4j \Types \WGS84Point ;
28
+ use RuntimeException ;
28
29
29
30
/**
30
31
* Maps the arrays to their respective values.
31
32
*
32
- * @psalm-type RelationshipArray = array{id: string, type: string, startNode: string, endNode: string, properties? : array<string, scalar|null|array<array-key, scalar|null|array>>}
33
+ * @psalm-type RelationshipArray = array{id: string, type: string, startNode: string, endNode: string, properties: array<string, scalar|null|array<array-key, scalar|null|array>>}
33
34
* @psalm-type NodeArray = array{id: string, labels: list<string>, properties: array<string, scalar|null|array}
34
- * @psalm-type MetaArray = null|array{id?: int, type: string, deleted?: bool}
35
+ * @psalm-type Meta = array{id?: int, type: string, deleted?: bool}
36
+ * @psalm-type MetaArray = list<Meta|null|list<array{id: int, type: string, deleted: bool}>>
35
37
*
36
38
* @psalm-import-type OGMTypes from \Laudis\Neo4j\Formatter\OGMFormatter
37
39
*
@@ -94,10 +96,10 @@ private function translateCypherList(array $value): CypherList
94
96
95
97
/**
96
98
* @param list<RelationshipArray> $relationships
97
- * @param list< MetaArray|null> $meta
99
+ * @param MetaArray $meta
98
100
* @param list<NodeArray> $nodes
99
101
*
100
- * @return array{0: int, 1: int, 2:Cartesian3DPoint|CartesianPoint|CypherList|CypherMap|Node|Relationship|WGS843DPoint|WGS84Point}
102
+ * @return array{0: int, 1: int, 2:Cartesian3DPoint|CartesianPoint|CypherList|CypherMap|Node|Relationship|WGS843DPoint|WGS84Point|Path }
101
103
*/
102
104
public function translate (array $ meta , array $ relationships , int $ metaIndex , int $ relationshipIndex , array $ nodes , array $ value ): array
103
105
{
@@ -112,6 +114,11 @@ public function translate(array $meta, array $relationships, int $metaIndex, int
112
114
++$ relationshipIncrease ;
113
115
break ;
114
116
case 'path ' :
117
+ /**
118
+ * @psalm-suppress UnnecessaryVarAnnotation False positive
119
+ *
120
+ * @var list<array{id: int, type: string, deleted: bool}> $currentMeta
121
+ */
115
122
[$ path , $ relIncrease ] = $ this ->path ($ currentMeta , $ nodes , $ relationships , $ relationshipIndex );
116
123
$ relationshipIncrease += $ relIncrease ;
117
124
$ tbr = $ path ;
@@ -120,20 +127,31 @@ public function translate(array $meta, array $relationships, int $metaIndex, int
120
127
$ tbr = $ this ->translatePoint ($ value );
121
128
break ;
122
129
default :
123
- /** @var array<array-key, array|scalar|null> $value */
124
- $ tbr = $ this ->translateContainer ($ value );
125
130
if ($ type === 'node ' && isset ($ currentMeta ['id ' ])) {
126
- $ tbr = $ this ->translateNode ($ nodes , $ currentMeta ['id ' ]);
131
+ /** @var int $id */
132
+ $ id = $ currentMeta ['id ' ];
133
+ $ tbr = $ this ->translateNode ($ nodes , $ id );
134
+ } else {
135
+ /** @var array<array-key, array|scalar|null> $value */
136
+ $ tbr = $ this ->translateContainer ($ value );
127
137
}
128
138
break ;
129
139
}
130
140
131
141
return [$ metaIncrease , $ relationshipIncrease , $ tbr ];
132
142
}
133
143
144
+ /**
145
+ * @param list<array{id: int, type: string, deleted: bool}> $meta
146
+ * @param list<NodeArray> $nodes
147
+ * @param list<RelationshipArray> $relationships
148
+ *
149
+ * @return array{0: Path, 1: int}
150
+ */
134
151
private function path (array $ meta , array $ nodes , array $ relationships , int $ relIndex ): array
135
152
{
136
153
$ nodesTbr = [];
154
+ /** @var list<int> $ids */
137
155
$ ids = [];
138
156
$ rels = [];
139
157
$ relIncrease = 0 ;
@@ -142,7 +160,8 @@ private function path(array $meta, array $nodes, array $relationships, int $relI
142
160
$ rel = $ relationships [$ relIndex ];
143
161
++$ relIndex ;
144
162
++$ relIncrease ;
145
- $ rels [] = new UnboundRelationship ((int ) $ rel ['id ' ], $ rel ['type ' ], new CypherMap ($ rel ['properties ' ]));
163
+ $ props = $ this ->translateCypherMap ($ rel ['properties ' ]);
164
+ $ rels [] = new UnboundRelationship ((int ) $ rel ['id ' ], $ rel ['type ' ], $ props );
146
165
} else {
147
166
$ nodesTbr [] = $ this ->translateNode ($ nodes , $ nodeOrRel ['id ' ]);
148
167
}
@@ -157,18 +176,13 @@ private function path(array $meta, array $nodes, array $relationships, int $relI
157
176
*/
158
177
private function translateNode (array $ nodes , int $ id ): Node
159
178
{
160
- /** @var list<string> */
161
- $ labels = [];
162
- $ props = [];
163
179
foreach ($ nodes as $ node ) {
164
180
if ((int ) $ node ['id ' ] === $ id ) {
165
- $ labels = $ node ['labels ' ];
166
- $ props = $ node ['properties ' ];
167
- break ;
181
+ return new Node ($ id , new CypherList ($ node ['labels ' ]), $ this ->translateCypherMap ($ node ['properties ' ]));
168
182
}
169
183
}
170
184
171
- return new Node ( $ id , new CypherList ( $ labels ), new CypherMap ( $ props ) );
185
+ throw new RuntimeException ( ' Error when translating node: Cannot find node with id: ' . $ id );
172
186
}
173
187
174
188
/**
0 commit comments