0 follower

Final Class Yiisoft\Data\Reader\Iterable\IterableDataReader

InheritanceYiisoft\Data\Reader\Iterable\IterableDataReader
ImplementsYiisoft\Data\Reader\DataReaderInterface

Iterable data reader takes iterable data as a source and can:

  • Limit items read
  • Skip N items from the beginning
  • Sort items
  • Form a filter criteria with {@see \Yiisoft\Data\Reader\FilterInterface}
  • Post-filter items with {@see \Yiisoft\Data\Reader\Iterable\IterableFilterHandlerInterface}

Method Details

Hide inherited methods

__construct() public method

public mixed __construct ( iterable $data, Yiisoft\Data\Reader\Iterable\ValueReader\ValueReaderInterface $valueReader = new FlatValueReader(), Yiisoft\Data\Reader\Iterable\IterableFilterHandlerInterface[] $extraFilterHandlers = [] )
$data iterable

Data to iterate.

$valueReader Yiisoft\Data\Reader\Iterable\ValueReader\ValueReaderInterface
$extraFilterHandlers Yiisoft\Data\Reader\Iterable\IterableFilterHandlerInterface[]

                public function __construct(
    private readonly iterable $data,
    ValueReaderInterface $valueReader = new FlatValueReader(),
    array $extraFilterHandlers = [],
) {
    $filterHandlers = $this->prepareFilterHandlers([
        new AllHandler(),
        new NoneHandler(),
        new AndXHandler(),
        new OrXHandler(),
        new BetweenHandler(),
        new EqualsHandler(),
        new EqualsNullHandler(),
        new GreaterThanHandler(),
        new GreaterThanOrEqualHandler(),
        new InHandler(),
        new LessThanHandler(),
        new LessThanOrEqualHandler(),
        new LikeHandler(),
        new NotHandler(),
        ...$extraFilterHandlers,
    ]);
    $this->context = new Context($filterHandlers, $valueReader);
    $this->filter = new All();
}

            
count() public method

public integer count ( )

                public function count(): int
{
    return count($this->internalRead(useLimitAndOffset: false));
}

            
getFilter() public method

public Yiisoft\Data\Reader\FilterInterface getFilter ( )

                public function getFilter(): FilterInterface
{
    return $this->filter;
}

            
getIterator() public method

public Generator getIterator ( )

                public function getIterator(): Generator
{
    yield from $this->read();
}

            
getLimit() public method

public ?int getLimit ( )

                public function getLimit(): ?int
{
    return $this->limit;
}

            
getOffset() public method

public integer getOffset ( )

                public function getOffset(): int
{
    return $this->offset;
}

            
getSort() public method

public ?\Yiisoft\Data\Reader\Sort getSort ( )

                public function getSort(): ?Sort
{
    return $this->sort;
}

            
read() public method

public array read ( )

                public function read(): array
{
    return $this->internalRead(useLimitAndOffset: true);
}

            
readOne() public method

public array|object|null readOne ( )

                public function readOne(): array|object|null
{
    if ($this->limit === 0) {
        return null;
    }
    /** @infection-ignore-all Any value more than one in `withLimit()` will be ignored because returned `current()` */
    return $this
        ->withLimit(1)
        ->getIterator()
        ->current();
}

            
withFilter() public method

public static withFilter ( Yiisoft\Data\Reader\FilterInterface $filter )
$filter Yiisoft\Data\Reader\FilterInterface

                public function withFilter(FilterInterface $filter): static
{
    $new = clone $this;
    $new->filter = $filter;
    return $new;
}

            
withLimit() public method

public static withLimit ( ?int $limit )
$limit ?int

                public function withLimit(?int $limit): static
{
    if ($limit < 0) {
        throw new InvalidArgumentException('The limit must not be less than 0.');
    }
    $new = clone $this;
    $new->limit = $limit;
    return $new;
}

            
withOffset() public method

public static withOffset ( integer $offset )
$offset integer

                public function withOffset(int $offset): static
{
    $new = clone $this;
    $new->offset = $offset;
    return $new;
}

            
withSort() public method

public static withSort ( ?\Yiisoft\Data\Reader\Sort $sort )
$sort ?\Yiisoft\Data\Reader\Sort

                public function withSort(?Sort $sort): static
{
    $new = clone $this;
    $new->sort = $sort;
    return $new;
}