Passed
Branch development (176841)
by Elk
15:26
created

ValuesContainer::toArray()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 0
dl 0
loc 3
ccs 0
cts 0
cp 0
crap 2
rs 10
c 0
b 0
f 0
1
<?php
2
3
/**
4
 * Just a class to implement ArrayAccess and getter/setter and stuff like that.
5
 *
6
 * @package   ElkArte Forum
7
 * @copyright ElkArte Forum contributors
8
 * @license   BSD http://opensource.org/licenses/BSD-3-Clause (see accompanying LICENSE.txt file)
9
 *
10
 * @version 2.0 dev
11
 *
12
 */
13
14
namespace ElkArte;
15
16
/**
17
 * Class ValuesContainer
18
 *
19
 * - Implements generic ArrayAccess and getter/setter bag for use
20
 *
21
 * @package ElkArte
22
 */
23
class ValuesContainer implements \ArrayAccess
24
{
25
	/**
26
	 * The array that holds all the data collected by the object.
27
	 *
28
	 * @var mixed[]
29
	 */
30
	protected $data = array();
31
32
	/**
33
	 * Constructor
34
	 *
35
	 * @param mixed[]|null $data Any array of data used to initialize the object (optional)
36
	 */
37 85
	public function __construct($data = null)
38
	{
39 85
		if ($data !== null)
40
		{
41 75
			$this->data = (array) $data;
42
		}
43 85
	}
44
45
	/**
46
	 * Setter
47
	 *
48
	 * @param string|int $key
49
	 * @param mixed $val
50
	 */
51 17
	public function __set($key, $val)
52
	{
53 17
		$this->data[$key] = $val;
54 17
	}
55
56
	/**
57
	 * Getter
58
	 *
59
	 * @param string|int $key
60
	 * @return mixed
61
	 */
62 81
	public function __get($key)
63
	{
64 81
		if (isset($this->data[$key]))
65 73
			return $this->data[$key];
66
		else
67 36
			return null;
68
	}
69
70
	/**
71
	 * Returns the value of the key, or the default if empty
72
	 *
73
	 * @param string|int $key
74
	 * @param mixed $default
75
	 * @return mixed
76
	 */
77 3
	public function getEmpty($key, $default)
78
	{
79 3
		if (!empty($this->data[$key]))
80
		{
81
			return $this->data[$key];
82
		}
83
		else
84
		{
85 3
			return $default;
86
		}
87
	}
88
89
	/**
90
	 * Calling as method uses the argument as default
91
	 *
92
	 * @param string|int $key
93
	 * @param mixed[] $args
94
	 * @return mixed
95
	 */
96 3
	public function __call($key, $args)
97
	{
98 3
		if (isset($this->data[$key]))
99
		{
100 2
			return $this->data[$key];
101
		}
102
		else
103
		{
104 1
			if (isset($args[0]))
105
			{
106 1
				return $args[0];
107
			}
108
			else
109
			{
110
				return null;
111
			}
112
		}
113
	}
114
115
	/**
116
	 * Tests if the key is set.
117
	 *
118
	 * @param string|int $key
119
	 * @return bool
120
	 */
121 89
	public function __isset($key)
122
	{
123 89
		return isset($this->data[$key]);
124
	}
125
126
	/**
127
	 * Assigns a value to a certain offset.
128
	 *
129
	 * @param mixed|mixed[] $offset
130
	 * @param string $value
131
	 */
132 67
	public function offsetSet($offset, $value)
133
	{
134 67
		if (is_null($offset))
135
		{
136
			$this->data[] = $value;
137
		}
138
		else
139
		{
140 67
			$this->data[$offset] = $value;
141
		}
142 67
	}
143
144
	/**
145
	 * Tests if an offset key is set.
146
	 *
147
	 * @param string|int $offset
148
	 * @return bool
149
	 */
150 54
	public function offsetExists($offset)
151
	{
152 54
		return isset($this->data[$offset]);
153
	}
154
155
	/**
156
	 * Unset a certain offset key.
157
	 *
158
	 * @param string|int $offset
159
	 */
160
	public function offsetUnset($offset)
161
	{
162
		unset($this->data[$offset]);
163
	}
164
165
	/**
166
	 * Returns the value associated to a certain offset.
167
	 *
168
	 * @param string|int $offset
169
	 * @return mixed|mixed[]
170
	 */
171 98
	public function offsetGet($offset)
172
	{
173 98
		return isset($this->data[$offset]) ? $this->data[$offset] : null;
174
	}
175
176
	/**
177
	 * Returns if the data array is empty or not
178
	 *
179
	 * @return bool
180
	 */
181 2
	public function isEmpty()
182
	{
183 2
		return empty($this->data);
184
	}
185
186
	/**
187
	 * Merges the passed array into the existing one.
188
	 * Works the same as array_merge.
189
	 *
190
	 * @param mixed[] $new_data
191
	 */
192
	public function mergeWith($new_data)
193
	{
194
		$this->data = array_merge($this->data, $new_data);
195
	}
196
197
	/**
198
	 * Returns the data of the user in an array format
199
	 *
200
	 * @return mixed[]
201
	 */
202
	public function toArray()
203
	{
204
		return $this->data;
205
	}
206
}
207