Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .github/workflows/composer-require-checker.yml
Original file line number Diff line number Diff line change
Expand Up @@ -31,4 +31,4 @@ jobs:
os: >-
['ubuntu-latest']
php: >-
['8.1', '8.2', '8.3']
['8.1', '8.2', '8.3', '8.4']
1 change: 1 addition & 0 deletions .github/workflows/mssql.yml
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ jobs:
- 8.1
- 8.2
- 8.3
- 8.4

mssql:
- server: 2022-latest
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/mutation.yml
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ jobs:
os: >-
['ubuntu-latest']
php: >-
['8.3']
['8.4']
min-covered-msi: 100
secrets:
STRYKER_DASHBOARD_API_KEY: ${{ secrets.STRYKER_DASHBOARD_API_KEY }}
1 change: 1 addition & 0 deletions .github/workflows/mysql.yml
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ jobs:
- 8.1
- 8.2
- 8.3
- 8.4

mysql:
- 5.7
Expand Down
1 change: 1 addition & 0 deletions .github/workflows/pgsql.yml
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ jobs:
- 8.1
- 8.2
- 8.3
- 8.4

pgsql:
- 9
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/rector.yml
Original file line number Diff line number Diff line change
Expand Up @@ -21,4 +21,4 @@ jobs:
os: >-
['ubuntu-latest']
php: >-
['8.3']
['8.4']
1 change: 1 addition & 0 deletions .github/workflows/sqlite.yml
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ jobs:
- 8.1
- 8.2
- 8.3
- 8.4

steps:
- name: Checkout.
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/static.yml
Original file line number Diff line number Diff line change
Expand Up @@ -29,4 +29,4 @@ jobs:
os: >-
['ubuntu-latest']
php: >-
['8.1', '8.2', '8.3']
['8.1', '8.2', '8.3', '8.4']
4 changes: 2 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@
"prefer-stable": true,
"minimum-stability": "dev",
"require": {
"php": "^8.1",
"php": "8.1 - 8.4",
"ext-mbstring": "*",
"cycle/database": "^2.11",
"cycle/orm": "^2.9",
Expand All @@ -44,7 +44,7 @@
"rector/rector": "^2.1.5",
"roave/infection-static-analysis-plugin": "^1.35",
"spatie/phpunit-watcher": "^1.24",
"vimeo/psalm": "^5.26",
"vimeo/psalm": "^5.26.1 || ^6.10.3",
"vlucas/phpdotenv": "^5.6"
},
"autoload": {
Expand Down
37 changes: 27 additions & 10 deletions src/Reader/EntityReader.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
use Yiisoft\Data\Cycle\Exception\NotSupportedFilterException;
use Yiisoft\Data\Cycle\Reader\FilterHandler\LikeHandler\LikeHandlerFactory;
use Yiisoft\Data\Reader\DataReaderInterface;
use Yiisoft\Data\Reader\Filter\All;
use Yiisoft\Data\Reader\FilterHandlerInterface;
use Yiisoft\Data\Reader\FilterInterface;
use Yiisoft\Data\Reader\Sort;
Expand All @@ -34,7 +35,7 @@ final class EntityReader implements DataReaderInterface
private ?int $limit = null;
private int $offset = 0;
private ?Sort $sorting = null;
private ?FilterInterface $filter = null;
private FilterInterface $filter;
private CachedCount $countCache;
private CachedCollection $itemsCache;
private CachedCollection $oneItemCache;
Expand All @@ -56,7 +57,9 @@ public function __construct(Select|SelectQuery $query)
$likeHandler = LikeHandlerFactory::getLikeHandler($this->query->getDriver()?->getType() ?? 'SQLite');
$this->setFilterHandlers(
new FilterHandler\AllHandler(),
new FilterHandler\AnyHandler(),
new FilterHandler\NoneHandler(),
new FilterHandler\AndXHandler(),
new FilterHandler\OrXHandler(),
new FilterHandler\BetweenHandler(),
new FilterHandler\EqualsHandler(),
new FilterHandler\EqualsNullHandler(),
Expand All @@ -68,8 +71,11 @@ public function __construct(Select|SelectQuery $query)
$likeHandler,
new FilterHandler\NotHandler(),
);

$this->filter = new All();
}

