Composite::offsetExists()   A
last analyzed

Complexity

Conditions 4
Paths 4

Size

Total Lines 14

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 7
CRAP Score 4.0312

Importance

Changes 0
Metric Value
dl 0
loc 14
ccs 7
cts 8
cp 0.875
rs 9.7998
c 0
b 0
f 0
cc 4
nc 4
nop 1
crap 4.0312
1
<?php
2
3
namespace Metabor\KeyValue;
4
5
/**
6
 * @author otischlinger
7
 */
8
class Composite implements \ArrayAccess
9
{
10
    /**
11
     * @var \SplObjectStorage
12
     */
13
    private $container;
14
15
    /**
16
     *
17
     */
18 4
    public function __construct()
19
    {
20 4
        $this->container = new \SplObjectStorage();
21 4
    }
22
23
    /**
24
     * @param \ArrayAccess $keyValue
25
     */
26 4
    public function attach(\ArrayAccess $keyValue)
27
    {
28 4
        $this->container->attach($keyValue);
29 4
    }
30
31
    /**
32
     * @param \ArrayAccess $keyValue
33
     */
34
    public function detach(\ArrayAccess $keyValue)
35
    {
36
        $this->container->detach($keyValue);
37
    }
38
39
    /**
40
     * @see ArrayAccess::offsetExists()
41
     */
42 1
    public function offsetExists($offset)
43
    {
44 1
        if ($this->container->count()) {
45 1
            $result = true;
46
            /* @var $keyValue \ArrayAccess */
47 1
            foreach ($this->container as $keyValue) {
48 1
                $result = $result && $keyValue->offsetExists($offset);
49 1
            }
50
51 1
            return $result;
52
        } else {
53
            return false;
54
        }
55
    }
56
57
    /**
58
     * @see ArrayAccess::offsetGet()
59
     */
60 2
    public function offsetGet($offset)
61
    {
62 2
        $values = array();
63
        /* @var $keyValue \ArrayAccess */
64 2
        foreach ($this->container as $keyValue) {
65 2
            $values[] = $keyValue->offsetGet($offset);
66 2
        }
67 2
        $values = array_unique($values, SORT_REGULAR);
68
69 2
        switch (count($values)) {
70 2
            case 0:
71
                return;
72 2
            case 1:
73 2
                return reset($values);
74
            default:
75
                throw new \RuntimeException('Offset "' . $offset . '" is not unique!');
76
        }
77
    }
78
79
    /**
80
     * @see ArrayAccess::offsetSet()
81
     */
82 2
    public function offsetSet($offset, $value)
83
    {
84
        /* @var $keyValue \ArrayAccess */
85 2
        foreach ($this->container as $keyValue) {
86 2
            $keyValue->offsetSet($offset, $value);
87 2
        }
88 2
    }
89
    /**
90
     * @see ArrayAccess::offsetUnset()
91
     */
92
    public function offsetUnset($offset)
93
    {
94
        /* @var $keyValue \ArrayAccess */
95
        foreach ($this->container as $keyValue) {
96
            $keyValue->offsetUnset($offset);
97
        }
98
    }
99
}
100