Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
80.00% |
40 / 50 |
|
66.67% |
10 / 15 |
CRAP | |
0.00% |
0 / 1 |
| ItemSet | |
80.00% |
40 / 50 |
|
66.67% |
10 / 15 |
34.27 | |
0.00% |
0 / 1 |
| __construct | |
100.00% |
5 / 5 |
|
100.00% |
1 / 1 |
2 | |||
| count | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| first | |
0.00% |
0 / 3 |
|
0.00% |
0 / 1 |
6 | |||
| toArray | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| getIterator | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| jsonSerialize | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| offsetExists | |
100.00% |
2 / 2 |
|
100.00% |
1 / 1 |
1 | |||
| offsetGet | |
100.00% |
4 / 4 |
|
100.00% |
1 / 1 |
2 | |||
| getType | |
100.00% |
4 / 4 |
|
100.00% |
1 / 1 |
2 | |||
| offsetCheck | |
85.71% |
6 / 7 |
|
0.00% |
0 / 1 |
4.05 | |||
| typeCheck | |
100.00% |
5 / 5 |
|
100.00% |
1 / 1 |
3 | |||
| append | |
0.00% |
0 / 4 |
|
0.00% |
0 / 1 |
6 | |||
| offsetSet | |
80.00% |
4 / 5 |
|
0.00% |
0 / 1 |
2.03 | |||
| offsetUnset | |
83.33% |
5 / 6 |
|
0.00% |
0 / 1 |
3.04 | |||
| clone | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| 1 | <?php |
| 2 | namespace Apie\Core\Lists; |
| 3 | |
| 4 | use Apie\Core\Entities\EntityInterface; |
| 5 | use Apie\Core\Exceptions\IndexNotFoundException; |
| 6 | use Apie\Core\Exceptions\InvalidTypeException; |
| 7 | use Apie\Core\Exceptions\ObjectIsEmpty; |
| 8 | use Apie\Core\Exceptions\ObjectIsImmutable; |
| 9 | use Apie\Core\TypeUtils; |
| 10 | use Apie\Core\ValueObjects\Utils; |
| 11 | use ArrayIterator; |
| 12 | use Iterator; |
| 13 | use ReflectionClass; |
| 14 | use ReflectionType; |
| 15 | use Throwable; |
| 16 | |
| 17 | /** |
| 18 | * @template T |
| 19 | * @implements ItemListInterface<T> |
| 20 | */ |
| 21 | class ItemSet implements ItemListInterface |
| 22 | { |
| 23 | /** |
| 24 | * @var array<int, T> |
| 25 | */ |
| 26 | protected array $internal = []; |
| 27 | |
| 28 | /** @var ReflectionType[] */ |
| 29 | private static $typeMapping = []; |
| 30 | |
| 31 | protected bool $mutable = true; |
| 32 | |
| 33 | /** |
| 34 | * @param array<int|string, T> $input |
| 35 | */ |
| 36 | final public function __construct(array $input = []) |
| 37 | { |
| 38 | $oldMutable = $this->mutable; |
| 39 | $this->mutable = true; |
| 40 | foreach ($input as $item) { |
| 41 | $this->offsetSet(null, $item); |
| 42 | } |
| 43 | $this->mutable = $oldMutable; |
| 44 | } |
| 45 | |
| 46 | public function count(): int |
| 47 | { |
| 48 | return count($this->internal); |
| 49 | } |
| 50 | |
| 51 | /** |
| 52 | * @return T |
| 53 | */ |
| 54 | public function first(): mixed |
| 55 | { |
| 56 | if (empty($this->internal)) { |
| 57 | throw ObjectIsEmpty::createForList(); |
| 58 | } |
| 59 | return reset($this->internal); |
| 60 | } |
| 61 | |
| 62 | /** |
| 63 | * @return array<int, T> |
| 64 | */ |
| 65 | public function toArray(): array |
| 66 | { |
| 67 | return array_values($this->internal); |
| 68 | } |
| 69 | |
| 70 | /** |
| 71 | * @return Iterator<int, T> |
| 72 | */ |
| 73 | public function getIterator(): Iterator |
| 74 | { |
| 75 | return new ArrayIterator(array_values($this->internal)); |
| 76 | } |
| 77 | |
| 78 | /** |
| 79 | * @return array<int, T> |
| 80 | */ |
| 81 | public function jsonSerialize(): array |
| 82 | { |
| 83 | return array_values($this->internal); |
| 84 | } |
| 85 | |
| 86 | public function offsetExists(mixed $offset): bool |
| 87 | { |
| 88 | $offset = $this->offsetCheck($offset); |
| 89 | return array_key_exists($offset, $this->internal); |
| 90 | } |
| 91 | |
| 92 | |
| 93 | /** |
| 94 | * @return T |
| 95 | */ |
| 96 | public function offsetGet(mixed $offset): mixed |
| 97 | { |
| 98 | $offset = $this->offsetCheck($offset); |
| 99 | if (!array_key_exists($offset, $this->internal)) { |
| 100 | throw new IndexNotFoundException($offset); |
| 101 | } |
| 102 | return $this->internal[$offset]; |
| 103 | } |
| 104 | |
| 105 | protected function getType(): ReflectionType |
| 106 | { |
| 107 | $currentClass = static::class; |
| 108 | if (!isset(self::$typeMapping[$currentClass])) { |
| 109 | self::$typeMapping[$currentClass] = (new ReflectionClass($currentClass))->getMethod('offsetGet')->getReturnType(); |
| 110 | } |
| 111 | return self::$typeMapping[$currentClass]; |
| 112 | } |
| 113 | |
| 114 | protected function offsetCheck(mixed $value): string |
| 115 | { |
| 116 | if (is_array($value)) { |
| 117 | return 'array,' . json_encode($value); |
| 118 | } |
| 119 | try { |
| 120 | return get_debug_type($value) . ',' . Utils::toString($value); |
| 121 | } catch (Throwable) { |
| 122 | if ($value instanceof EntityInterface) { |
| 123 | return get_debug_type($value) . ',' . $value->getId()->toNative(); |
| 124 | } |
| 125 | return get_debug_type($value) . ',' . spl_object_hash($value); |
| 126 | } |
| 127 | } |
| 128 | |
| 129 | protected function typeCheck(mixed $value): void |
| 130 | { |
| 131 | if (static::class === ItemSet::class) { |
| 132 | return; |
| 133 | } |
| 134 | $type = $this->getType(); |
| 135 | if (!TypeUtils::matchesType($type, $value)) { |
| 136 | throw new InvalidTypeException($value, $type->__toString()); |
| 137 | } |
| 138 | } |
| 139 | |
| 140 | /** |
| 141 | * @return self<T> |
| 142 | */ |
| 143 | public function append(mixed $value): self |
| 144 | { |
| 145 | $this->typeCheck($value); |
| 146 | $returnValue = $this->mutable ? $this : clone $this; |
| 147 | $returnValue->internal[$returnValue->offsetCheck($value)] = $value; |
| 148 | |
| 149 | return $returnValue; |
| 150 | } |
| 151 | |
| 152 | /** |
| 153 | * @param T $value |
| 154 | */ |
| 155 | public function offsetSet(mixed $offset, mixed $value): void |
| 156 | { |
| 157 | if (!$this->mutable) { |
| 158 | throw new ObjectIsImmutable($this); |
| 159 | } |
| 160 | $offset = $this->offsetCheck($value); |
| 161 | $this->typeCheck($value); |
| 162 | $this->internal[$offset] = $value; |
| 163 | } |
| 164 | |
| 165 | public function offsetUnset(mixed $offset): void |
| 166 | { |
| 167 | if (!$this->mutable) { |
| 168 | throw new ObjectIsImmutable($this); |
| 169 | } |
| 170 | $offset = $this->offsetCheck($offset); |
| 171 | if (!array_key_exists($offset, $this->internal)) { |
| 172 | throw new IndexNotFoundException($offset); |
| 173 | } |
| 174 | unset($this->internal[$offset]); |
| 175 | } |
| 176 | |
| 177 | public function clone(): static |
| 178 | { |
| 179 | return new static($this->internal); |
| 180 | } |
| 181 | } |