Completed
Branch dev (35fed3)
by Dennis
02:04
created

WzItemCollection::getItemsByLevel()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 9
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 7
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 9
ccs 7
cts 7
cp 1
rs 9.6666
c 0
b 0
f 0
cc 1
eloc 6
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 ArrayObject;
13
use InvalidArgumentException;
14
15
class WzItemCollection extends ArrayObject implements WzItemCollectionInterface
16
{
17
    /**
18
     *
19
     */
20 24
    public function __construct()
21
    {
22 24
        parent::__construct();
23 24
    }
24
25
    /**
26
     * @inheritdoc
27
     */
28 18
    public function add(WzItemInterface $item)
29
    {
30 18
        $this[$item->getId()] = $item;
31 18
    }
32
33
    /**
34
     * @inheritdoc
35
     */
36 3
    public function get($id)
37
    {
38 3
        return $this->has($id) ? $this[$id] : null;
39
    }
40
41
    /**
42
     * @inheritdoc
43
     */
44 3
    public function getItemsByLevel($level)
45
    {
46 3
        return array_merge(
47 1
            array_filter(
48 3
                $this->getArrayCopy(),
49 3
                function (WzItemInterface $item) use ($level) {
50 3
                    return $item->getLevel() == $level;
51 3
                }));
52
    }
53
54
    /**
55
     * @inheritdoc
56
     */
57 3
    public function has($id)
58
    {
59 3
        return isset($this[$id]);
60
    }
61
62
    /**
63
     * @inheritdoc
64
     */
65 24
    public function offsetSet($index, $newval)
66
    {
67 24
        if (!$newval instanceof WzItemInterface) {
68 3
            throw new InvalidArgumentException(sprintf('Only `%s` objects allowed.', WzItemInterface::class));
69
        }
70
71 21
        if ($index !== $newval->getId()) {
72 3
            throw new InvalidArgumentException(sprintf(
73 3
                "Key `%s` doesn't match `%s->getId()`: `%s`.",
74 1
                $index,
75 3
                WzItemInterface::class,
76 3
                $newval->getId()));
77
        }
78
79 18
        parent::offsetSet($index, $newval);
80 18
    }
81
}
82