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

AbstractCollection   A

Complexity

Total Complexity 14

Size/Duplication

Total Lines 123
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 0

Test Coverage

Coverage 100%

Importance

Changes 2
Bugs 1 Features 1
Metric Value
wmc 14
c 2
b 1
f 1
lcom 1
cbo 0
dl 0
loc 123
ccs 34
cts 34
cp 1
rs 10

10 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 8 1
isItemValid() 0 1 ?
A getIterator() 0 4 1
A count() 0 4 1
A addItem() 0 6 1
A addNamedItem() 0 6 1
A getItems() 0 4 1
A getNamedItem() 0 8 2
A isTraversable() 0 9 3
A checkItems() 0 13 3
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