Skip to content

Commit

Permalink
refactor(add-paging-to-result-set): fixed legacy type-hintings, rearr…
Browse files Browse the repository at this point in the history
…anged methods
  • Loading branch information
ncosta-ic committed Apr 12, 2024
1 parent 0dfed84 commit 5f4ee88
Showing 1 changed file with 53 additions and 47 deletions.
100 changes: 53 additions & 47 deletions src/ResultSet.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,18 +10,16 @@

/**
* Dataset containing database rows
*
* @implements Iterator<int, mixed>
*/
class ResultSet implements Iterator
{
/** @var ArrayIterator<int, mixed> */
/** @var ArrayIterator */
protected $cache;

Check failure on line 17 in src/ResultSet.php

View workflow job for this annotation

GitHub Actions / phpstan / Static analysis with phpstan and php 8.0 on ubuntu-latest

Property ipl\Orm\ResultSet::$cache with generic class ArrayIterator does not specify its types: TKey, TValue

Check failure on line 17 in src/ResultSet.php

View workflow job for this annotation

GitHub Actions / phpstan / Static analysis with phpstan and php 8.3 on ubuntu-latest

Property ipl\Orm\ResultSet::$cache with generic class ArrayIterator does not specify its types: TKey, TValue

Check failure on line 17 in src/ResultSet.php

View workflow job for this annotation

GitHub Actions / phpstan / Static analysis with phpstan and php 8.1 on ubuntu-latest

Property ipl\Orm\ResultSet::$cache with generic class ArrayIterator does not specify its types: TKey, TValue

Check failure on line 17 in src/ResultSet.php

View workflow job for this annotation

GitHub Actions / phpstan / Static analysis with phpstan and php 8.2 on ubuntu-latest

Property ipl\Orm\ResultSet::$cache with generic class ArrayIterator does not specify its types: TKey, TValue

/** @var bool Whether cache is disabled */
protected $isCacheDisabled = false;

/** @var Generator<int, mixed, mixed, mixed> */
/** @var Generator */
protected $generator;

/** @var ?int */
Expand All @@ -37,26 +35,60 @@ class ResultSet implements Iterator
protected $pageSize;

/**
* @param Traversable<int, mixed> $traversable
* @param Traversable $traversable
* @param ?int $limit
* @param ?int $offset
*/
public function __construct(Traversable $traversable, ?int $limit = null, ?int $offset = null)
public function __construct(Traversable $traversable, $limit = null, $offset = null)
{
$this->cache = new ArrayIterator();
$this->generator = $this->yieldTraversable($traversable);
$this->limit = $limit;
$this->offset = $offset;
}

/**
* Returns the current page calculated from the {@see ResultSet::$offset} and the {@see ResultSet::$pageSize}
*
* @return int
* @throws BadMethodCallException if no {@see ResultSet::$pageSize} has been provided
*/
public function getCurrentPage(): int
{
if ($this->pageSize) {
if ($this->offset && $this->offset > $this->pageSize) {
// offset is not on the first page anymore
return intval(floor($this->offset / $this->pageSize));
}

// no offset defined or still on page 1
return 1;
}

throw new BadMethodCallException(`The 'pageSize' property has not been set. Cannot calculate pages.`);
}

/**
* Sets the amount of items a page should contain (needed for pagination)
*
* @param ?int $size
* @return $this
*/
public function setPageSize(?int $size)
{
$this->pageSize = $size;

return $this;
}

/**
* Create a new result set from the given query
*
* @param Query $query
*
* @return ResultSet
* @return static
*/
public static function fromQuery(Query $query): ResultSet
public static function fromQuery(Query $query)
{
return new static($query->yieldResults(), $query->getLimit(), $query->getOffset());
}
Expand All @@ -66,21 +98,27 @@ public static function fromQuery(Query $query): ResultSet
*
* ResultSet instance can only be iterated once
*
* @return ResultSet
* @return $this
*/
public function disableCache(): ResultSet
public function disableCache()
{
$this->isCacheDisabled = true;

return $this;
}

public function hasMore(): bool
/**
* @return bool
*/
public function hasMore()
{
return $this->generator->valid();
}

public function hasResult(): bool
/**
* @return bool
*/
public function hasResult()
{
return $this->generator->valid();
}
Expand Down Expand Up @@ -140,7 +178,7 @@ public function rewind(): void
}
}

protected function advance(): void
protected function advance()
{
if (! $this->generator->valid()) {
return;
Expand All @@ -161,45 +199,13 @@ protected function advance(): void
}

/**
* @param Traversable<int, mixed> $traversable
* @param Traversable $traversable
* @return Generator
*/
protected function yieldTraversable(Traversable $traversable): Generator
protected function yieldTraversable(Traversable $traversable)
{
foreach ($traversable as $key => $value) {
yield $key => $value;
}
}

/**
* Sets the amount of items a page should contain (only needed for pagination)
*
* @param ?int $size
* @return void
*/
public function setPageSize(?int $size): void
{
$this->pageSize = $size;
}

/**
* Returns the current page calculated from the {@see ResultSet::$offset} and the {@see ResultSet::$pageSize}
*
* @return int
* @throws BadMethodCallException if no {@see ResultSet::$pageSize} has been provided
*/
protected function getCurrentPage(): int
{
if ($this->pageSize) {
if ($this->offset && $this->offset > $this->pageSize) {
// offset is not on the first page anymore
return intval(floor($this->offset / $this->pageSize));
}

// no offset defined or still on page 1
return 1;
}

throw new BadMethodCallException(`The 'pageSize' property has not been set. Cannot calculate pages.`);
}
}

0 comments on commit 5f4ee88

Please sign in to comment.