|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +/* |
| 6 | + * This file is part of rekalogika/mapper package. |
| 7 | + * |
| 8 | + * (c) Priyadi Iman Nurcahyo <https://rekalogika.dev> |
| 9 | + * |
| 10 | + * For the full copyright and license information, please view the LICENSE file |
| 11 | + * that was distributed with this source code. |
| 12 | + */ |
| 13 | + |
| 14 | +namespace Rekalogika\Mapper\Transformer\Model; |
| 15 | + |
| 16 | +use Rekalogika\Mapper\Context\Context; |
| 17 | +use Rekalogika\Mapper\ListInterface; |
| 18 | +use Rekalogika\Mapper\MainTransformer\MainTransformerInterface; |
| 19 | +use Rekalogika\Mapper\Transformer\ArrayLikeMetadata\ArrayLikeMetadata; |
| 20 | +use Rekalogika\Mapper\Transformer\MainTransformerAwareTrait; |
| 21 | +use Rekalogika\Mapper\Transformer\Trait\ArrayLikeTransformerTrait; |
| 22 | + |
| 23 | +/** |
| 24 | + * Discards source key, and use incremental integer key in the target. |
| 25 | + * |
| 26 | + * @template TValue |
| 27 | + * @implements ListInterface<int,TValue> |
| 28 | + * @internal |
| 29 | + */ |
| 30 | +final class LazyList implements ListInterface |
| 31 | +{ |
| 32 | + use MainTransformerAwareTrait; |
| 33 | + use ArrayLikeTransformerTrait; |
| 34 | + |
| 35 | + /** |
| 36 | + * @var array<int,TValue> |
| 37 | + */ |
| 38 | + private array $cachedData = []; |
| 39 | + private bool $isCached = false; |
| 40 | + |
| 41 | + /** |
| 42 | + * @param (\Traversable<mixed,mixed>&\ArrayAccess<mixed,mixed>&\Countable)|array<int|string,mixed> $source |
| 43 | + */ |
| 44 | + public function __construct( |
| 45 | + private (\Traversable&\ArrayAccess&\Countable)|array $source, |
| 46 | + MainTransformerInterface $mainTransformer, |
| 47 | + private ArrayLikeMetadata $metadata, |
| 48 | + private Context $context, |
| 49 | + ) { |
| 50 | + $this->mainTransformer = $mainTransformer; |
| 51 | + } |
| 52 | + |
| 53 | + public function offsetExists(mixed $offset): bool |
| 54 | + { |
| 55 | + return isset($this->cachedData[$offset]) || isset($this->source[$offset]); |
| 56 | + } |
| 57 | + |
| 58 | + public function offsetGet(mixed $offset): mixed |
| 59 | + { |
| 60 | + if (!$this->isCached) { |
| 61 | + foreach($this->getIterator() as $i); |
| 62 | + } |
| 63 | + |
| 64 | + return $this->cachedData[$offset]; |
| 65 | + } |
| 66 | + |
| 67 | + public function offsetSet(mixed $offset, mixed $value): void |
| 68 | + { |
| 69 | + throw new \BadMethodCallException('LazyArray is immutable.'); |
| 70 | + } |
| 71 | + |
| 72 | + public function offsetUnset(mixed $offset): void |
| 73 | + { |
| 74 | + throw new \BadMethodCallException('LazyArray is immutable.'); |
| 75 | + } |
| 76 | + |
| 77 | + public function getIterator(): \Traversable |
| 78 | + { |
| 79 | + if ($this->isCached) { |
| 80 | + yield from $this->cachedData; |
| 81 | + |
| 82 | + return; |
| 83 | + } |
| 84 | + |
| 85 | + $i = 0; |
| 86 | + |
| 87 | + /** |
| 88 | + * @var mixed $sourceMemberValue |
| 89 | + */ |
| 90 | + foreach ($this->source as $sourceMemberKey => $sourceMemberValue) { |
| 91 | + /** |
| 92 | + * @var TValue $value |
| 93 | + */ |
| 94 | + [, $value] = $this->transformMember( |
| 95 | + sourceMemberKey: $sourceMemberKey, |
| 96 | + sourceMemberValue: $sourceMemberValue, |
| 97 | + metadata: $this->metadata, |
| 98 | + context: $this->context, |
| 99 | + ); |
| 100 | + |
| 101 | + $this->cachedData[$i] = $value; |
| 102 | + |
| 103 | + yield $value; |
| 104 | + |
| 105 | + $i++; |
| 106 | + } |
| 107 | + |
| 108 | + $this->isCached = true; |
| 109 | + } |
| 110 | + |
| 111 | + public function count(): int |
| 112 | + { |
| 113 | + return count($this->source); |
| 114 | + } |
| 115 | +} |
0 commit comments