Skip to content

Commit

Permalink
Allow to create custom items
Browse files Browse the repository at this point in the history
  • Loading branch information
vjik committed Nov 21, 2023
1 parent b0052d1 commit 1c02b8c
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 3 deletions.
15 changes: 13 additions & 2 deletions src/AbstractQueryDataReader.php
Original file line number Diff line number Diff line change
Expand Up @@ -338,8 +338,14 @@ public function getSort(): ?Sort
public function read(): array
{
if ($this->data === null) {
/** @psalm-var array<TKey, TValue> */
$this->data = $this->getPreparedQuery()->all();
$this->data = [];

Check warning on line 341 in src/AbstractQueryDataReader.php

View check run for this annotation

Codecov / codecov/patch

src/AbstractQueryDataReader.php#L341

Added line #L341 was not covered by tests
/**
* @psalm-var TKey $key
* @psalm-var array $row
*/
foreach ($this->getPreparedQuery()->all() as $key => $row) {
$this->data[$key] = $this->createItem($row);

Check warning on line 347 in src/AbstractQueryDataReader.php

View check run for this annotation

Codecov / codecov/patch

src/AbstractQueryDataReader.php#L346-L347

Added lines #L346 - L347 were not covered by tests
}
}

return $this->data;
Expand All @@ -362,4 +368,9 @@ public function readOne(): array|object|null

return $this->withLimit(1)->getIterator()->current();
}

/**
* @psalm-return TValue
*/
abstract protected function createItem(array $row): array|object;
}
7 changes: 6 additions & 1 deletion src/QueryDataReader.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,15 @@
* Base class for `QueryDataReaderInterface`
*
* @template TKey as array-key
* @template TValue as array|object
* @template TValue as array
*
* @extends AbstractQueryDataReader<TKey, TValue>
*/
final class QueryDataReader extends AbstractQueryDataReader
{
protected function createItem(array $row): array

Check warning on line 17 in src/QueryDataReader.php

View check run for this annotation

Codecov / codecov/patch

src/QueryDataReader.php#L17

Added line #L17 was not covered by tests
{
/** @psalm-var TValue */
return $row;

Check warning on line 20 in src/QueryDataReader.php

View check run for this annotation

Codecov / codecov/patch

src/QueryDataReader.php#L20

Added line #L20 was not covered by tests
}
}

0 comments on commit 1c02b8c

Please sign in to comment.