Skip to content

Commit

Permalink
Adopt for last changes
Browse files Browse the repository at this point in the history
  • Loading branch information
Gerych1984 committed Nov 23, 2023
1 parent 3496ff7 commit 3f85736
Show file tree
Hide file tree
Showing 5 changed files with 59 additions and 8 deletions.
24 changes: 17 additions & 7 deletions src/AbstractQueryDataReader.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;

/**
Expand Down Expand Up @@ -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);

Check failure on line 111 in src/AbstractQueryDataReader.php

View workflow job for this annotation

GitHub Actions / psalm / PHP 8.0-ubuntu-latest

MixedArgument

src/AbstractQueryDataReader.php:111:51: MixedArgument: Argument 1 of Yiisoft\Data\Db\AbstractQueryDataReader::createItem cannot be mixed, expecting array<array-key, mixed>|object (see https://psalm.dev/030)

Check failure on line 111 in src/AbstractQueryDataReader.php

View workflow job for this annotation

GitHub Actions / psalm / PHP 8.1-ubuntu-latest

MixedArgument

src/AbstractQueryDataReader.php:111:51: MixedArgument: Argument 1 of Yiisoft\Data\Db\AbstractQueryDataReader::createItem cannot be mixed, expecting array<array-key, mixed>|object (see https://psalm.dev/030)

Check failure on line 111 in src/AbstractQueryDataReader.php

View workflow job for this annotation

GitHub Actions / psalm / PHP 8.2-ubuntu-latest

MixedArgument

src/AbstractQueryDataReader.php:111:51: MixedArgument: Argument 1 of Yiisoft\Data\Db\AbstractQueryDataReader::createItem cannot be mixed, expecting array<array-key, mixed>|object (see https://psalm.dev/030)

Check warning on line 111 in src/AbstractQueryDataReader.php

View check run for this annotation

Codecov / codecov/patch

src/AbstractQueryDataReader.php#L111

Added line #L111 was not covered by tests
}
}
}
Expand Down Expand Up @@ -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));
}
Expand All @@ -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);
}

Expand All @@ -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);
}

Expand Down Expand Up @@ -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();

Check warning on line 377 in src/AbstractQueryDataReader.php

View check run for this annotation

Codecov / codecov/patch

src/AbstractQueryDataReader.php#L377

Added line #L377 was not covered by tests

return $current === null ? null : $this->createItem($current);

Check warning on line 379 in src/AbstractQueryDataReader.php

View check run for this annotation

Codecov / codecov/patch

src/AbstractQueryDataReader.php#L379

Added line #L379 was not covered by tests
}

/**
* @psalm-return TValue
*/
abstract protected function createItem(array $row): array|object;
abstract protected function createItem(array|object $row): array|object;
}
2 changes: 1 addition & 1 deletion src/QueryDataReader.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
*/
final class QueryDataReader extends AbstractQueryDataReader
{
protected function createItem(array $row): array
protected function createItem(array|object $row): array|object

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;
Expand Down
13 changes: 13 additions & 0 deletions tests/DataReaderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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());
} */
}
9 changes: 9 additions & 0 deletions tests/Support/CustomerDTO.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<?php

declare(strict_types=1);

namespace Yiisoft\Data\Db\Tests\Support;

final class CustomerDTO
{
}
19 changes: 19 additions & 0 deletions tests/Support/CustomerDataReader.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<?php

declare(strict_types=1);

namespace Yiisoft\Data\Db\Tests\Support;

use Yiisoft\Data\Db\AbstractQueryDataReader;

final class CustomerDataReader extends AbstractQueryDataReader
{

/**
* @inheritDoc
*/
protected function createItem(object|array $row): array|object
{
return new CustomerDTO();
}
}

0 comments on commit 3f85736

Please sign in to comment.