Completed
Push — master ( 5cf8b4...adaec5 )
by Ítalo
22:16
created

Set::offsetExists()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 1
Bugs 1 Features 0
Metric Value
c 1
b 1
f 0
dl 0
loc 4
ccs 0
cts 4
cp 0
rs 10
cc 2
eloc 2
nc 2
nop 1
crap 6
1
<?php
2
3
namespace Collections;
4
5
use Collections\Iterator\SetIterator;
6
use Collections\Traits\GuardTrait;
7
use Collections\Traits\StrictKeyedIterableTrait;
8
9
class Set extends AbstractCollectionArray implements SetInterface, \ArrayAccess
10
{
11
    use GuardTrait,
12
        StrictKeyedIterableTrait;
13
14
    public function at($k)
15
    {
16
        return $this[$k];
17
    }
18
19
    /**
20
     * @inheritDoc
21
     */
22
    public function get($key)
23
    {
24
        $this->validateKeyBounds($key);
25
26
        return $this->container[$key];
27
    }
28
29
    /**
30
     * @inheritDoc
31
     */
32
    public function containsKey($key)
33
    {
34
        return array_key_exists($key, $this->container);
35
    }
36
37
    /**
38
     * @inheritDoc
39
     */
40
    public function contains($item)
41
    {
42
        return in_array($item, $this->container, true);
43
    }
44
45
    /**
46
     * @inheritDoc
47
     */
48
    public function set($key, $value)
49
    {
50
        $this->container[$key] = $value;
51
52
        return $this;
53
    }
54
55
    /**
56
     * @inheritDoc
57
     */
58
    public function addAll($items)
59
    {
60
        if (!is_array($items) && !$items instanceof \Traversable) {
61
            throw new \InvalidArgumentException('The items must be an array or Traversable');
62
        }
63
64
        foreach ($items as $value) {
65
            $this->add($value);
66
        }
67
    }
68
69
    /**
70
     * {@inheritdoc}
71
     */
72
    public function remove($element)
73
    {
74
        $key = array_search($element, $this->container, true);
75
        $this->removeKey($key);
76
77
        return $this;
78
    }
79
80
    /**
81
     * {@inheritdoc}
82
     */
83
    public function removeKey($key)
84
    {
85
        unset($this->container[$key]);
86
87
        return $this;
88
    }
89
90
    /**
91
     * @inheritDoc
92
     */
93
    public function getIterator()
94
    {
95
        return new SetIterator($this->container);
96
    }
97
98
    /**
99
     * @inheritDoc
100
     */
101
    public function add($item)
102
    {
103
        $this->container[] = $item;
104
105
        return $this;
106
    }
107
108
    /**
109
     * @inheritDoc
110
     */
111
    public function removeAll(Iterable $iterable)
112
    {
113
        $iterable->each(function ($item) {
114
            if ($this->contains($item)) {
115
                $this->remove($item);
116
            }
117
        });
118
119
        return $this;
120
    }
121
122
    /**
123
     * {@inheritdoc}
124
     */
125
    public function offsetExists($offset)
126
    {
127
        return $this->containsKey($offset) && $this->at($offset) !== null;
128
    }
129
130
    /**
131
     * {@inheritdoc}
132
     */
133
    public function offsetGet($offset)
134
    {
135
        return $this->get($offset);
136
    }
137
138
    /**
139
     * {@inheritdoc}
140
     */
141
    public function offsetSet($offset, $value)
142
    {
143
        if (is_null($offset)) {
144
            $this->add($value);
145
        } else {
146
            $this->set($offset, $value);
147
        }
148
    }
149
150
    /**
151
     * {@inheritdoc}
152
     */
153
    public function offsetUnset($offset)
154
    {
155
        throw new \RuntimeException(
156
            'Cannot unset an element of a ' . get_class($this));
157
    }
158
}
159