Completed
Pull Request — development (#2330)
by Joshua
10:25
created

ValuesContainer::__construct()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6
Metric Value
dl 0
loc 5
ccs 0
cts 5
cp 0
rs 9.4286
cc 2
eloc 3
nc 2
nop 1
crap 6
1
<?php
2
3
/**
4
 * Just a class to implement ArrayAccess and getter/setter and stuff like that.
5
 *
6
 * @name      ElkArte Forum
7
 * @copyright ElkArte Forum contributors
8
 * @license   BSD http://opensource.org/licenses/BSD-3-Clause
9
 *
10
 * @version 1.0.3
11
 *
12
 */
13
14
namespace ElkArte;
15
16
if (!defined('ELK'))
17
	die('No access...');
18
19
class ValuesContainer implements \ArrayAccess
20
{
21
	/**
22
	 * The array that holds all the data collected by the object.
23
	 *
24
	 * @var mixed[]
25
	 */
26
	protected $data = array();
27
28
	/**
29
	 * Constructor
30
	 *
31
	 * @param mixed[]|null $data Any array of data used to initialize the object (optional)
32
	 */
33
	public function __construct($data = null)
34
	{
35
		if ($data !== null)
36
			$this->data = (array) $data;
37
	}
38
39
	/**
40
	 * Setter
41
	 *
42
	 * @param string|int $key
43
	 * @param string|int|bool|null|object $val
44
	 */
45
	public function __set($key, $val)
46
	{
47
		$this->data[$key] = $val;
48
	}
49
50
	/**
51
	 * Getter
52
	 *
53
	 * @param string|int $key
54
	 * @return string|int|bool|null|object
55
	 */
56
	public function __get($key)
57
	{
58
		if (isset($this->data[$key]))
59
			return $this->data[$key];
60
		else
61
			return null;
62
	}
63
64
	/**
65
	 * Tests if the key is set.
66
	 *
67
	 * @param string|int $key
68
	 * @return bool
69
	 */
70
	public function __isset($key)
71
	{
72
		return isset($this->data[$key]);
73
	}
74
75
	/**
76
	 * Assigns a value to a certain offset.
77
	 *
78
	 * @param mixed|mixed[] $offset
79
	 */
80
	public function offsetSet($offset, $value)
81
	{
82
		if (is_null($offset))
83
		{
84
			$this->data[] = $value;
85
		}
86
		else
87
		{
88
			$this->data[$offset] = $value;
89
		}
90
	}
91
92
	/**
93
	 * Tests if an offeset key is set.
94
	 *
95
	 * @param string|int $offset
96
	 * @return bool
97
	 */
98
	public function offsetExists($offset)
99
	{
100
		return isset($this->data[$offset]);
101
	}
102
103
	/**
104
	 * Unsets a certain offeset key.
105
	 *
106
	 * @param string|int $offset
107
	 */
108
	public function offsetUnset($offset)
109
	{
110
		unset($this->data[$offset]);
111
	}
112
113
	/**
114
	 * Returns the value associated to a certain offset.
115
	 *
116
	 * @param string|int $offset
117
	 * @return mixed|mixed[]
118
	 */
119
	public function offsetGet($offset)
120
	{
121
		return isset($this->data[$offset]) ? $this->data[$offset] : null;
122
	}
123
}