34
34
use Laudis \Neo4j \Types \Path ;
35
35
use Laudis \Neo4j \Types \Relationship ;
36
36
use Laudis \Neo4j \Types \Time ;
37
+ use Laudis \Neo4j \Types \UnboundRelationship ;
37
38
use Laudis \Neo4j \Types \WGS843DPoint ;
38
39
use Laudis \Neo4j \Types \WGS84Point ;
39
40
use RuntimeException ;
45
46
/**
46
47
* @psalm-immutable
47
48
*
48
- * @psalm-type RelationshipArray = array{id: string, type: string, startNode: string, endNode: string, properties: array<string, scalar|null|array<array-key, scalar|null|array>>}
49
- * @psalm-type NodeArray = array{id: string, labels: list<string>, properties: array<string, scalar|null|array}
50
- * @psalm-type Meta = array{id?: int, type: string, deleted?: bool}
51
- * @psalm-type MetaArray = list<Meta|null|list<array{id: int, type: string, deleted: bool}>>
52
- *
53
- * @psalm-type CypherResultDataRow = list<array{row: list<scalar|array|null>, meta: MetaArray, graph: array{nodes: list<NodeArray>, relationships: list<RelationshipArray>}}>
54
- * @psalm-type CypherResult = array{columns: list<string>, data: CypherResultDataRow}
55
- *
56
49
* @psalm-import-type OGMResults from \Laudis\Neo4j\Formatter\OGMFormatter
57
50
* @psalm-import-type OGMTypes from \Laudis\Neo4j\Formatter\OGMFormatter
58
51
*/
@@ -68,17 +61,28 @@ public function translateResult(stdClass $result): CypherList
68
61
/** @var list<CypherMap<OGMTypes>> $tbr */
69
62
$ tbr = [];
70
63
64
+ /** @var list<string> $columns */
71
65
$ columns = $ result ->columns ;
72
- foreach ($ result ->data as $ data ) {
66
+ /** @var list<stdClass> $datas */
67
+ $ datas = $ result ->data ;
68
+ foreach ($ datas as $ data ) {
73
69
$ meta = HttpMetaInfo::createFromData ($ data );
74
70
75
- $ row = array_combine ($ columns , (array ) $ data ->row );
71
+ /** @var list<stdClass> $row */
72
+ $ row = $ data ->row ;
73
+ /** @var array<string, stdClass> $row */
74
+ $ row = array_combine ($ columns , $ row );
76
75
$ tbr [] = $ this ->translateCypherMap ($ row , $ meta )[0 ];
77
76
}
78
77
79
78
return new CypherList ($ tbr );
80
79
}
81
80
81
+ /**
82
+ * @param array<string, stdClass> $row
83
+ *
84
+ * @return array{0: CypherMap<OGMTypes>, 1: HttpMetaInfo}
85
+ */
82
86
public function translateCypherMap (array $ row , HttpMetaInfo $ meta ): array
83
87
{
84
88
/** @var array<string, OGMTypes> $record */
@@ -93,9 +97,13 @@ public function translateCypherMap(array $row, HttpMetaInfo $meta): array
93
97
}
94
98
95
99
/**
96
- * @param mixed $value
100
+ * @param stdClass|array|scalar|null $value
97
101
*
98
102
* @return array{0: OGMTypes, 1: HttpMetaInfo}
103
+ *
104
+ * @psalm-suppress MixedArgumentTypeCoercion
105
+ * @psalm-suppress MixedArgument
106
+ * @psalm-suppress MixedAssignment
99
107
*/
100
108
private function translateValue ($ value , HttpMetaInfo $ meta ): array
101
109
{
@@ -110,7 +118,7 @@ private function translateValue($value, HttpMetaInfo $meta): array
110
118
*
111
119
* @see OGMFormatterIntegrationTest::testPathMultiple for an example
112
120
*/
113
- if (is_array ($ value [0 ])) {
121
+ if (array_key_exists ( 0 , $ value ) && is_array ($ value [0 ])) {
114
122
$ tbr = [];
115
123
foreach ($ value as $ path ) {
116
124
$ tbr [] = $ this ->path ($ path , $ meta ->withNestedMeta ());
@@ -120,7 +128,7 @@ private function translateValue($value, HttpMetaInfo $meta): array
120
128
return [new CypherList ($ tbr ), $ meta ];
121
129
}
122
130
123
- $ tbr = $ this ->path (( array ) $ value , $ meta ->withNestedMeta ());
131
+ $ tbr = $ this ->path ($ value , $ meta ->withNestedMeta ());
124
132
$ meta = $ meta ->incrementMeta ();
125
133
126
134
return [$ tbr , $ meta ];
@@ -138,12 +146,18 @@ private function translateValue($value, HttpMetaInfo $meta): array
138
146
139
147
/**
140
148
* @return array{0: Cartesian3DPoint|CartesianPoint|CypherList|CypherMap|Node|Relationship|WGS843DPoint|WGS84Point|Path, 1: HttpMetaInfo}
149
+ *
150
+ * @psalm-suppress MixedArgument
151
+ * @psalm-suppress MixedArgumentTypeCoercion
141
152
*/
142
- private function translateObject (object $ value , HttpMetaInfo $ meta ): array
153
+ private function translateObject (stdClass $ value , HttpMetaInfo $ meta ): array
143
154
{
144
155
$ type = $ meta ->getCurrentType ();
145
156
if ($ type === 'relationship ' ) {
146
- return $ this ->relationship ($ meta ->getCurrentRelationship (), $ meta );
157
+ /** @var stdClass $relationship */
158
+ $ relationship = $ meta ->getCurrentRelationship ();
159
+
160
+ return $ this ->relationship ($ relationship , $ meta );
147
161
}
148
162
149
163
if ($ type === 'point ' ) {
@@ -163,24 +177,34 @@ private function translateObject(object $value, HttpMetaInfo $meta): array
163
177
return $ this ->translateCypherMap ((array ) $ value , $ meta );
164
178
}
165
179
166
- private function translateProperties (stdClass $ properties ): CypherMap
180
+ /**
181
+ * @param array<string, array|stdClass|scalar|null> $properties
182
+ *
183
+ * @return CypherMap<OGMTypes>
184
+ */
185
+ private function translateProperties (array $ properties ): CypherMap
167
186
{
168
187
$ tbr = [];
169
- foreach (( array ) $ properties as $ key => $ value ) {
188
+ foreach ($ properties as $ key => $ value ) {
170
189
if ($ value instanceof stdClass) {
171
- $ tbr [$ key ] = new CypherMap ((array ) $ value );
190
+ /** @var array<string, array|stdClass|scalar|null> $castedValue */
191
+ $ castedValue = (array ) $ value ;
192
+ $ tbr [$ key ] = $ this ->translateProperties ($ castedValue );
172
193
} elseif (is_array ($ value )) {
173
- $ tbr [$ key ] = new CypherList ($ value );
194
+ /** @var array<string, array|stdClass|scalar|null> $value */
195
+ $ tbr [$ key ] = new CypherList ($ this ->translateProperties ($ value )->values ());
174
196
} else {
175
197
$ tbr [$ key ] = $ value ;
176
198
}
177
199
}
178
-
200
+ /** @var CypherMap<OGMTypes> */
179
201
return new CypherMap ($ tbr );
180
202
}
181
203
182
204
/**
183
- * @param RelationshipArray $relationship
205
+ * @psalm-suppress MixedArgument
206
+ *
207
+ * @return array{0: Relationship, 1: HttpMetaInfo}
184
208
*/
185
209
private function relationship (stdClass $ relationship , HttpMetaInfo $ meta ): array
186
210
{
@@ -216,82 +240,84 @@ private function translateCypherList(array $value, HttpMetaInfo $meta): array
216
240
}
217
241
218
242
/**
219
- * @param list<stdClass> $meta
220
- * @param list<NodeArray> $nodes
221
- * @param list<RelationshipArray> $relationship
243
+ * @param list<stdClass> $value
222
244
*/
223
245
private function path (array $ value , HttpMetaInfo $ meta ): Path
224
246
{
247
+ /** @var list<Node> $nodes */
225
248
$ nodes = [];
226
249
/** @var list<int> $ids */
227
250
$ ids = [];
251
+ /** @var list<UnboundRelationship> $rels */
228
252
$ rels = [];
229
253
230
254
foreach ($ value as $ x ) {
231
- $ ids [] = $ meta ->currentMeta ()->id ;
255
+ /** @var stdClass $currentMeta */
256
+ $ currentMeta = $ meta ->currentMeta ();
257
+ /** @var int $id */
258
+ $ id = $ currentMeta ->id ;
259
+ $ ids [] = $ id ;
232
260
[$ x , $ meta ] = $ this ->translateObject ($ x , $ meta );
233
261
if ($ x instanceof Node) {
234
262
$ nodes [] = $ x ;
235
- } else {
236
- $ rels [] = $ x ;
263
+ } elseif ( $ x instanceof Relationship) {
264
+ $ rels [] = new UnboundRelationship ( $ x -> getId (), $ x -> getType (), $ x -> getProperties ()) ;
237
265
}
238
266
}
239
267
240
268
return new Path (new CypherList ($ nodes ), new CypherList ($ rels ), new CypherList ($ ids ));
241
269
}
242
270
243
- /**
244
- * @param NodeArray $nodes
245
- */
246
- private function translateNode (array $ node ): Node
247
- {
248
- return new Node ($ node ['id ' ], new CypherList ($ node ['labels ' ]), $ this ->translateProperties ($ node ['properties ' ]));
249
- }
250
-
251
271
/**
252
272
* @return CartesianPoint|Cartesian3DPoint|WGS843DPoint|WGS84Point
253
273
*/
254
274
private function translatePoint (stdClass $ value ): PointInterface
255
275
{
256
- /** array{type: 'point', coordinates: array{0: float, 1: float, 2?:float}, crs: array{srid: int, type: string, name: 'cartesian'|'cartesian-3d'|'wgs-84'|'wgs-84-3d', properties: array<string, string>}} */
257
- $ pointType = $ value ->crs ->name ;
258
- if ($ pointType === 'cartesian ' ) {
276
+ /** @var stdClass $crs */
277
+ $ crs = $ value ->crs ;
278
+ /** @var "cartesian"|"cartesian-3d"|"wgs-84"|"wgs-84-3d" */
279
+ $ name = $ crs ->name ;
280
+ /** @var array{0: float, 1: float, 2:float} $coordinates */
281
+ $ coordinates = $ value ->coordinates ;
282
+ /** @var int $srid */
283
+ $ srid = $ crs ->srid ;
284
+ if ($ name === 'cartesian ' ) {
259
285
return new CartesianPoint (
260
- $ value -> coordinates [0 ],
261
- $ value -> coordinates [1 ],
262
- $ value -> crs -> name ,
263
- $ value -> crs -> srid
286
+ $ coordinates [0 ],
287
+ $ coordinates [1 ],
288
+ $ name ,
289
+ $ srid
264
290
);
265
291
}
266
- if ($ pointType === 'cartesian-3d ' ) {
292
+ if ($ name === 'cartesian-3d ' ) {
267
293
return new Cartesian3DPoint (
268
- $ value -> coordinates [0 ],
269
- $ value -> coordinates [1 ],
270
- $ value -> coordinates [2 ],
271
- $ value -> crs -> name ,
272
- $ value -> crs -> srid
294
+ $ coordinates [0 ],
295
+ $ coordinates [1 ],
296
+ $ coordinates [2 ],
297
+ $ name ,
298
+ $ srid
273
299
);
274
300
}
275
- if ($ pointType === 'wgs-84 ' ) {
301
+ if ($ name === 'wgs-84 ' ) {
276
302
return new WGS84Point (
277
- $ value -> coordinates [0 ],
278
- $ value -> coordinates [1 ],
279
- $ value -> coordinates [0 ],
280
- $ value -> coordinates [1 ],
281
- $ value -> crs -> name ,
282
- $ value -> crs -> srid
303
+ $ coordinates [0 ],
304
+ $ coordinates [1 ],
305
+ $ coordinates [0 ],
306
+ $ coordinates [1 ],
307
+ $ name ,
308
+ $ srid
283
309
);
284
310
}
285
311
286
312
return new WGS843DPoint (
287
- $ value -> coordinates [0 ],
288
- $ value -> coordinates [1 ],
289
- $ value -> coordinates [2 ],
290
- $ value -> coordinates [0 ],
291
- $ value -> coordinates [1 ],
292
- $ value -> coordinates [2 ],
293
- $ value -> crs -> name ,
294
- $ value -> crs -> srid
313
+ $ coordinates [0 ],
314
+ $ coordinates [1 ],
315
+ $ coordinates [2 ],
316
+ $ coordinates [0 ],
317
+ $ coordinates [1 ],
318
+ $ coordinates [2 ],
319
+ $ name ,
320
+ $ srid
295
321
);
296
322
}
297
323
0 commit comments