From ae9276c7c28c29ca0b430e7e492c2d7417261299 Mon Sep 17 00:00:00 2001 From: Sergei Predvoditelev Date: Tue, 1 Apr 2025 11:36:33 +0300 Subject: [PATCH] Add `AbstractQueryDataReader` methods for query result reaching --- src/AbstractQueryDataReader.php | 31 ++++++++++++++++++++++++++++++- 1 file changed, 30 insertions(+), 1 deletion(-) diff --git a/src/AbstractQueryDataReader.php b/src/AbstractQueryDataReader.php index 16644f0..c9346df 100644 --- a/src/AbstractQueryDataReader.php +++ b/src/AbstractQueryDataReader.php @@ -74,6 +74,7 @@ public function getIterator(): Generator /** @var array|object $row */ foreach ($iterator as $index => $row) { + $this->reachRow($row); yield $index => $this->createItem($row); } } @@ -282,11 +283,15 @@ public function read(): array { if ($this->data === null) { $this->data = []; + + $rows = $this->getPreparedQuery()->all(); + $this->reachRows($rows); + /** * @psalm-var TKey $key * @psalm-var array $row */ - foreach ($this->getPreparedQuery()->all() as $key => $row) { + foreach ($rows as $key => $row) { $this->data[$key] = $this->createItem($row); } } @@ -317,6 +322,30 @@ public function readOne(): array|object|null */ abstract protected function createItem(array|object $row): array|object; + /** + * This method is called before processing the query results ({@see read()}). + * + * It allows modifying the array of rows before passing them to the {@see createItem()} method. + * + * @param array $rows An array of query result rows. + * + * @psalm-param array $rows + */ + protected function reachRows(array &$rows): void + { + } + + /** + * This method is called before processing each individual row of the query result ({@see getIterator()}). + * + * It allows modifying the row before passing it to the {@see createItem()} method. + * + * @param array|object $row A single row from the query result. + */ + protected function reachRow(array|object &$row): void + { + } + public function getFilter(): ?FilterInterface { return $this->filter;