Skip to content

Commit df4cf8c

Browse files
committed
WIP?
1 parent 1074621 commit df4cf8c

6 files changed

Lines changed: 458 additions & 209 deletions

File tree

src/Db/ResultBuilder.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ public function setRowFactory(RowFactory $rowFactory): void
6969
private function getRowFactory(): RowFactory
7070
{
7171
if ($this->rowFactory === null) {
72-
$this->rowFactory = new RowFactories\Basic();
72+
$this->rowFactory = new RowFactories\Lazy();
7373
}
7474

7575
return $this->rowFactory;

src/Db/Row.php

Lines changed: 11 additions & 206 deletions
Original file line numberDiff line numberDiff line change
@@ -3,237 +3,42 @@
33
namespace Forrest79\PhPgSql\Db;
44

55
/**
6-
* @implements \ArrayAccess<string, mixed>
7-
* @implements \IteratorAggregate<string, mixed>
6+
* @extends \ArrayAccess<string, mixed>
7+
* @extends \IteratorAggregate<string, mixed>
88
*/
9-
class Row implements \ArrayAccess, \IteratorAggregate, \Countable, \JsonSerializable
9+
interface Row extends \ArrayAccess, \IteratorAggregate, \Countable, \JsonSerializable
1010
{
11-
private ColumnValueParser|null $columnValueParser;
1211

13-
/** @var array<string, string|null> */
14-
private array $rawValues;
12+
function __get(string $column): mixed;
1513

16-
/** @var array<string, mixed> */
17-
private array $values;
1814

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

4417

45-
public function __set(string $column, mixed $value): void
46-
{
47-
$this->setValue($column, $value);
48-
}
18+
function __isset(string $column): bool;
4919

5020

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

6223

6324
/**
6425
* @return array<string, mixed>
6526
*/
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;
14428

14529

14630
/**
14731
* @return list<string>
14832
*/
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;
19734

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-
}
20435

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

22838

22939
/**
23040
* @param array<string, mixed> $values
23141
*/
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;
23843

23944
}

src/Db/RowFactories/Full.php

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
<?php declare(strict_types=1);
2+
3+
namespace Forrest79\PhPgSql\Db\RowFactories;
4+
5+
use Forrest79\PhPgSql\Db;
6+
7+
class Full implements Db\RowFactory
8+
{
9+
10+
/**
11+
* @param array<string, string|null> $rawValues
12+
*/
13+
public function create(Db\ColumnValueParser $columnValueParser, array $rawValues): Db\Row
14+
{
15+
$values = [];
16+
foreach ($rawValues as $column => $rawValue) {
17+
$values[$column] = $columnValueParser->parseColumnValue($column, $rawValue);
18+
}
19+
20+
return new Db\Rows\Full($values);
21+
}
22+
23+
}
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,15 +4,15 @@
44

55
use Forrest79\PhPgSql\Db;
66

7-
class Basic implements Db\RowFactory
7+
class Lazy implements Db\RowFactory
88
{
99

1010
/**
1111
* @param array<string, string|null> $rawValues
1212
*/
1313
public function create(Db\ColumnValueParser $columnValueParser, array $rawValues): Db\Row
1414
{
15-
return new Db\Row($columnValueParser, $rawValues);
15+
return new Db\Rows\Lazy($columnValueParser, $rawValues);
1616
}
1717

1818
}

0 commit comments

Comments
 (0)