13
13
14
14
namespace Laudis \Neo4j \Databags ;
15
15
16
- use Laudis \Neo4j \Types \CypherList ;
16
+ use ArrayAccess ;
17
+ use BadMethodCallException ;
18
+ use Generator ;
19
+ use function is_iterable ;
20
+ use IteratorAggregate ;
21
+ use JsonSerializable ;
22
+ use function sprintf ;
17
23
18
24
/**
19
25
* A result containing the values and the summary.
20
26
*
21
27
* @template T
22
28
*
23
29
* @psalm-immutable
30
+ *
31
+ * @implements ArrayAccess<string, ResultSummary|T>
24
32
*/
25
- final class SummarizedResult
33
+ final class SummarizedResult implements JsonSerializable, ArrayAccess, IteratorAggregate
26
34
{
27
35
private ResultSummary $ summary ;
28
36
/** @var T */
29
37
private $ result ;
30
38
31
39
/**
32
- * @param T $result
33
- * @param CypherList<string> $keys
40
+ * @param T $result
34
41
*/
35
42
public function __construct ($ result , ResultSummary $ summary )
36
43
{
@@ -55,4 +62,45 @@ public function getSummary(): ResultSummary
55
62
{
56
63
return $ this ->summary ;
57
64
}
65
+
66
+ public function getIterator (): Generator
67
+ {
68
+ if (is_iterable ($ this ->result )) {
69
+ yield from $ this ->result ;
70
+ } else {
71
+ yield 'summary ' => $ this ->summary ;
72
+ yield 'result ' => $ this ->result ;
73
+ }
74
+ }
75
+
76
+ /**
77
+ * @return array{summary: ResultSummary, result: T}
78
+ */
79
+ public function jsonSerialize (): array
80
+ {
81
+ return [
82
+ 'summary ' => $ this ->summary ,
83
+ 'result ' => $ this ->result ,
84
+ ];
85
+ }
86
+
87
+ public function offsetExists ($ offset )
88
+ {
89
+ return array_key_exists ($ offset , $ this ->jsonSerialize ());
90
+ }
91
+
92
+ public function offsetGet ($ offset )
93
+ {
94
+ return $ this ->jsonSerialize ()[$ offset ];
95
+ }
96
+
97
+ public function offsetSet ($ offset , $ value )
98
+ {
99
+ throw new BadMethodCallException (sprintf ('%s is immutable ' , __CLASS__ ));
100
+ }
101
+
102
+ public function offsetUnset ($ offset )
103
+ {
104
+ throw new BadMethodCallException (sprintf ('%s is immutable ' , __CLASS__ ));
105
+ }
58
106
}
0 commit comments