Skip to content

Commit 7b1cbb6

Browse files
committed
summarized result iterates over its result when available
1 parent a4c5233 commit 7b1cbb6

File tree

1 file changed

+52
-4
lines changed

1 file changed

+52
-4
lines changed

src/Databags/SummarizedResult.php

Lines changed: 52 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,24 +13,31 @@
1313

1414
namespace Laudis\Neo4j\Databags;
1515

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;
1723

1824
/**
1925
* A result containing the values and the summary.
2026
*
2127
* @template T
2228
*
2329
* @psalm-immutable
30+
*
31+
* @implements ArrayAccess<string, ResultSummary|T>
2432
*/
25-
final class SummarizedResult
33+
final class SummarizedResult implements JsonSerializable, ArrayAccess, IteratorAggregate
2634
{
2735
private ResultSummary $summary;
2836
/** @var T */
2937
private $result;
3038

3139
/**
32-
* @param T $result
33-
* @param CypherList<string> $keys
40+
* @param T $result
3441
*/
3542
public function __construct($result, ResultSummary $summary)
3643
{
@@ -55,4 +62,45 @@ public function getSummary(): ResultSummary
5562
{
5663
return $this->summary;
5764
}
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+
}
58106
}

0 commit comments

Comments
 (0)