Completed
Pull Request — master (#93)
by Krzysztof
04:16
created

AbstractCollection::count()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 4
ccs 2
cts 2
cp 1
rs 10
cc 1
eloc 2
nc 1
nop 0
crap 1
1
<?php
2
3
namespace KGzocha\Searcher;
4
5
/**
6
 * @author Krzysztof Gzocha <[email protected]>
7
 */
8
abstract class AbstractCollection implements \Countable, \IteratorAggregate
9
{
10
    /**
11
     * @var array
12
     */
13
    private $items;
14
15
    /**
16
     * @param \Traversable|array $items
17
     */
18 48
    public function __construct($items = [])
19
    {
20 48
        $this->items = [];
21 48
        $this->isTraversable($items);
22 33
        $this->checkItems($items);
23
24 23
        $this->items = $items;
25 23
    }
26
27
    /**
28
     * @param object $item
29
     *
30
     * @return bool
31
     */
32
    abstract protected function isItemValid($item);
33
34
    /**
35
     * {@inheritdoc}
36
     */
37 1
    public function getIterator()
38
    {
39 1
        return new \ArrayIterator($this->items);
40
    }
41
42
    /**
43
     * {@inheritdoc}
44
     */
45 3
    public function count()
46
    {
47 3
        return count($this->items);
48
    }
49
50
    /**
51
     * @param mixed $item
52
     *
53
     * @return $this
54
     */
55 5
    protected function addItem($item)
56
    {
57 5
        $this->items[] = $item;
58
59 5
        return $this;
60
    }
61
62
    /**
63
     * @param string $name
64
     * @param mixed  $item
65
     *
66
     * @return $this
67
     */
68 3
    protected function addNamedItem($name, $item)
69
    {
70 3
        $this->items[$name] = $item;
71
72 3
        return $this;
73
    }
74
75
    /**
76
     * @return array
77
     */
78 8
    protected function getItems()
79
    {
80 8
        return $this->items;
81
    }
82
83
    /**
84
     * @param string $name
85
     *
86
     * @return mixed|null
87
     */
88 2
    protected function getNamedItem($name)
89
    {
90 2
        if (array_key_exists($name, $this->items)) {
91 1
            return $this->items[$name];
92
        }
93
94 1
        return null;
95
    }
96
97
    /**
98
     * @param mixed $items
99
     *
100
     * @throws \InvalidArgumentException
101
     */
102 48
    private function isTraversable($items)
103
    {
104 48
        if (!is_array($items) && !$items instanceof \Traversable) {
105 15
            throw new \InvalidArgumentException(sprintf(
106 15
                'Argument passed to collection %s needs to be an array or traversable object',
107
                get_class($this)
108
            ));
109
        }
110 33
    }
111
112
    /**
113
     * @param array|\Traversable $items
114
     *
115
     * @throws \InvalidArgumentException
116
     */
117 33
    private function checkItems($items)
118
    {
119 33
        foreach ($items as $item) {
120 17
            if ($this->isItemValid($item)) {
121 7
                continue;
122
            }
123
124 10
            throw new \InvalidArgumentException(sprintf(
125 10
                'At least one item in collection "%s" is invalid.',
126
                get_class($this)
127
            ));
128
        }
129 23
    }
130
}
131