@@ -25,34 +25,56 @@ final class BoltCypherFormatter
25
25
*
26
26
* @return Vector<Map<string, scalar|array|null>>
27
27
*/
28
- public function formatResult (array $ meta , array $ results ): Vector
28
+ public function formatResult (array $ meta , iterable $ results ): Vector
29
29
{
30
30
$ results = array_slice ($ results , 0 , count ($ results ) - 1 );
31
31
32
32
$ tbr = new Vector ();
33
33
foreach ($ results as $ result ) {
34
- /** @var Map<string, scalar|array|null> $map */
35
- $ map = new Map ();
36
- foreach ($ meta ['fields ' ] as $ i => $ column ) {
37
- /** @var mixed $value */
38
- $ value = $ result [$ i ];
39
- if (is_object ($ value )) {
40
- $ map ->put ($ column , $ this ->objectToProperty ($ value ));
41
- } elseif ($ value === null || is_scalar ($ value )) {
42
- $ map ->put ($ column , $ value );
43
- } elseif (is_array ($ value )) {
44
- $ value = $ this ->remapObjectsInArray ($ value );
45
- $ map ->put ($ column , $ value );
46
- } else {
47
- throw new UnexpectedValueException ('Did not expect to receive value of type: ' .gettype ($ value ));
48
- }
49
- }
50
- $ tbr ->push ($ map );
34
+ $ tbr ->push ($ this ->formatRow ($ meta , $ result ));
51
35
}
52
36
53
37
return $ tbr ;
54
38
}
55
39
40
+ /**
41
+ * @param array{fields: array<int, string>} $meta
42
+ *
43
+ * @return Map<string, scalar|array|null>
44
+ */
45
+ private function formatRow (array $ meta , array $ result ): Map
46
+ {
47
+ /** @var Map<string, scalar|array|null> $map */
48
+ $ map = new Map ();
49
+ foreach ($ meta ['fields ' ] as $ i => $ column ) {
50
+ $ map ->put ($ column , $ this ->mapValue ($ result [$ i ]));
51
+ }
52
+
53
+ return $ map ;
54
+ }
55
+
56
+ /**
57
+ * @param mixed $value
58
+ *
59
+ * @return scalar|array|null
60
+ */
61
+ private function mapValue ($ value )
62
+ {
63
+ if (is_object ($ value )) {
64
+ return $ this ->objectToProperty ($ value );
65
+ }
66
+
67
+ if ($ value === null || is_scalar ($ value )) {
68
+ return $ value ;
69
+ }
70
+
71
+ if (is_array ($ value )) {
72
+ return $ this ->remapObjectsInArray ($ value );
73
+ }
74
+
75
+ throw new UnexpectedValueException ('Did not expect to receive value of type: ' .gettype ($ value ));
76
+ }
77
+
56
78
private function objectToProperty (object $ object ): array
57
79
{
58
80
if (!method_exists ($ object , 'properties ' )) {
0 commit comments