Lines
92.85%
13 / 14
Functions and Methods
85.71%
6 / 7
Classes and Traits
0.00%
0 / 1
| Name | Lines | Functions and Methods | CRAP | Classes and Traits | ||||||
|---|---|---|---|---|---|---|---|---|---|---|
| KeyedCollection | 92.85% | 13 / 14 | 85.71% | 6 / 7 | 8.02 | 0.00% | 0 / 1 | |||
| keyedBy | 100.00% | 4 / 4 | 100.00% | 1 / 1 | 2 | |||||
| fromElementsByKey | 100.00% | 1 / 1 | 100.00% | 1 / 1 | 1 | |||||
| withElementAt | 100.00% | 3 / 3 | 100.00% | 1 / 1 | 1 | |||||
| withoutKey | 100.00% | 3 / 3 | 100.00% | 1 / 1 | 1 | |||||
| elementAt | 100.00% | 1 / 1 | 100.00% | 1 / 1 | 1 | |||||
| hasKey | 0.00% | 0 / 1 | 0.00% | 0 / 1 | 2 | |||||
| arrangeElements | 100.00% | 1 / 1 | 100.00% | 1 / 1 | 1 | |||||
| 1 | <?php | |
| 2 | ||
| 3 | declare(strict_types=1); | |
| 4 | ||
| 5 | namespace Stewart\Contracts\Collection; | |
| 6 | ||
| 7 | use Closure; | |
| 8 | ||
| 9 | /** | |
| 10 | * @template TKey of array-key | |
| 11 | * @template T | |
| 12 | * @extends TypedCollection<TKey, T> | |
| 13 | */ | |
| 14 | abstract readonly class KeyedCollection extends TypedCollection | |
| 15 | { | |
| 16 | /** | |
| 17 | * @param iterable<T> $elements | |
| 18 | * @param Closure(T): TKey $keyOf | |
| 19 | */ | |
| 20 | protected static function keyedBy(iterable $elements, Closure $keyOf): static | |
| 21 | { | |
| 22 | $keyed = []; | |
| 23 | ||
| 24 | foreach ($elements as $element) { | |
| 25 | $keyed[$keyOf($element)] = $element; | |
| 26 | } | |
| 27 | ||
| 28 | return new static($keyed); | |
| 29 | } | |
| 30 | ||
| 31 | /** @param array<TKey, T> $elementsByKey */ | |
| 32 | protected static function fromElementsByKey(array $elementsByKey): static | |
| 33 | { | |
| 34 | return new static($elementsByKey); | |
| 35 | } | |
| 36 | ||
| 37 | /** | |
| 38 | * @param TKey $key | |
| 39 | * @param T $element | |
| 40 | */ | |
| 41 | protected function withElementAt(int|string $key, mixed $element): static | |
| 42 | { | |
| 43 | $elements = $this->elements(); | |
| 44 | $elements[$key] = $element; | |
| 45 | ||
| 46 | return new static($elements); | |
| 47 | } | |
| 48 | ||
| 49 | /** @param TKey $key */ | |
| 50 | protected function withoutKey(int|string $key): static | |
| 51 | { | |
| 52 | $elements = $this->elements(); | |
| 53 | unset($elements[$key]); | |
| 54 | ||
| 55 | return new static($elements); | |
| 56 | } | |
| 57 | ||
| 58 | /** | |
| 59 | * @param TKey $key | |
| 60 | * @return T|null | |
| 61 | */ | |
| 62 | protected function elementAt(int|string $key): mixed | |
| 63 | { | |
| 64 | return $this->elements()[$key] ?? null; | |
| 65 | } | |
| 66 | ||
| 67 | /** @param TKey $key */ | |
| 68 | protected function hasKey(int|string $key): bool | |
| 69 | { | |
| 70 | return \array_key_exists($key, $this->elements()); | |
| 71 | } | |
| 72 | ||
| 73 | /** | |
| 74 | * @param array<TKey, T> $elements | |
| 75 | * @return array<TKey, T> | |
| 76 | */ | |
| 77 | protected function arrangeElements(array $elements): array | |
| 78 | { | |
| 79 | return $elements; | |
| 80 | } | |
| 81 | } |