|
3 | 3 | namespace Forrest79\PhPgSql\Db; |
4 | 4 |
|
5 | 5 | /** |
6 | | - * @implements \ArrayAccess<string, mixed> |
7 | | - * @implements \IteratorAggregate<string, mixed> |
| 6 | + * @extends \ArrayAccess<string, mixed> |
| 7 | + * @extends \IteratorAggregate<string, mixed> |
8 | 8 | */ |
9 | | -class Row implements \ArrayAccess, \IteratorAggregate, \Countable, \JsonSerializable |
| 9 | +interface Row extends \ArrayAccess, \IteratorAggregate, \Countable, \JsonSerializable |
10 | 10 | { |
11 | | - private ColumnValueParser|null $columnValueParser; |
12 | 11 |
|
13 | | - /** @var array<string, string|null> */ |
14 | | - private array $rawValues; |
| 12 | + function __get(string $column): mixed; |
15 | 13 |
|
16 | | - /** @var array<string, mixed> */ |
17 | | - private array $values; |
18 | 14 |
|
19 | | - |
20 | | - /** |
21 | | - * @param array<string, string|null> $rawValues |
22 | | - */ |
23 | | - public function __construct(ColumnValueParser|null $columnValueParser, array $rawValues) |
24 | | - { |
25 | | - if ($columnValueParser === null && $rawValues !== []) { |
26 | | - throw Exceptions\RowException::columnValueParserMissing(); |
27 | | - } |
28 | | - |
29 | | - $this->columnValueParser = $columnValueParser; |
30 | | - $this->rawValues = $rawValues; |
31 | | - |
32 | | - $this->values = \array_fill_keys(\array_keys($rawValues), null); |
33 | | - } |
34 | | - |
35 | | - |
36 | | - /** |
37 | | - * @throws Exceptions\RowException |
38 | | - */ |
39 | | - public function __get(string $column): mixed |
40 | | - { |
41 | | - return $this->getValue($column); |
42 | | - } |
| 15 | + function __set(string $column, mixed $value): void; |
43 | 16 |
|
44 | 17 |
|
45 | | - public function __set(string $column, mixed $value): void |
46 | | - { |
47 | | - $this->setValue($column, $value); |
48 | | - } |
| 18 | + function __isset(string $column): bool; |
49 | 19 |
|
50 | 20 |
|
51 | | - public function __isset(string $column): bool |
52 | | - { |
53 | | - return $this->hasColumn($column) && ($this->getValue($column) !== null); |
54 | | - } |
55 | | - |
56 | | - |
57 | | - public function __unset(string $column): void |
58 | | - { |
59 | | - $this->removeValue($column); |
60 | | - } |
| 21 | + function __unset(string $column): void; |
61 | 22 |
|
62 | 23 |
|
63 | 24 | /** |
64 | 25 | * @return array<string, mixed> |
65 | 26 | */ |
66 | | - public function toArray(): array |
67 | | - { |
68 | | - // intentionally not using array_keys($this->rawValues) as $column - this is 2x faster |
69 | | - foreach ($this->rawValues as $column => $value) { |
70 | | - $this->parseValue($column); |
71 | | - } |
72 | | - |
73 | | - return $this->values; |
74 | | - } |
75 | | - |
76 | | - |
77 | | - public function count(): int |
78 | | - { |
79 | | - return \count($this->values); |
80 | | - } |
81 | | - |
82 | | - |
83 | | - /** |
84 | | - * @return \ArrayIterator<string, mixed> |
85 | | - */ |
86 | | - public function getIterator(): \ArrayIterator |
87 | | - { |
88 | | - return new \ArrayIterator($this->toArray()); |
89 | | - } |
90 | | - |
91 | | - |
92 | | - /** |
93 | | - * @param string $column |
94 | | - * @throws Exceptions\RowException |
95 | | - */ |
96 | | - #[\ReturnTypeWillChange] |
97 | | - public function offsetGet(mixed $column): mixed |
98 | | - { |
99 | | - if (!\is_string($column)) { |
100 | | - throw Exceptions\RowException::notStringKey(); |
101 | | - } |
102 | | - |
103 | | - return $this->getValue($column); |
104 | | - } |
105 | | - |
106 | | - |
107 | | - /** |
108 | | - * @param string|null $column |
109 | | - */ |
110 | | - public function offsetSet(mixed $column, mixed $value): void |
111 | | - { |
112 | | - if (!\is_string($column)) { |
113 | | - throw Exceptions\RowException::notStringKey(); |
114 | | - } |
115 | | - |
116 | | - $this->setValue($column, $value); |
117 | | - } |
118 | | - |
119 | | - |
120 | | - /** |
121 | | - * @param string $column |
122 | | - */ |
123 | | - public function offsetExists(mixed $column): bool |
124 | | - { |
125 | | - if (!\is_string($column)) { |
126 | | - throw Exceptions\RowException::notStringKey(); |
127 | | - } |
128 | | - |
129 | | - return $this->hasColumn($column) && ($this->getValue($column) !== null); |
130 | | - } |
131 | | - |
132 | | - |
133 | | - /** |
134 | | - * @param string $column |
135 | | - */ |
136 | | - public function offsetUnset(mixed $column): void |
137 | | - { |
138 | | - if (!\is_string($column)) { |
139 | | - throw Exceptions\RowException::notStringKey(); |
140 | | - } |
141 | | - |
142 | | - $this->removeValue($column); |
143 | | - } |
| 27 | + function toArray(): array; |
144 | 28 |
|
145 | 29 |
|
146 | 30 | /** |
147 | 31 | * @return list<string> |
148 | 32 | */ |
149 | | - public function getColumns(): array |
150 | | - { |
151 | | - return \array_keys($this->values); |
152 | | - } |
153 | | - |
154 | | - |
155 | | - public function hasColumn(string $column): bool |
156 | | - { |
157 | | - return \array_key_exists($column, $this->values); |
158 | | - } |
159 | | - |
160 | | - |
161 | | - /** |
162 | | - * @throws Exceptions\RowException |
163 | | - */ |
164 | | - private function getValue(string $column): mixed |
165 | | - { |
166 | | - if (!\array_key_exists($column, $this->values)) { |
167 | | - throw Exceptions\RowException::noColumn($column); |
168 | | - } |
169 | | - |
170 | | - if (\array_key_exists($column, $this->rawValues)) { |
171 | | - $this->parseValue($column); |
172 | | - } |
173 | | - |
174 | | - return $this->values[$column]; |
175 | | - } |
176 | | - |
177 | | - |
178 | | - /** |
179 | | - * @return array<string, mixed> |
180 | | - */ |
181 | | - public function __serialize(): array |
182 | | - { |
183 | | - return $this->toArray(); |
184 | | - } |
185 | | - |
186 | | - |
187 | | - /** |
188 | | - * @param array<string, mixed> $values |
189 | | - */ |
190 | | - public function __unserialize(array $values): void |
191 | | - { |
192 | | - $this->columnValueParser = null; |
193 | | - $this->rawValues = []; |
194 | | - $this->values = $values; |
195 | | - } |
196 | | - |
| 33 | + function getColumns(): array; |
197 | 34 |
|
198 | | - private function parseValue(string $column): void |
199 | | - { |
200 | | - assert($this->columnValueParser !== null); |
201 | | - $this->values[$column] = $this->columnValueParser->parseColumnValue($column, $this->rawValues[$column]); |
202 | | - unset($this->rawValues[$column]); |
203 | | - } |
204 | 35 |
|
205 | | - |
206 | | - private function setValue(string $column, mixed $value): void |
207 | | - { |
208 | | - $this->values[$column] = $value; |
209 | | - unset($this->rawValues[$column]); |
210 | | - } |
211 | | - |
212 | | - |
213 | | - private function removeValue(string $column): void |
214 | | - { |
215 | | - unset($this->rawValues[$column]); |
216 | | - unset($this->values[$column]); |
217 | | - } |
218 | | - |
219 | | - |
220 | | - /** |
221 | | - * @return array<string, mixed> |
222 | | - */ |
223 | | - public function jsonSerialize(): array |
224 | | - { |
225 | | - return $this->toArray(); |
226 | | - } |
| 36 | + function hasColumn(string $column): bool; |
227 | 37 |
|
228 | 38 |
|
229 | 39 | /** |
230 | 40 | * @param array<string, mixed> $values |
231 | 41 | */ |
232 | | - public static function from(array $values): static |
233 | | - { |
234 | | - $row = new static(null, []); |
235 | | - $row->values = $values; |
236 | | - return $row; |
237 | | - } |
| 42 | + static function from(array $values): static; |
238 | 43 |
|
239 | 44 | } |
0 commit comments