#[\Override]
public function getSort(): ?Sort
{
return $this->sorting;
Expand All @@ -78,6 +84,7 @@ public function getSort(): ?Sort
/**
* @psalm-mutation-free
*/
#[\Override]
public function withLimit(?int $limit): static
{
/** @psalm-suppress DocblockTypeContradiction */
Expand All @@ -95,6 +102,7 @@ public function withLimit(?int $limit): static
/**
* @psalm-mutation-free
*/
#[\Override]
public function withOffset(int $offset): static
{
$new = clone $this;
Expand All @@ -108,6 +116,7 @@ public function withOffset(int $offset): static
/**
* @psalm-mutation-free
*/
#[\Override]
public function withSort(?Sort $sort): static
{
$new = clone $this;
Expand All @@ -122,7 +131,8 @@ public function withSort(?Sort $sort): static
/**
* @psalm-mutation-free
*/
public function withFilter(?FilterInterface $filter): static
#[\Override]
public function withFilter(FilterInterface $filter): static
{
$new = clone $this;
if ($new->filter !== $filter) {
Expand All @@ -138,6 +148,7 @@ public function withFilter(?FilterInterface $filter): static
/**
* @psalm-mutation-free
*/
#[\Override]
public function withAddedFilterHandlers(FilterHandlerInterface ...$filterHandlers): static
{
$new = clone $this;
Expand All @@ -150,11 +161,13 @@ public function withAddedFilterHandlers(FilterHandlerInterface ...$filterHandler
return $new;
}

#[\Override]
public function count(): int
{
return $this->countCache->getCount();
}

#[\Override]
public function read(): iterable
{
if ($this->itemsCache->getCollection() === null) {
Expand All @@ -164,6 +177,7 @@ public function read(): iterable
return $this->itemsCache->getCollection();
}

#[\Override]
public function readOne(): null|array|object
{
if (!$this->oneItemCache->isCollected()) {
Expand All @@ -181,6 +195,7 @@ public function readOne(): null|array|object
/**
* Get Iterator without caching
*/
#[\Override]
public function getIterator(): Generator
{
yield from $this->itemsCache->getCollection() ?? $this->buildSelectQuery()->getIterator();
Expand Down Expand Up @@ -215,9 +230,9 @@ private function buildSelectQuery(): SelectQuery|Select
if ($this->limit !== null) {
$newQuery->limit($this->limit);
}
if ($this->filter !== null) {
$newQuery->andWhere($this->makeFilterClosure($this->filter));
}

$newQuery->andWhere($this->makeFilterClosure($this->filter));

return $newQuery;
}

Expand All @@ -235,9 +250,8 @@ private function makeFilterClosure(FilterInterface $filter): Closure
private function resetCountCache(): void
{
$newQuery = clone $this->query;
if ($this->filter !== null) {
$newQuery->andWhere($this->makeFilterClosure($this->filter));
}
$newQuery->andWhere($this->makeFilterClosure($this->filter));

$this->countCache = new CachedCount($newQuery);
}

Expand All @@ -256,16 +270,19 @@ private function normalizeSortingCriteria(array $criteria): array
return $criteria;
}

public function getFilter(): ?FilterInterface
#[\Override]
public function getFilter(): FilterInterface
{
return $this->filter;
}

#[\Override]
public function getLimit(): ?int
{
return $this->limit;
}

#[\Override]
public function getOffset(): int
{
return $this->offset;
Expand Down
15 changes: 5 additions & 10 deletions src/Reader/FilterHandler/AllHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,33 +4,28 @@

namespace Yiisoft\Data\Cycle\Reader\FilterHandler;

use Cycle\Database\Injection\Expression;
use Cycle\ORM\Select\QueryBuilder;
use Yiisoft\Data\Cycle\Exception\NotSupportedFilterException;
use Yiisoft\Data\Cycle\Reader\QueryBuilderFilterHandler;
use Yiisoft\Data\Reader\Filter\All;
use Yiisoft\Data\Reader\FilterHandlerInterface;
use Yiisoft\Data\Reader\FilterInterface;

final class AllHandler implements QueryBuilderFilterHandler, FilterHandlerInterface
{
#[\Override]
public function getFilterClass(): string
{
return All::class;
}

#[\Override]
public function getAsWhereArguments(FilterInterface $filter, array $handlers): array
{
/** @var All $filter */

return [
static function (QueryBuilder $select) use ($filter, $handlers) {
foreach ($filter->getFilters() as $subFilter) {
$handler = $handlers[$subFilter::class] ?? null;
if ($handler === null) {
throw new NotSupportedFilterException($subFilter::class);
}
$select->andWhere(...$handler->getAsWhereArguments($subFilter, $handlers));
}
static function (QueryBuilder $select) {
$select->where(new Expression('1 = 1'));
},
];
}
Expand Down
38 changes: 38 additions & 0 deletions src/Reader/FilterHandler/AndXHandler.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
<?php

declare(strict_types=1);

namespace Yiisoft\Data\Cycle\Reader\FilterHandler;

use Cycle\ORM\Select\QueryBuilder;
use Yiisoft\Data\Cycle\Exception\NotSupportedFilterException;
use Yiisoft\Data\Cycle\Reader\QueryBuilderFilterHandler;
use Yiisoft\Data\Reader\Filter\AndX;
use Yiisoft\Data\Reader\FilterHandlerInterface;
use Yiisoft\Data\Reader\FilterInterface;

final class AndXHandler implements QueryBuilderFilterHandler, FilterHandlerInterface
{
#[\Override]
public function getFilterClass(): string
{
return AndX::class;
}

#[\Override]
public function getAsWhereArguments(FilterInterface $filter, array $handlers): array
{
/** @var AndX $filter */
return [
static function (QueryBuilder $select) use ($filter, $handlers) {
foreach ($filter->filters as $subFilter) {
$handler = $handlers[$subFilter::class] ?? null;
if ($handler === null) {
throw new NotSupportedFilterException($subFilter::class);
}
$select->andWhere(...$handler->getAsWhereArguments($subFilter, $handlers));
}
},
];
}
}
4 changes: 3 additions & 1 deletion src/Reader/FilterHandler/BetweenHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,15 +11,17 @@

final class BetweenHandler implements QueryBuilderFilterHandler, FilterHandlerInterface
{
#[\Override]
public function getFilterClass(): string
{
return Between::class;
}

#[\Override]
public function getAsWhereArguments(FilterInterface $filter, array $handlers): array
{
/** @var Between $filter */

return [$filter->getField(), 'between', $filter->getMinValue(), $filter->getMaxValue()];
return [$filter->field, 'between', $filter->minValue, $filter->maxValue];
}
}
4 changes: 3 additions & 1 deletion src/Reader/FilterHandler/EqualsHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,15 +11,17 @@

final class EqualsHandler implements QueryBuilderFilterHandler, FilterHandlerInterface
{
#[\Override]
public function getFilterClass(): string
{
return Equals::class;
}

#[\Override]
public function getAsWhereArguments(FilterInterface $filter, array $handlers): array
{
/** @var Equals $filter */

return [$filter->getField(), '=', $filter->getValue()];
return [$filter->field, '=', $filter->value];
}
}
4 changes: 3 additions & 1 deletion src/Reader/FilterHandler/EqualsNullHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,15 +11,17 @@

final class EqualsNullHandler implements QueryBuilderFilterHandler, FilterHandlerInterface
{
#[\Override]
public function getFilterClass(): string
{
return EqualsNull::class;
}

#[\Override]
public function getAsWhereArguments(FilterInterface $filter, array $handlers): array
{
/** @var EqualsNull $filter */

return [$filter->getField(), '=', null];
return [$filter->field, '=', null];
}
}
4 changes: 3 additions & 1 deletion src/Reader/FilterHandler/GreaterThanHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,15 +11,17 @@

final class GreaterThanHandler implements QueryBuilderFilterHandler, FilterHandlerInterface
{
#[\Override]
public function getFilterClass(): string
{
return GreaterThan::class;
}

#[\Override]
public function getAsWhereArguments(FilterInterface $filter, array $handlers): array
{
/** @var GreaterThan $filter */

return [$filter->getField(), '>', $filter->getValue()];
return [$filter->field, '>', $filter->value];
}
}
Loading
Loading