Completed
Branch dev (ef719a)
by Dennis
02:05
created

WzClassification::has()   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 0
Metric Value
dl 0
loc 4
ccs 2
cts 2
cp 1
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 1
crap 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 24
    public function __construct(array $items)
26
    {
27 24
        foreach ($items as $item) {
28 24
            if (!$item instanceof WzItemInterface) {
29 3
                throw new InvalidArgumentException(sprintf(
30 3
                    'Only `%s` objects are allowed.',
31 3
                    WzItemInterface::class));
32
            }
33
34 21
            $this->items[$item->getId()] = $item;
35 7
        }
36 21
    }
37
38
    /**
39
     * @inheritdoc
40
     */
41 6
    public function count()
42
    {
43 6
        return count($this->items);
44
    }
45
46
    /**
47
     * @inheritdoc
48
     */
49 12
    public function get($id)
50
    {
51 12
        return $this->has($id) ? $this->items[$id] : null;
52
    }
53
54
    /**
55
     * @param int $level
56
     * @return WzItemInterface[]
57
     */
58 3
    public function getItemsByLevel($level)
59
    {
60 3
        return array_merge(
61 1
            array_filter(
62 3
                $this->items,
63 3
                function (WzItemInterface $item) use ($level) {
64 3
                    return $item->getLevel() == $level;
65 3
                }));
66
    }
67
68
    /**
69
     * @param string $id
70
     * @return bool
71
     */
72 12
    public function has($id)
73
    {
74 12
        return isset($this->items[$id]);
75
    }
76
}
77