| Total Complexity | 10 |
| Total Lines | 55 |
| Duplicated Lines | 0 % |
| Coverage | 100% |
| Changes | 0 | ||
| 1 | <?php declare(strict_types=1); |
||
| 9 | class Collection implements IteratorAggregate, Countable |
||
| 10 | { |
||
| 11 | /** |
||
| 12 | * @var array |
||
| 13 | */ |
||
| 14 | private $elements; |
||
| 15 | |||
| 16 | 47 | private function __construct(array $elements) |
|
| 17 | { |
||
| 18 | 47 | $this->elements = $elements; |
|
| 19 | } |
||
| 20 | |||
| 21 | 47 | public static function create(array $elements): Collection |
|
| 22 | { |
||
| 23 | 47 | return new self($elements); |
|
| 24 | } |
||
| 25 | |||
| 26 | 5 | public function getIterator(): Traversable |
|
| 27 | { |
||
| 28 | 5 | yield from $this->elements; |
|
| 29 | } |
||
| 30 | |||
| 31 | 22 | public function merge(Collection $other): Collection |
|
| 32 | { |
||
| 33 | 22 | return self::create(\array_merge($this->elements, $other->elements)); |
|
| 34 | } |
||
| 35 | |||
| 36 | 22 | public function filter(callable $f): Collection |
|
| 37 | { |
||
| 38 | 22 | return self::create(\array_values(\array_filter($this->elements, $f))); |
|
| 39 | } |
||
| 40 | |||
| 41 | 14 | public function map(callable $f): Collection |
|
| 42 | { |
||
| 43 | 14 | return self::create(\array_map($f, $this->elements)); |
|
| 44 | } |
||
| 45 | |||
| 46 | 3 | public function reduce($initial, callable $param) |
|
| 47 | { |
||
| 48 | 3 | return \array_reduce($this->elements, $param, $initial); |
|
| 49 | } |
||
| 50 | |||
| 51 | 25 | public function toArray(): array |
|
| 52 | { |
||
| 53 | 25 | return $this->elements; |
|
| 54 | } |
||
| 55 | |||
| 56 | 13 | public function count(): int |
|
| 59 | } |
||
| 60 | |||
| 61 | 25 | public function empty(): bool |
|
| 62 | { |
||
| 63 | 25 | return empty($this->elements); |
|
| 64 | } |
||
| 65 | } |
||
| 66 |