Skip to content

Commit

Permalink
LimitableDataInterface
Browse files Browse the repository at this point in the history
  • Loading branch information
vjik committed Nov 15, 2023
1 parent f30a971 commit 154f82a
Show file tree
Hide file tree
Showing 8 changed files with 66 additions and 28 deletions.
2 changes: 2 additions & 0 deletions psalm-php80.xml
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
<?xml version="1.0"?>
<psalm
errorLevel="1"
findUnusedBaselineEntry="true"
findUnusedCode="false"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="https://getpsalm.org/schema/config"
xsi:schemaLocation="https://getpsalm.org/schema/config vendor/vimeo/psalm/config.xsd"
Expand Down
2 changes: 2 additions & 0 deletions psalm.xml
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
<?xml version="1.0"?>
<psalm
errorLevel="1"
findUnusedBaselineEntry="true"
findUnusedCode="false"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="https://getpsalm.org/schema/config"
xsi:schemaLocation="https://getpsalm.org/schema/config vendor/vimeo/psalm/config.xsd"
Expand Down
14 changes: 11 additions & 3 deletions src/Paginator/KeysetPaginator.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
use Yiisoft\Data\Reader\Filter\LessThan;
use Yiisoft\Data\Reader\Filter\LessThanOrEqual;
use Yiisoft\Data\Reader\FilterableDataInterface;
use Yiisoft\Data\Reader\LimitableDataInterface;
use Yiisoft\Data\Reader\ReadableDataInterface;
use Yiisoft\Data\Reader\Sort;
use Yiisoft\Data\Reader\SortableDataInterface;
Expand Down Expand Up @@ -49,7 +50,7 @@ final class KeysetPaginator implements PaginatorInterface
/**
* Data reader being paginated.
*
* @psalm-var ReadableDataInterface<TKey, TValue>&FilterableDataInterface&SortableDataInterface
* @psalm-var ReadableDataInterface<TKey, TValue>&LimitableDataInterface&FilterableDataInterface&SortableDataInterface
*/
private ReadableDataInterface $dataReader;

Expand Down Expand Up @@ -82,11 +83,18 @@ final class KeysetPaginator implements PaginatorInterface

/**
* @param ReadableDataInterface $dataReader Data reader being paginated.
* @psalm-param ReadableDataInterface<TKey, TValue>&FilterableDataInterface&SortableDataInterface $dataReader
* @psalm-param ReadableDataInterface<TKey, TValue>&LimitableDataInterface&FilterableDataInterface&SortableDataInterface $dataReader
* @psalm-suppress DocblockTypeContradiction Needed to allow validating `$dataReader`
*/
public function __construct(ReadableDataInterface $dataReader)
{
if (!$dataReader instanceof LimitableDataInterface) {
throw new InvalidArgumentException(sprintf(
'Data reader should implement "%s" to be used with keyset paginator.',
LimitableDataInterface::class,
));
}

if (!$dataReader instanceof FilterableDataInterface) {
throw new InvalidArgumentException(sprintf(
'Data reader should implement "%s" to be used with keyset paginator.',
Expand Down Expand Up @@ -289,7 +297,7 @@ private function reverseData(array $data): array
}

/**
* @psalm-param ReadableDataInterface<TKey, TValue>&FilterableDataInterface&SortableDataInterface $dataReader
* @psalm-param ReadableDataInterface<TKey, TValue>&LimitableDataInterface&FilterableDataInterface&SortableDataInterface $dataReader
*/
private function previousPageExist(ReadableDataInterface $dataReader, Sort $sort): bool
{
Expand Down
12 changes: 10 additions & 2 deletions src/Paginator/OffsetPaginator.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
use Generator;
use InvalidArgumentException;
use Yiisoft\Data\Reader\CountableDataInterface;
use Yiisoft\Data\Reader\LimitableDataInterface;
use Yiisoft\Data\Reader\OffsetableDataInterface;
use Yiisoft\Data\Reader\ReadableDataInterface;
use Yiisoft\Data\Reader\Sort;
Expand Down Expand Up @@ -50,17 +51,24 @@ final class OffsetPaginator implements PaginatorInterface
/**
* Data reader being paginated.
*
* @psalm-var ReadableDataInterface<TKey, TValue>&OffsetableDataInterface&CountableDataInterface
* @psalm-var ReadableDataInterface<TKey, TValue>&LimitableDataInterface&OffsetableDataInterface&CountableDataInterface
*/
private ReadableDataInterface $dataReader;

/**
* @param ReadableDataInterface $dataReader Data reader being paginated.
* @psalm-param ReadableDataInterface<TKey, TValue>&OffsetableDataInterface&CountableDataInterface $dataReader
* @psalm-param ReadableDataInterface<TKey, TValue>&LimitableDataInterface&OffsetableDataInterface&CountableDataInterface $dataReader
* @psalm-suppress DocblockTypeContradiction Needed to allow validating `$dataReader`
*/
public function __construct(ReadableDataInterface $dataReader)
{
if (!$dataReader instanceof LimitableDataInterface) {
throw new InvalidArgumentException(sprintf(
'Data reader should implement "%s" in order to be used with offset paginator.',
LimitableDataInterface::class,
));
}

if (!$dataReader instanceof OffsetableDataInterface) {
throw new InvalidArgumentException(sprintf(
'Data reader should implement "%s" in order to be used with offset paginator.',
Expand Down
5 changes: 4 additions & 1 deletion src/Paginator/PaginatorInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

namespace Yiisoft\Data\Paginator;

use Yiisoft\Data\Reader\ReadableDataInterface;
use Yiisoft\Data\Reader\Sort;

/**
Expand All @@ -14,8 +15,10 @@
*
* @template TKey as array-key
* @template TValue as array|object
*
* @extends ReadableDataInterface<TKey, TValue>
*/
interface PaginatorInterface
interface PaginatorInterface extends ReadableDataInterface
{
/**
* Page size that is used in case it is not set explicitly.
Expand Down
2 changes: 2 additions & 0 deletions src/Reader/DataReaderInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,12 @@
* @template TValue as array|object
*
* @extends ReadableDataInterface<TKey, TValue>
* @extends LimitableDataInterface<TKey, TValue>
* @extends IteratorAggregate<TKey, TValue>
*/
interface DataReaderInterface extends
ReadableDataInterface,
LimitableDataInterface,
OffsetableDataInterface,
CountableDataInterface,
SortableDataInterface,
Expand Down
35 changes: 35 additions & 0 deletions src/Reader/LimitableDataInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
<?php

declare(strict_types=1);

namespace Yiisoft\Data\Reader;

use InvalidArgumentException;

/**
* @template TKey as array-key
* @template TValue as array|object
*/
interface LimitableDataInterface
{

/**
* Get a new instance with limit set.
*
* @param int $limit Limit. 0 means "no limit".
*
* @throws InvalidArgumentException If limit is less than 0.
*
* @return static New instance.
* @psalm-return $this
*/
public function withLimit(int $limit): static;

/**
* Get a next item from the data set.
*
* @return array|object|null An item or null if there is none.
* @psalm-return TValue|null
*/
public function readOne(): array|object|null;
}
22 changes: 0 additions & 22 deletions src/Reader/ReadableDataInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,6 @@

namespace Yiisoft\Data\Reader;

use InvalidArgumentException;

/**
* Readable data is a data set that could be read up to number of items
* defined by limit either one by one or by getting an iterator.
Expand All @@ -15,31 +13,11 @@
*/
interface ReadableDataInterface
{
/**
* Get a new instance with limit set.
*
* @param int $limit Limit. 0 means "no limit".
*
* @throws InvalidArgumentException If limit is less than 0.
*
* @return static New instance.
* @psalm-return $this
*/
public function withLimit(int $limit): static;

/**
* Get iterable for the data set.
*
* @return iterable Iterable for the data.
* @psalm-return iterable<TKey, TValue>
*/
public function read(): iterable;

/**
* Get a next item from the data set.
*
* @return array|object|null An item or null if there is none.
* @psalm-return TValue|null
*/
public function readOne(): array|object|null;
}

0 comments on commit 154f82a

Please sign in to comment.