Skip to content

Commit 540eede

Browse files
authored
Debug (#9)
* closes #6
1 parent 6a11cec commit 540eede

File tree

1 file changed

+40
-18
lines changed

1 file changed

+40
-18
lines changed

src/Formatter/BoltCypherFormatter.php

Lines changed: 40 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -25,34 +25,56 @@ final class BoltCypherFormatter
2525
*
2626
* @return Vector<Map<string, scalar|array|null>>
2727
*/
28-
public function formatResult(array $meta, array $results): Vector
28+
public function formatResult(array $meta, iterable $results): Vector
2929
{
3030
$results = array_slice($results, 0, count($results) - 1);
3131

3232
$tbr = new Vector();
3333
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));
5135
}
5236

5337
return $tbr;
5438
}
5539

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+
5678
private function objectToProperty(object $object): array
5779
{
5880
if (!method_exists($object, 'properties')) {

0 commit comments

Comments
 (0)