Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
100.00% |
12 / 12 |
|
100.00% |
7 / 7 |
CRAP | |
100.00% |
1 / 1 |
| FakeIterator | |
100.00% |
12 / 12 |
|
100.00% |
7 / 7 |
8 | |
100.00% |
1 / 1 |
| __construct | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| current | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| updateCurrent | |
100.00% |
4 / 4 |
|
100.00% |
1 / 1 |
2 | |||
| key | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| next | |
100.00% |
2 / 2 |
|
100.00% |
1 / 1 |
1 | |||
| rewind | |
100.00% |
2 / 2 |
|
100.00% |
1 / 1 |
1 | |||
| valid | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| 1 | <?php |
| 2 | namespace Apie\Faker\Datalayers; |
| 3 | |
| 4 | use Apie\Core\Entities\EntityInterface; |
| 5 | use Faker\Generator; |
| 6 | use Iterator; |
| 7 | use ReflectionClass; |
| 8 | |
| 9 | /** |
| 10 | * @template T of EntityInterface |
| 11 | * @implements Iterator<int, T> |
| 12 | */ |
| 13 | class FakeIterator implements Iterator |
| 14 | { |
| 15 | private int $offset; |
| 16 | |
| 17 | private ?EntityInterface $currentEntity; |
| 18 | |
| 19 | /** |
| 20 | * @param ReflectionClass<T> $class |
| 21 | */ |
| 22 | public function __construct( |
| 23 | private readonly int $count, |
| 24 | private readonly ReflectionClass $class, |
| 25 | private readonly Generator $faker |
| 26 | ) { |
| 27 | $this->rewind(); |
| 28 | } |
| 29 | /** |
| 30 | * @return T|null |
| 31 | */ |
| 32 | public function current(): ?EntityInterface |
| 33 | { |
| 34 | return $this->currentEntity; |
| 35 | } |
| 36 | |
| 37 | private function updateCurrent(): void |
| 38 | { |
| 39 | if ($this->offset >= $this->count) { |
| 40 | $this->currentEntity = null; |
| 41 | return; |
| 42 | } |
| 43 | $this->currentEntity = $this->faker->fakeClass($this->class->name); |
| 44 | } |
| 45 | public function key(): int |
| 46 | { |
| 47 | return $this->offset; |
| 48 | } |
| 49 | public function next(): void |
| 50 | { |
| 51 | $this->offset++; |
| 52 | $this->updateCurrent(); |
| 53 | } |
| 54 | public function rewind(): void |
| 55 | { |
| 56 | $this->offset = 0; |
| 57 | $this->updateCurrent(); |
| 58 | } |
| 59 | public function valid(): bool |
| 60 | { |
| 61 | return null !== $this->currentEntity; |
| 62 | } |
| 63 | } |