Passed
Push — master ( 254d09...309d02 )
by Jakub
01:36
created

Collection::getIterator()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 0
dl 0
loc 3
ccs 2
cts 2
cp 1
crap 1
rs 10
c 0
b 0
f 0
1
<?php declare(strict_types=1);
2
3
namespace Zalas\Toolbox\Tool;
4
5
use Countable;
6
use IteratorAggregate;
7
use Traversable;
8
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
57
    {
58 13
        return \count($this->elements);
59
    }
60
61 25
    public function empty(): bool
62
    {
63 25
        return empty($this->elements);
64
    }
65
}
66