Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
100.00% |
13 / 13 |
|
100.00% |
6 / 6 |
CRAP | |
100.00% |
1 / 1 |
| IteratorProxy | |
100.00% |
13 / 13 |
|
100.00% |
6 / 6 |
11 | |
100.00% |
1 / 1 |
| __construct | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| current | |
100.00% |
2 / 2 |
|
100.00% |
1 / 1 |
1 | |||
| unwrap | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| is | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
2 | |||
| in | |
100.00% |
6 / 6 |
|
100.00% |
1 / 1 |
4 | |||
| toArray | |
100.00% |
2 / 2 |
|
100.00% |
1 / 1 |
2 | |||
| 1 | <?php |
| 2 | |
| 3 | declare(strict_types=1); |
| 4 | |
| 5 | namespace Celema\Boiler\Proxy; |
| 6 | |
| 7 | use Celema\Boiler\Contract\Wrapper; |
| 8 | use Iterator; |
| 9 | use IteratorIterator; |
| 10 | use Override; |
| 11 | use Traversable; |
| 12 | |
| 13 | /** |
| 14 | * @api |
| 15 | * |
| 16 | * @template-covariant TKey |
| 17 | * @template-covariant TValue |
| 18 | * |
| 19 | * @template TIterator as \Traversable<TKey, TValue> |
| 20 | * |
| 21 | * @template-extends IteratorIterator<TKey, TValue, TIterator> |
| 22 | * @implements Proxy<Iterator<TKey, TValue>|null> |
| 23 | */ |
| 24 | final class IteratorProxy extends IteratorIterator implements Proxy |
| 25 | { |
| 26 | /** @param TIterator $iterator */ |
| 27 | public function __construct( |
| 28 | Traversable $iterator, |
| 29 | private readonly Wrapper $wrapper, |
| 30 | ) { |
| 31 | parent::__construct($iterator); |
| 32 | } |
| 33 | |
| 34 | #[Override] |
| 35 | public function current(): mixed |
| 36 | { |
| 37 | $value = parent::current(); |
| 38 | |
| 39 | /** @psalm-suppress MixedReturnStatement see above */ |
| 40 | return $this->wrapper->wrap($value); |
| 41 | } |
| 42 | |
| 43 | #[Override] |
| 44 | public function unwrap(): ?Iterator |
| 45 | { |
| 46 | return $this->getInnerIterator(); |
| 47 | } |
| 48 | |
| 49 | #[Override] |
| 50 | public function is(mixed $other): bool |
| 51 | { |
| 52 | return $this->getInnerIterator() === ($other instanceof Proxy ? $other->unwrap() : $other); |
| 53 | } |
| 54 | |
| 55 | /** @param ArrayProxy|array<array-key, mixed> $haystack */ |
| 56 | #[Override] |
| 57 | public function in(ArrayProxy|array $haystack): bool |
| 58 | { |
| 59 | if ($haystack instanceof ArrayProxy) { |
| 60 | $haystack = $haystack->unwrap(); |
| 61 | } |
| 62 | |
| 63 | /** @var mixed $item */ |
| 64 | foreach ($haystack as $item) { |
| 65 | if ($this->is($item)) { |
| 66 | return true; |
| 67 | } |
| 68 | } |
| 69 | |
| 70 | return false; |
| 71 | } |
| 72 | |
| 73 | public function toArray(): ArrayProxy |
| 74 | { |
| 75 | $inner = $this->getInnerIterator(); |
| 76 | |
| 77 | return new ArrayProxy($inner ? iterator_to_array($inner) : [], $this->wrapper); |
| 78 | } |
| 79 | } |