ArrayCollection::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 1
dl 0
loc 4
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace Sinergi\Config;
4
5
use ArrayAccess;
6
use ArrayIterator;
7
use Countable;
8
use IteratorAggregate;
9
10
class ArrayCollection implements Countable, IteratorAggregate, ArrayAccess
11
{
12
    /**
13
     * @var array
14
     */
15
    protected $container = [];
16
17
    /**
18
     * @param array $elements
19
     */
20
    public function __construct(array $elements = array())
21
    {
22
        $this->container = $elements;
23
    }
24
25
    /**
26
     * @return array
27
     */
28
    public function toArray()
29
    {
30
        return $this->container;
31
    }
32
33
    /**
34
     * @return array
35
     */
36
    public function jsonSerialize()
37
    {
38
        return $this->container;
39
    }
40
41
    /**
42
     * @param string $offset
43
     * @return bool
44
     */
45
    public function offsetExists($offset)
46
    {
47
        return isset($this->container[$offset]) || array_key_exists($offset, $this->container);
48
    }
49
50
    /**
51
     * @param string $offset
52
     * @return mixed
53
     */
54
    public function offsetGet($offset)
55
    {
56
        return $this->get($offset);
57
    }
58
59
    /**
60
     * @param string $offset
61
     * @param mixed $value
62
     */
63
    public function offsetSet($offset, $value)
64
    {
65
        $this->set($offset, $value);
66
    }
67
68
    /**
69
     * @param string $offset
70
     * @return null
71
     */
72
    public function offsetUnset($offset)
73
    {
74
        return $this->remove($offset);
75
    }
76
77
    /**
78
     * @return int
79
     */
80
    public function count()
81
    {
82
        return count($this->container);
83
    }
84
85
    /**
86
     * @return ArrayIterator
87
     */
88
    public function getIterator()
89
    {
90
        return new ArrayIterator($this->container);
91
    }
92
93
    /**
94
     * @param string $key
95
     * @return null|mixed
96
     */
97
    public function get($key)
98
    {
99
        if (isset($this->container[$key])) {
100
            return $this->container[$key];
101
        }
102
        return null;
103
    }
104
105
    /**
106
     * @param string $key
107
     * @param mixed $value
108
     */
109
    public function set($key, $value)
110
    {
111
        $this->container[$key] = $value;
112
    }
113
114
    /**
115
     * @param mixed $value
116
     * @return bool
117
     */
118
    public function add($value)
119
    {
120
        $this->container[] = $value;
121
        return true;
122
    }
123
124
    /**
125
     * @param string $key
126
     * @return null|mixed
127
     */
128
    public function remove($key)
129
    {
130
        if (isset($this->container[$key]) || array_key_exists($key, $this->container)) {
131
            $removed = $this->container[$key];
132
            unset($this->container[$key]);
133
134
            return $removed;
135
        }
136
        return null;
137
    }
138
139
    public function removeAll()
140
    {
141
        $this->container = [];
142
    }
143
}
144