1
|
|
|
<?php |
2
|
|
|
/* |
3
|
|
|
+-----------------------------------------------------------------------------+ |
4
|
|
|
| PHPPackage - Magic Class |
5
|
|
|
+-----------------------------------------------------------------------------+ |
6
|
|
|
| Copyright (c)2018 (http://github.com/phppackage/magicclass) |
7
|
|
|
+-----------------------------------------------------------------------------+ |
8
|
|
|
| This source file is subject to MIT License |
9
|
|
|
| that is bundled with this package in the file LICENSE. |
10
|
|
|
| |
11
|
|
|
| If you did not receive a copy of the license and are unable to |
12
|
|
|
| obtain it through the world-wide-web, please send an email |
13
|
|
|
| to [email protected] so we can send you a copy immediately. |
14
|
|
|
+-----------------------------------------------------------------------------+ |
15
|
|
|
| Authors: |
16
|
|
|
| Lawrence Cherone <[email protected]> |
17
|
|
|
+-----------------------------------------------------------------------------+ |
18
|
|
|
*/ |
19
|
|
|
|
20
|
|
|
namespace PHPPackage; |
21
|
|
|
|
22
|
|
|
use ArrayAccess; |
23
|
|
|
use ArrayObject; |
24
|
|
|
use Countable; |
25
|
|
|
|
26
|
|
|
class MagicClass implements ArrayAccess, Countable |
27
|
|
|
{ |
28
|
|
|
/** |
29
|
|
|
* @var ArrayObject |
30
|
|
|
*/ |
31
|
|
|
private $storage; |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* @param array $arguments |
35
|
|
|
*/ |
36
|
12 |
|
public function __construct(...$arguments) |
37
|
|
|
{ |
38
|
12 |
|
$this->storage = new ArrayObject($arguments); |
39
|
12 |
|
$this->storage->setFlags( |
40
|
12 |
|
ArrayObject::STD_PROP_LIST | ArrayObject::ARRAY_AS_PROPS |
41
|
|
|
); |
42
|
12 |
|
} |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* ArrayAccess offsetGet (getter). |
46
|
|
|
* |
47
|
|
|
* @param string $index |
48
|
|
|
*/ |
49
|
3 |
|
public function offsetGet($index) |
50
|
|
|
{ |
51
|
3 |
|
return isset($this->storage->{$index}) ? $this->storage->{$index} : null; |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* ArrayAccess offsetSet (setter). |
56
|
|
|
* |
57
|
|
|
* @param string $index |
58
|
|
|
* @param mixed $value |
59
|
|
|
*/ |
60
|
10 |
|
public function offsetSet($index, $value) |
61
|
|
|
{ |
62
|
10 |
|
if (is_null($index)) { |
63
|
2 |
|
$this->storage[] = $value; |
64
|
|
|
} else { |
65
|
10 |
|
$this->storage->{$index} = $value; |
66
|
|
|
} |
67
|
10 |
|
} |
68
|
|
|
|
69
|
|
|
/** |
70
|
|
|
* ArrayAccess offsetExists (isset). |
71
|
|
|
* |
72
|
|
|
* @param string $index |
73
|
|
|
*/ |
74
|
4 |
|
public function offsetExists($index) |
75
|
|
|
{ |
76
|
4 |
|
return isset($this->storage->{$index}); |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
/** |
80
|
|
|
* ArrayAccess offsetUnset (unset). |
81
|
|
|
* |
82
|
|
|
* @param string $index |
83
|
|
|
*/ |
84
|
2 |
|
public function offsetUnset($index) |
85
|
|
|
{ |
86
|
2 |
|
unset($this->storage->{$index}); |
87
|
2 |
|
} |
88
|
|
|
|
89
|
|
|
/** |
90
|
|
|
* Magic method (getter). |
91
|
|
|
* |
92
|
|
|
* @param string $index |
93
|
|
|
*/ |
94
|
2 |
|
public function __get($index) |
95
|
|
|
{ |
96
|
2 |
|
return $this->offsetGet($index); |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
/** |
100
|
|
|
* Magic method (setter). |
101
|
|
|
* |
102
|
|
|
* @param string $index |
103
|
|
|
* @param mixed $value |
104
|
|
|
*/ |
105
|
7 |
|
public function __set($index, $value) |
106
|
|
|
{ |
107
|
7 |
|
return $this->offsetSet($index, $value); |
|
|
|
|
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
/** |
111
|
|
|
* Magic method (isset). |
112
|
|
|
* |
113
|
|
|
* @param string $index |
114
|
|
|
*/ |
115
|
2 |
|
public function __isset($index) |
116
|
|
|
{ |
117
|
2 |
|
return $this->offsetExists($index); |
118
|
|
|
} |
119
|
|
|
|
120
|
|
|
/** |
121
|
|
|
* Magic method (unset). |
122
|
|
|
* |
123
|
|
|
* @param string $index |
124
|
|
|
*/ |
125
|
1 |
|
public function __unset($index) |
126
|
|
|
{ |
127
|
1 |
|
return $this->offsetUnset($index); |
|
|
|
|
128
|
|
|
} |
129
|
|
|
|
130
|
|
|
/** |
131
|
|
|
* Magic method (as function invoker). |
132
|
|
|
* |
133
|
|
|
* @param mixed $arguments |
134
|
|
|
*/ |
135
|
1 |
|
public function __invoke(...$arguments) |
136
|
|
|
{ |
137
|
1 |
|
if (isset($this->storage->{$arguments[0]})) { |
138
|
1 |
|
return $this->storage->{$arguments[0]}; |
139
|
|
|
} |
140
|
1 |
|
} |
141
|
|
|
|
142
|
|
|
/** |
143
|
|
|
* Magic method (toString well json). |
144
|
|
|
*/ |
145
|
1 |
|
public function __toString() |
146
|
|
|
{ |
147
|
1 |
|
$return = []; |
148
|
1 |
|
foreach ($this->storage as $key => $value) { |
149
|
1 |
|
$return[$key] = $value; |
150
|
|
|
} |
151
|
1 |
|
return json_encode($return, JSON_PRETTY_PRINT); |
152
|
|
|
} |
153
|
|
|
|
154
|
|
|
/** |
155
|
|
|
* Magic method (override print_r/var_dump). |
156
|
|
|
*/ |
157
|
1 |
|
public function __debugInfo() |
158
|
|
|
{ |
159
|
1 |
|
$return = []; |
160
|
1 |
|
foreach ($this->storage as $key => $value) { |
161
|
1 |
|
$return[$key] = $value; |
162
|
|
|
} |
163
|
1 |
|
return $return; |
164
|
|
|
} |
165
|
|
|
|
166
|
|
|
/** |
167
|
|
|
* Implements Countable |
168
|
|
|
*/ |
169
|
1 |
|
public function count() |
170
|
|
|
{ |
171
|
1 |
|
return $this->storage->count(); |
172
|
|
|
} |
173
|
|
|
} |
174
|
|
|
|
This check looks for function or method calls that always return null and whose return value is used.
The method
getObject()
can return nothing but null, so it makes no sense to use the return value.The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.