generated from yii-tools/template
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
e81368d
commit 5a9de9c
Showing
5 changed files
with
89 additions
and
120 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Yii\DataProvider; | ||
|
||
use ArrayIterator; | ||
use Traversable; | ||
|
||
abstract class AbstractIteratorProvider implements IteratorProviderInterface | ||
{ | ||
protected int $limit = self::DEFAULT_LIMIT; | ||
protected int $offset = self::DEFAULT_OFFSET; | ||
|
||
public function getIterator(): Traversable | ||
{ | ||
return new ArrayIterator($this->read()); | ||
} | ||
|
||
public function getLimit(): int | ||
{ | ||
return $this->limit; | ||
} | ||
|
||
public function getOffset(): int | ||
{ | ||
return $this->offset; | ||
} | ||
|
||
public function withLimit(int $value): static | ||
{ | ||
// validate limit | ||
if ($value < 0) { | ||
$value = self::DEFAULT_LIMIT; | ||
} | ||
|
||
$new = clone $this; | ||
$new->limit = $value; | ||
|
||
return $new; | ||
} | ||
|
||
public function withOffset(int $value): static | ||
{ | ||
$new = clone $this; | ||
$new->offset = $value; | ||
|
||
// validate offset | ||
if ($new->limit * ($new->offset - 1) <= 0) { | ||
$new->offset = self::DEFAULT_OFFSET; | ||
} | ||
|
||
return $new; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters