0 follower

Final Class Yiisoft\Data\Cycle\Reader\EntityReader

InheritanceYiisoft\Data\Cycle\Reader\EntityReader
ImplementsYiisoft\Data\Reader\DataReaderInterface

Method Details

Hide inherited methods

__construct() public method

public mixed __construct ( \Cycle\ORM\Select|\Cycle\Database\Query\SelectQuery $query, Yiisoft\Data\Cycle\Reader\QueryBuilderFilterHandler[] $extraFilterHandlers = [] )
$query \Cycle\ORM\Select|\Cycle\Database\Query\SelectQuery
$extraFilterHandlers Yiisoft\Data\Cycle\Reader\QueryBuilderFilterHandler[]

                public function __construct(Select|SelectQuery $query, array $extraFilterHandlers = [])
{
    $this->query = clone $query;
    $this->countCache = new CachedCount($this->query);
    $this->itemsCache = new CachedCollection();
    $this->oneItemCache = new CachedCollection();
    /**
     * @psalm-suppress InternalMethod There is no other way to get driver for SelectQuery.
     * @psalm-suppress UndefinedMagicMethod The magic method is not defined in annotations.
     */
    $likeHandler = LikeHandlerFactory::getLikeHandler($this->query->getDriver()?->getType() ?? 'SQLite');
    $this->setFilterHandlers(
        new FilterHandler\AllHandler(),
        new FilterHandler\NoneHandler(),
        new FilterHandler\AndXHandler(),
        new FilterHandler\OrXHandler(),
        new FilterHandler\BetweenHandler(),
        new FilterHandler\EqualsHandler(),
        new FilterHandler\EqualsNullHandler(),
        new FilterHandler\GreaterThanHandler(),
        new FilterHandler\GreaterThanOrEqualHandler(),
        new FilterHandler\InHandler(),
        new FilterHandler\LessThanHandler(),
        new FilterHandler\LessThanOrEqualHandler(),
        $likeHandler,
        new FilterHandler\NotHandler(),
        ...$extraFilterHandlers,
    );
    $this->filter = new All();
}

            
count() public method

public integer count ( )

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

            
getFilter() public method

public \Yiisoft\Data\Reader\FilterInterface getFilter ( )

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

            
getIterator() public method

Get Iterator without caching

public Generator getIterator ( )

                #[\Override]
public function getIterator(): Generator
{
    yield from $this->itemsCache->getCollection() ?? $this->buildSelectQuery()->getIterator();
}

            
getLimit() public method

public integer|null getLimit ( )

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

            
getOffset() public method

public integer getOffset ( )

                #[\Override]
public function getOffset(): int
{
    return $this->offset;
}

            
getSort() public method

public \Yiisoft\Data\Reader\Sort|null getSort ( )

                #[\Override]
public function getSort(): ?Sort
{
    return $this->sorting;
}

            
getSql() public method

public string getSql ( )

                public function getSql(): string
{
    $query = $this->buildSelectQuery();
    return (string)($query instanceof Select ? $query->buildQuery() : $query);
}

            
read() public method

public iterable read ( )

                #[\Override]
public function read(): iterable
{
    if ($this->itemsCache->getCollection() === null) {
        $query = $this->buildSelectQuery();
        $this->itemsCache->setCollection($query->fetchAll());
    }
    return $this->itemsCache->getCollection();
}

            
readOne() public method

public null|array|object readOne ( )

                #[\Override]
public function readOne(): null|array|object
{
    if (!$this->oneItemCache->isCollected()) {
        $item = $this->itemsCache->isCollected()
            // get the first item from a cached collection
            ? $this->itemsCache->getGenerator()->current()
            // read data with limit 1
            : $this->withLimit(1)->getIterator()->current();
        $this->oneItemCache->setCollection($item === null ? [] : [$item]);
    }
    return $this->oneItemCache->getGenerator()->current();
}

            
withFilter() public method

public Yiisoft\Data\Cycle\Reader\EntityReader withFilter ( \Yiisoft\Data\Reader\FilterInterface $filter )
$filter \Yiisoft\Data\Reader\FilterInterface

                #[\Override]
public function withFilter(FilterInterface $filter): static
{
    $new = clone $this;
    if ($new->filter !== $filter) {
        $new->filter = $filter;
        $new->itemsCache = new CachedCollection();
        $new->oneItemCache = new CachedCollection();
        /** @psalm-suppress ImpureMethodCall */
        $new->resetCountCache();
    }
    return $new;
}

            
withLimit() public method

public Yiisoft\Data\Cycle\Reader\EntityReader withLimit ( integer|null $limit )
$limit integer|null

                #[\Override]
public function withLimit(?int $limit): static
{
    /** @psalm-suppress DocblockTypeContradiction */
    if ($limit < 0) {
        throw new InvalidArgumentException('$limit must not be less than 0.');
    }
    $new = clone $this;
    if ($new->limit !== $limit) {
        $new->limit = $limit;
        $new->itemsCache = new CachedCollection();
    }
    return $new;
}

            
withOffset() public method

public Yiisoft\Data\Cycle\Reader\EntityReader withOffset ( integer $offset )
$offset integer

                #[\Override]
public function withOffset(int $offset): static
{
    $new = clone $this;
    if ($new->offset !== $offset) {
        $new->offset = $offset;
        $new->itemsCache = new CachedCollection();
    }
    return $new;
}

            
withSort() public method

public Yiisoft\Data\Cycle\Reader\EntityReader withSort ( \Yiisoft\Data\Reader\Sort|null $sort )
$sort \Yiisoft\Data\Reader\Sort|null

                #[\Override]
public function withSort(?Sort $sort): static
{
    $new = clone $this;
    if ($new->sorting !== $sort) {
        $new->sorting = $sort;
        $new->itemsCache = new CachedCollection();
        $new->oneItemCache = new CachedCollection();
    }
    return $new;
}