Completed
Push — development ( 7e5391...81536e )
by Stephen
21:40 queued 06:42
created

ValuesContainer::__call()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 18
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 12

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 3
eloc 8
nc 3
nop 2
dl 0
loc 18
ccs 0
cts 6
cp 0
crap 12
rs 9.4285
c 1
b 0
f 0
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.1 Release Candidate 1
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 18
	public function __construct($data = null)
38
	{
39 18
		if ($data !== null)
40 12
		{
41 18
			$this->data = (array) $data;
42
		}
43
	}
44
45
	/**
46
	 * Setter
47
	 *
48
	 * @param string|int $key
49
	 * @param mixed $val
50
	 */
51
	public function __set($key, $val)
52
	{
53
		$this->data[$key] = $val;
54
	}
55
56
	/**
57
	 * Getter
58
	 *
59
	 * @param string|int $key
60 12
	 * @return mixed
61
	 */
62 12
	public function __get($key)
63 12
	{
64
		if (isset($this->data[$key]))
65
			return $this->data[$key];
66
		else
67
			return null;
68
	}
69
70
	/**
71
	 * Calling as method uses the argument as default
72
	 *
73
	 * @param string|int $key
74
	 * @param mixed[] $args
75
	 * @return mixed
76
	 */
77
	public function __call($key, $args)
78
	{
79
		if (isset($this->data[$key]))
80
		{
81
			return $this->data[$key];
82
		}
83
		else
84
		{
85
			if (isset($args[0]))
86
			{
87
				return $args[0];
88
			}
89
			else
90
			{
91
				return null;
92
			}
93
		}
94
	}
95
96
	/**
97
	 * Tests if the key is set.
98
	 *
99
	 * @param string|int $key
100
	 * @return bool
101
	 */
102
	public function __isset($key)
103
	{
104
		return isset($this->data[$key]);
105
	}
106
107
	/**
108
	 * Assigns a value to a certain offset.
109
	 *
110
	 * @param mixed|mixed[] $offset
111
	 * @param string $value
112
	 */
113
	public function offsetSet($offset, $value)
114
	{
115
		if (is_null($offset))
116
		{
117
			$this->data[] = $value;
118
		}
119
		else
120
		{
121
			$this->data[$offset] = $value;
122
		}
123
	}
124 12
125
	/**
126 12
	 * Tests if an offset key is set.
127
	 *
128
	 * @param string|int $offset
129
	 * @return bool
130
	 */
131
	public function offsetExists($offset)
132
	{
133
		return isset($this->data[$offset]);
134
	}
135
136
	/**
137
	 * Unset a certain offset key.
138
	 *
139
	 * @param string|int $offset
140
	 */
141
	public function offsetUnset($offset)
142
	{
143
		unset($this->data[$offset]);
144
	}
145
146
	/**
147
	 * Returns the value associated to a certain offset.
148
	 *
149
	 * @param string|int $offset
150
	 * @return mixed|mixed[]
151
	 */
152
	public function offsetGet($offset)
153
	{
154
		return isset($this->data[$offset]) ? $this->data[$offset] : null;
155
	}
156
}
157