AbstractArray   A
last analyzed

Complexity

Total Complexity 15

Size/Duplication

Total Lines 133
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 0

Importance

Changes 0
Metric Value
wmc 15
lcom 1
cbo 0
dl 0
loc 133
rs 10
c 0
b 0
f 0

12 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 8 2
A getIterator() 0 4 1
A offsetExists() 0 4 1
A offsetGet() 0 4 2
A offsetSet() 0 8 2
A offsetUnset() 0 4 1
A serialize() 0 4 1
A unserialize() 0 4 1
A count() 0 4 1
A clear() 0 4 1
A toArray() 0 4 1
A isEmpty() 0 4 1
1
<?php
2
/**
3
 * This file is part of the ramsey/collection library
4
 *
5
 * For the full copyright and license information, please view the LICENSE
6
 * file that was distributed with this source code.
7
 *
8
 * @copyright Copyright (c) Ben Ramsey <[email protected]>
9
 * @license http://opensource.org/licenses/MIT MIT
10
 * @link https://benramsey.com/projects/ramsey-collection/ Documentation
11
 * @link https://packagist.org/packages/ramsey/collection Packagist
12
 * @link https://github.com/ramsey/collection GitHub
13
 */
14
15
namespace Ramsey\Collection;
16
17
/**
18
 * This class provides an implementation of the ArrayInterface, to
19
 * minimize the effort required to implement this interface
20
 */
21
abstract class AbstractArray implements ArrayInterface
22
{
23
    /**
24
     * @var array
25
     */
26
    protected $data = [];
27
28
    /**
29
     * Constructs a new array object
30
     *
31
     * @param array $data
32
     */
33
    public function __construct(array $data = [])
34
    {
35
        // Invoke offsetSet() for each value added; in this way, sub-classes
36
        // may provide additional logic about values added to the array object.
37
        foreach ($data as $key => $value) {
38
            $this[$key] = $value;
39
        }
40
    }
41
42
    /**
43
     * Returns a new iterator from this array
44
     *
45
     * @return \ArrayIterator
46
     * @link http://php.net/manual/en/iteratoraggregate.getiterator.php
47
     */
48
    public function getIterator()
49
    {
50
        return new \ArrayIterator($this->data);
51
    }
52
53
    /**
54
     * Checks whether the specified offset exists in the array
55
     *
56
     * @param mixed $offset
57
     * @return bool
58
     * @link http://php.net/manual/en/arrayaccess.offsetexists.php
59
     */
60
    public function offsetExists($offset)
61
    {
62
        return isset($this->data[$offset]);
63
    }
64
65
    /**
66
     * Returns the value stored at the specified offset in the array
67
     *
68
     * @param mixed $offset
69
     * @return mixed
70
     * @link http://php.net/manual/en/arrayaccess.offsetget.php
71
     */
72
    public function offsetGet($offset)
73
    {
74
        return isset($this->data[$offset]) ? $this->data[$offset] : null;
75
    }
76
77
    /**
78
     * Sets the specified offset in the map with the given value
79
     *
80
     * @param mixed $offset
81
     * @param mixed $value
82
     * @throws \InvalidArgumentException
83
     * @link http://php.net/manual/en/arrayaccess.offsetset.php
84
     */
85
    public function offsetSet($offset, $value)
86
    {
87
        if ($offset === null) {
88
            $this->data[] = $value;
89
        } else {
90
            $this->data[$offset] = $value;
91
        }
92
    }
93
94
    /**
95
     * Removes the specified offset and its value from the map
96
     *
97
     * @param mixed $offset
98
     * @link http://php.net/manual/en/arrayaccess.offsetunset.php
99
     */
100
    public function offsetUnset($offset)
101
    {
102
        unset($this->data[$offset]);
103
    }
104
105
    /**
106
     * Converts this map object to a string when the object is serialized
107
     * with `serialize()`
108
     *
109
     * @return string
110
     * @link http://php.net/manual/en/class.serializable.php
111
     */
112
    public function serialize()
113
    {
114
        return serialize($this->data);
115
    }
116
117
    /**
118
     * Re-constructs the object from its serialized form
119
     *
120
     * @param string $serialized
121
     * @link http://php.net/manual/en/serializable.unserialize.php
122
     */
123
    public function unserialize($serialized)
124
    {
125
        $this->data = unserialize($serialized);
126
    }
127
128
    /**
129
     * Returns the number of elements contained in this array
130
     *
131
     * @return int
132
     * @link http://php.net/manual/en/countable.count.php
133
     */
134
    public function count()
135
    {
136
        return count($this->data);
137
    }
138
139
    public function clear()
140
    {
141
        $this->data = [];
142
    }
143
144
    public function toArray()
145
    {
146
        return (array) $this->data;
147
    }
148
149
    public function isEmpty()
150
    {
151
        return empty($this->data);
152
    }
153
}
154