Code Coverage |
||||||||||||||||
Lines |
Branches |
Paths |
Functions and Methods |
Classes and Traits |
||||||||||||
| Total | |
83.33% |
10 / 12 |
|
84.62% |
11 / 13 |
|
70.00% |
7 / 10 |
|
66.67% |
4 / 6 |
CRAP | |
0.00% |
0 / 1 |
| Collection | |
83.33% |
10 / 12 |
|
84.62% |
11 / 13 |
|
70.00% |
7 / 10 |
|
66.67% |
4 / 6 |
11.19 | |
0.00% |
0 / 1 |
| getIterator | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| __construct | |
80.00% |
4 / 5 |
|
83.33% |
5 / 6 |
|
50.00% |
2 / 4 |
|
0.00% |
0 / 1 |
4.12 | |||
| add | |
100.00% |
3 / 3 |
|
100.00% |
3 / 3 |
|
100.00% |
2 / 2 |
|
100.00% |
1 / 1 |
2 | |||
| get | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| count | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| toArray | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| 1 | <?php |
| 2 | |
| 3 | namespace App\Shared\Domain\Model; |
| 4 | |
| 5 | use ArrayIterator; |
| 6 | use Countable; |
| 7 | use InvalidArgumentException; |
| 8 | use IteratorAggregate; |
| 9 | |
| 10 | /** |
| 11 | * @author Wilhelm Zwertvaegher |
| 12 | * Generic collection |
| 13 | * @template T |
| 14 | * @implements IteratorAggregate<int, T> |
| 15 | */ |
| 16 | class Collection implements IteratorAggregate, Countable |
| 17 | { |
| 18 | |
| 19 | private string $className; |
| 20 | |
| 21 | /** @var T[] */ |
| 22 | private array $elements; |
| 23 | |
| 24 | /** |
| 25 | * @return ArrayIterator<int, T> |
| 26 | */ |
| 27 | public function getIterator(): ArrayIterator |
| 28 | { |
| 29 | return new ArrayIterator($this->elements); |
| 30 | } |
| 31 | |
| 32 | /** |
| 33 | * |
| 34 | * @param class-string<T> $className |
| 35 | * @param T[] $elements |
| 36 | */ |
| 37 | public function __construct(string $className, array $elements = []) |
| 38 | { |
| 39 | foreach ($elements as $element) { |
| 40 | if (!$element instanceof $className) { |
| 41 | throw new InvalidArgumentException(sprintf('All elements must be %s', $className)); |
| 42 | } |
| 43 | } |
| 44 | $this->className = $className; |
| 45 | $this->elements = $elements; |
| 46 | } |
| 47 | |
| 48 | |
| 49 | /** |
| 50 | * @param T $element |
| 51 | * @return void |
| 52 | */ |
| 53 | public function add($element): void |
| 54 | { |
| 55 | if (!$element instanceof $this->className) { |
| 56 | throw new InvalidArgumentException(sprintf('All elements must be %s', $this->className)); |
| 57 | } |
| 58 | $this->elements[] = $element; |
| 59 | } |
| 60 | |
| 61 | /** |
| 62 | * @return T|null |
| 63 | */ |
| 64 | public function get(int $index) |
| 65 | { |
| 66 | return $this->elements[$index] ?? null; |
| 67 | } |
| 68 | |
| 69 | public function count(): int |
| 70 | { |
| 71 | // TODO: Implement count() method. |
| 72 | return count($this->elements); |
| 73 | } |
| 74 | |
| 75 | /** |
| 76 | * @return T[] |
| 77 | */ |
| 78 | public function toArray(): array |
| 79 | { |
| 80 | return $this->elements; |
| 81 | } |
| 82 | } |