From 3f8573602efa5214d9e2984eac5dd00f62926eac Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=94=D0=BC=D0=B8=D1=82=D1=80=D0=B8=D0=B9?= Date: Thu, 23 Nov 2023 11:13:28 +0300 Subject: [PATCH] Adopt for last changes --- src/AbstractQueryDataReader.php | 24 +++++++++++++++++------- src/QueryDataReader.php | 2 +- tests/DataReaderTest.php | 13 +++++++++++++ tests/Support/CustomerDTO.php | 9 +++++++++ tests/Support/CustomerDataReader.php | 19 +++++++++++++++++++ 5 files changed, 59 insertions(+), 8 deletions(-) create mode 100644 tests/Support/CustomerDTO.php create mode 100644 tests/Support/CustomerDataReader.php diff --git a/src/AbstractQueryDataReader.php b/src/AbstractQueryDataReader.php index 021b4ac..1ae891c 100644 --- a/src/AbstractQueryDataReader.php +++ b/src/AbstractQueryDataReader.php @@ -30,9 +30,9 @@ use Yiisoft\Data\Reader\FilterInterface; use Yiisoft\Data\Reader\Sort; use Yiisoft\Db\Query\QueryInterface; - use function array_key_first; use function is_array; +use function is_object; use function sprintf; /** @@ -108,7 +108,7 @@ public function getIterator(): Generator $iterator = $this->getPreparedQuery()->each($this->batchSize); foreach ($iterator as $index => $row) { - yield $index => $row; + yield $index => $this->createItem($row); } } } @@ -158,8 +158,16 @@ public function getPreparedQuery(): QueryInterface return $query; } - protected function getHandlerByOperation(string $operation): QueryHandlerInterface + /** + * @param string|FilterInterface $operation + * @return QueryHandlerInterface + */ + public function getHandlerByOperation(string|FilterInterface $operation): QueryHandlerInterface { + if (is_object($operation)) { + $operation = $operation::getOperator(); + } + if (!isset($this->filterHandlers[$operation])) { throw new RuntimeException(sprintf('Operation "%s" is not supported', $operation)); } @@ -170,7 +178,7 @@ protected function getHandlerByOperation(string $operation): QueryHandlerInterfa protected function applyFilter(QueryInterface $query): QueryInterface { if ($this->filter !== null) { - $query = $this->getHandlerByOperation($this->filter::getOperator()) + $query = $this->getHandlerByOperation($this->filter) ->applyFilter($query, $this->filter); } @@ -180,7 +188,7 @@ protected function applyFilter(QueryInterface $query): QueryInterface protected function applyHaving(QueryInterface $query): QueryInterface { if ($this->having !== null) { - $query = $this->getHandlerByOperation($this->having::getOperator()) + $query = $this->getHandlerByOperation($this->having) ->applyHaving($query, $this->having); } @@ -366,11 +374,13 @@ public function readOne(): array|object|null return $key === null ? null : $this->data[$key]; } - return $this->withLimit(1)->getIterator()->current(); + $current = $this->withLimit(1)->getIterator()->current(); + + return $current === null ? null : $this->createItem($current); } /** * @psalm-return TValue */ - abstract protected function createItem(array $row): array|object; + abstract protected function createItem(array|object $row): array|object; } diff --git a/src/QueryDataReader.php b/src/QueryDataReader.php index 129b52d..fef90be 100644 --- a/src/QueryDataReader.php +++ b/src/QueryDataReader.php @@ -14,7 +14,7 @@ */ final class QueryDataReader extends AbstractQueryDataReader { - protected function createItem(array $row): array + protected function createItem(array|object $row): array|object { /** @psalm-var TValue */ return $row; diff --git a/tests/DataReaderTest.php b/tests/DataReaderTest.php index 7b0939c..7d26b28 100644 --- a/tests/DataReaderTest.php +++ b/tests/DataReaderTest.php @@ -137,4 +137,17 @@ public function testSort(Sort $sort, string $expected): void $dataReader->getPreparedQuery()->createCommand()->getRawSql() ); } + + /* + public function testCreateItem(): void + { + $db = $this->getConnection(true); + + $query = (new Query($db)) + ->from('customer'); + + $dataReader = new CustomerDataReader($query); + + $this->assertInstanceOf(CustomerDTO::class, $dataReader->readOne()); + } */ } diff --git a/tests/Support/CustomerDTO.php b/tests/Support/CustomerDTO.php new file mode 100644 index 0000000..057632d --- /dev/null +++ b/tests/Support/CustomerDTO.php @@ -0,0 +1,9 @@ +