Completed
Push — master ( d337b0...9a8ebc )
by Dennis
01:16
created

WzClassification   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 66
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 8
lcom 1
cbo 1
dl 0
loc 66
ccs 25
cts 25
cp 1
rs 10
c 0
b 0
f 0

5 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 13 3
A count() 0 4 1
A get() 0 4 2
A getItemsByLevel() 0 11 1
A has() 0 4 1
1
<?php
2
3
/**
4
 * (c) Dennis Meckel
5
 *
6
 * For the full copyright and license information,
7
 * please view the LICENSE file that was distributed with this source code.
8
 */
9
10
namespace Rayne\wz2008\Graph;
11
12
use InvalidArgumentException;
13
14
class WzClassification implements WzClassificationInterface
15
{
16
    /**
17
     * @var WzItemInterface[]
18
     */
19
    public $items = [];
20
21
    /**
22
     * @param WzItemInterface[] $items Unordered list of `WzItemInterface` objects.
23
     * @throws InvalidArgumentException On `$items` which aren't `WzItemInterface` objects.
24
     */
25 40
    public function __construct(array $items)
26
    {
27 40
        foreach ($items as $item) {
28 40
            if (!$item instanceof WzItemInterface) {
29 5
                throw new InvalidArgumentException(sprintf(
30 5
                    'Only `%s` objects are allowed.',
31 4
                    WzItemInterface::class
32 1
                ));
33
            }
34
35 35
            $this->items[$item->getId()] = $item;
36 7
        }
37 35
    }
38
39
    /**
40
     * @inheritdoc
41
     */
42 10
    public function count()
43
    {
44 10
        return count($this->items);
45
    }
46
47
    /**
48
     * @inheritdoc
49
     */
50 20
    public function get($id)
51
    {
52 20
        return $this->has($id) ? $this->items[$id] : null;
53
    }
54
55
    /**
56
     * @param int $level
57
     * @return WzItemInterface[]
58
     */
59 5
    public function getItemsByLevel($level)
60
    {
61 5
        return array_merge(
62 5
            array_filter(
63 5
                $this->items,
64 2
                function (WzItemInterface $item) use ($level) {
65 5
                    return $item->getLevel() == $level;
66 4
                }
67 1
            )
68 1
        );
69
    }
70
71
    /**
72
     * @param string $id
73
     * @return bool
74
     */
75 20
    public function has($id)
76
    {
77 20
        return isset($this->items[$id]);
78
    }
79
}
80