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 __construct( \Cycle\ORM\Select|\Cycle\Database\Query\SelectQuery $query, Yiisoft\Data\Cycle\Reader\QueryBuilderFilterHandler[] $extraFilterHandlers = [] ): mixed
$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 count( ): integer

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

            
getFilter() public method

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

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

            
getIterator() public method

Get Iterator without caching

public getIterator( ): Generator

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

            
getLimit() public method

public getLimit( ): integer|null

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

            
getOffset() public method

public getOffset( ): integer

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

            
getSort() public method

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

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

            
getSql() public method

public getSql( ): string

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

            
read() public method

public read( ): iterable

                #[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 readOne( ): array|object|null

                #[Override]
public function readOne(): array|object|null
{
    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 withFilter( \Yiisoft\Data\Reader\FilterInterface $filter ): Yiisoft\Data\Cycle\Reader\EntityReader
$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 withLimit( integer|null $limit ): Yiisoft\Data\Cycle\Reader\EntityReader
$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 withOffset( integer $offset ): Yiisoft\Data\Cycle\Reader\EntityReader
$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 withSort( \Yiisoft\Data\Reader\Sort|null $sort ): Yiisoft\Data\Cycle\Reader\EntityReader
$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;
}