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
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@
- New #213: Add `nextPage()` and `previousPage()` methods to `PaginatorInterface` (@samdark)
- New #200: Add matching mode parameter to `Like` filter (@samdark, @vjik)
- New #232: Add `All` and `None` filters (@vjik)
- Chg #233: Remove nullable types from `withFilter()` and `getFilter()` methods of `FilterableDataInterface` (@vjik)

## 1.0.1 January 25, 2023

Expand Down
4 changes: 2 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,8 @@
},
"require-dev": {
"maglnet/composer-require-checker": "^4.7.1",
"phpunit/phpunit": "^10.5.48",
"rector/rector": "^2.1.2",
"phpunit/phpunit": "^10.5.52",
"rector/rector": "^2.1.4",
"roave/infection-static-analysis-plugin": "^1.35",
"spatie/phpunit-watcher": "^1.24",
"vimeo/psalm": "^5.26.1 || ^6.10.3"
Expand Down
10 changes: 4 additions & 6 deletions src/Reader/FilterableDataInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,27 +25,25 @@ interface FilterableDataInterface extends ReadableDataInterface
/**
* Returns new instance with data reading criteria set.
*
* @param ?FilterInterface $filter Data reading criteria.
* @param FilterInterface $filter Data reading criteria.
*
* @return static New instance.
* @psalm-return $this
*/
public function withFilter(?FilterInterface $filter): static;
public function withFilter(FilterInterface $filter): static;

/**
* Get current data reading criteria.
*
* @return FilterInterface|null Data reading criteria.
* @return FilterInterface Data reading criteria.
*/
public function getFilter(): ?FilterInterface;
public function getFilter(): FilterInterface;

/**
* Returns new instance with additional handlers set.
*
* @param FilterHandlerInterface ...$filterHandlers Additional filter handlers.
*
* @return static New instance.
* @psalm-return $this
*/
public function withAddedFilterHandlers(FilterHandlerInterface ...$filterHandlers): static;
}
15 changes: 7 additions & 8 deletions src/Reader/Iterable/IterableDataReader.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
use Yiisoft\Arrays\ArrayHelper;
use Yiisoft\Data\Reader\DataReaderException;
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\Iterable\FilterHandler\AllHandler;
Expand Down Expand Up @@ -53,7 +54,7 @@
final class IterableDataReader implements DataReaderInterface
{
private ?Sort $sort = null;
private ?FilterInterface $filter = null;
private FilterInterface $filter;

/**
* @psalm-var non-negative-int|null
Expand Down Expand Up @@ -93,6 +94,7 @@ public function __construct(
new NotHandler(),
]);
$this->context = new Context($this->coreFilterHandlers, $this->valueReader);
$this->filter = new All();
}

/**
Expand All @@ -111,10 +113,7 @@ public function withAddedFilterHandlers(FilterHandlerInterface ...$filterHandler
return $new;
}

/**
* @psalm-return $this
*/
public function withFilter(?FilterInterface $filter): static
public function withFilter(FilterInterface $filter): static
{
$new = clone $this;
$new->filter = $filter;
Expand Down Expand Up @@ -216,8 +215,8 @@ private function internalRead(bool $useLimitAndOffset): array
continue;
}

// Filter items.
if ($this->filter === null || $this->matchFilter($item, $this->filter)) {
// Filter items
if ($this->matchFilter($item, $this->filter)) {
$data[$key] = $item;
}
}
Expand Down Expand Up @@ -320,7 +319,7 @@ private function iterableToArray(iterable $iterable): array
return $iterable instanceof Traversable ? iterator_to_array($iterable) : $iterable;
}

public function getFilter(): ?FilterInterface
public function getFilter(): FilterInterface
{
return $this->filter;
}
Expand Down
9 changes: 5 additions & 4 deletions tests/Paginator/KeysetPaginatorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
use Yiisoft\Data\Paginator\KeysetFilterContext;
use Yiisoft\Data\Paginator\KeysetPaginator;
use Yiisoft\Data\Paginator\PageToken;
use Yiisoft\Data\Reader\Filter\All;
use Yiisoft\Data\Reader\Filter\Equals;
use Yiisoft\Data\Reader\Filter\GreaterThan;
use Yiisoft\Data\Reader\Filter\GreaterThanOrEqual;
Expand Down Expand Up @@ -130,9 +131,9 @@ public function withAddedFilterHandlers(FilterHandlerInterface ...$filterHandler
return clone $this;
}

public function getFilter(): ?FilterInterface
public function getFilter(): FilterInterface
{
return null;
return new All();
}
};

Expand Down Expand Up @@ -670,9 +671,9 @@ public function withAddedFilterHandlers(FilterHandlerInterface ...$filterHandler
return clone $this;
}

public function getFilter(): ?FilterInterface
public function getFilter(): FilterInterface
{
return null;
return new All();
}

public function getLimit(): int
Expand Down
3 changes: 2 additions & 1 deletion tests/Reader/Iterable/IterableDataReaderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
use InvalidArgumentException;
use LogicException;
use Yiisoft\Data\Reader\DataReaderException;
use Yiisoft\Data\Reader\Filter\All;
use Yiisoft\Data\Reader\Filter\AndX;
use Yiisoft\Data\Reader\Filter\OrX;
use Yiisoft\Data\Reader\Filter\Equals;
Expand Down Expand Up @@ -69,7 +70,7 @@ public function testImmutability(): void
$reader = new IterableDataReader([]);

$this->assertNotSame($reader, $reader->withAddedFilterHandlers());
$this->assertNotSame($reader, $reader->withFilter(null));
$this->assertNotSame($reader, $reader->withFilter(new All()));
$this->assertNotSame($reader, $reader->withSort(null));
$this->assertNotSame($reader, $reader->withOffset(1));
$this->assertNotSame($reader, $reader->withLimit(1));
Expand Down
4 changes: 2 additions & 2 deletions tests/Support/MutationDataReader.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ public function __construct(
) {
}

public function withFilter(?FilterInterface $filter): static
public function withFilter(FilterInterface $filter): static
{
$new = clone $this;
$new->decorated = $this->decorated->withFilter($filter);
Expand Down Expand Up @@ -69,7 +69,7 @@ public function getSort(): ?Sort
return $this->decorated->getSort();
}

public function getFilter(): ?FilterInterface
public function getFilter(): FilterInterface
{
return $this->decorated->getFilter();
}
Expand Down
Loading