Repository navigation
Expand file tree
/
Copy pathHydratingResultSet.php
More file actions
100 lines (85 loc) · 2.57 KB
/
Copy pathHydratingResultSet.php
File metadata and controls
100 lines (85 loc) · 2.57 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
<?php
declare(strict_types=1);
namespace PhpDb\ResultSet;
use ArrayObject;
use Laminas\Hydrator\ArraySerializableHydrator;
use Laminas\Hydrator\HydratorInterface;
use Override;
use function is_array;
class HydratingResultSet extends AbstractResultSet implements HydratingResultSetInterface
{
public function __construct(
private ?HydratorInterface $hydrator = null,
private ?object $rowPrototype = null,
) {}
/**
* Iterator: get current item
*/
#[Override]
public function current(): ?object
{
if ($this->buffer === null) {
$this->buffer = -2; // implicitly disable buffering from here on
} elseif (is_array($this->buffer) && isset($this->buffer[$this->position])) {
return $this->buffer[$this->position];
}
$data = $this->dataSource->current();
$current = is_array($data) ? $this->getHydrator()->hydrate($data, clone $this->getRowPrototype()) : null;
if (is_array($this->buffer)) {
$this->buffer[$this->position] = $current;
}
return $current;
}
/**
* Get the hydrator to use for each row object
*/
public function getHydrator(): HydratorInterface
{
return $this->hydrator ??= new ArraySerializableHydrator();
}
/** @deprecated use getRowPrototype() */
public function getObjectPrototype(): ?object
{
return $this->getRowPrototype();
}
/** {@inheritDoc} */
#[Override]
public function getRowPrototype(): object
{
return $this->rowPrototype ??= new ArrayObject();
}
/**
* Set the hydrator to use for each row object
*/
public function setHydrator(HydratorInterface $hydrator): ResultSetInterface
{
$this->hydrator = $hydrator;
return $this;
}
/** @deprecated use setRowPrototype() */
public function setObjectPrototype(object $objectPrototype): ResultSetInterface
{
return $this->setRowPrototype($objectPrototype);
}
/** {@inheritDoc} */
#[Override]
public function setRowPrototype(object $rowPrototype): ResultSetInterface&HydratingResultSetInterface
{
$this->rowPrototype = $rowPrototype;
return $this;
}
/**
* Cast result set to array of arrays
*
* @throws Exception\RuntimeException If any row is not castable to an array.
*/
#[Override]
public function toArray(): array
{
$return = [];
foreach ($this as $row) {
$return[] = $this->getHydrator()->extract($row);
}
return $return;
}
}