|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/* |
|
4
|
|
|
* This file is part of the 2amigos/yii2-qrcode-component project. |
|
5
|
|
|
* |
|
6
|
|
|
* (c) 2amigOS! <http://2amigos.us/> |
|
7
|
|
|
* |
|
8
|
|
|
* For the full copyright and license information, please view |
|
9
|
|
|
* the LICENSE file that was distributed with this source code. |
|
10
|
|
|
*/ |
|
11
|
|
|
|
|
12
|
|
|
namespace Da\QrCode\Format; |
|
13
|
|
|
|
|
14
|
|
|
use Da\QrCode\Contracts\FormatInterface; |
|
15
|
|
|
use Da\QrCode\Exception\InvalidCallException; |
|
16
|
|
|
use Da\QrCode\Exception\UnknownMethodException; |
|
17
|
|
|
use Da\QrCode\Exception\UnknownPropertyException; |
|
18
|
|
|
|
|
19
|
|
|
/** |
|
20
|
|
|
* Abstract Class FormatAbstract for all formats |
|
21
|
|
|
* |
|
22
|
|
|
* @author Antonio Ramirez <[email protected]> |
|
23
|
|
|
* @link http://www.ramirezcobos.com/ |
|
24
|
|
|
* @link http://www.2amigos.us/ |
|
25
|
|
|
* @package Da\QrCode\Format |
|
26
|
|
|
*/ |
|
27
|
|
|
abstract class AbstractFormat implements FormatInterface |
|
28
|
|
|
{ |
|
29
|
|
|
/** |
|
30
|
|
|
* Constructor. |
|
31
|
|
|
* The default implementation does two things: |
|
32
|
|
|
* |
|
33
|
|
|
* - Initializes the object with the given configuration `$config`. |
|
34
|
|
|
* - Call [[init()]]. |
|
35
|
|
|
* |
|
36
|
|
|
* If this method is overridden in a child class, it is recommended that |
|
37
|
|
|
* |
|
38
|
|
|
* - the last parameter of the constructor is a configuration array, like `$config` here. |
|
39
|
|
|
* - call the parent implementation at the end of the constructor. |
|
40
|
|
|
* |
|
41
|
|
|
* @param array $config name-value pairs that will be used to initialize the object properties |
|
42
|
|
|
*/ |
|
43
|
|
|
public function __construct($config = []) |
|
44
|
|
|
{ |
|
45
|
|
|
if (!empty($config)) { |
|
46
|
|
|
foreach ($config as $name => $value) { |
|
47
|
|
|
$this->$name = $value; |
|
48
|
|
|
} |
|
49
|
|
|
} |
|
50
|
|
|
$this->init(); |
|
51
|
|
|
} |
|
52
|
|
|
|
|
53
|
|
|
/** |
|
54
|
|
|
* Initialization method |
|
55
|
|
|
*/ |
|
56
|
|
|
public function init() |
|
57
|
|
|
{ |
|
58
|
|
|
} |
|
59
|
|
|
|
|
60
|
|
|
/** |
|
61
|
|
|
* Returns the value of an object property. |
|
62
|
|
|
* |
|
63
|
|
|
* Do not call this method directly as it is a PHP magic method that |
|
64
|
|
|
* will be implicitly called when executing `$value = $object->property;`. |
|
65
|
|
|
* |
|
66
|
|
|
* @param string $name the property name |
|
67
|
|
|
* |
|
68
|
|
|
* @throws UnknownPropertyException if the property is not defined |
|
69
|
|
|
* @throws InvalidCallException if the property is write-only |
|
70
|
|
|
* @return mixed the property value |
|
71
|
|
|
* @see __set() |
|
72
|
|
|
*/ |
|
73
|
|
|
public function __get($name) |
|
74
|
|
|
{ |
|
75
|
|
|
$getter = 'get' . $name; |
|
76
|
|
|
if (method_exists($this, $getter)) { |
|
77
|
|
|
return $this->$getter(); |
|
78
|
|
|
} elseif (method_exists($this, 'set' . $name)) { |
|
79
|
|
|
throw new InvalidCallException('Getting write-only property: ' . get_class($this) . '::' . $name); |
|
80
|
|
|
} |
|
81
|
|
|
throw new UnknownPropertyException('Getting unknown property: ' . get_class($this) . '::' . $name); |
|
82
|
|
|
} |
|
83
|
|
|
|
|
84
|
|
|
/** |
|
85
|
|
|
* Sets value of an object property. |
|
86
|
|
|
* |
|
87
|
|
|
* Do not call this method directly as it is a PHP magic method that |
|
88
|
|
|
* will be implicitly called when executing `$object->property = $value;`. |
|
89
|
|
|
* |
|
90
|
|
|
* @param string $name the property name or the event name |
|
91
|
|
|
* @param mixed $value the property value |
|
92
|
|
|
* |
|
93
|
|
|
* @throws UnknownPropertyException if the property is not defined |
|
94
|
|
|
* @throws InvalidCallException if the property is read-only |
|
95
|
|
|
* @see __get() |
|
96
|
|
|
*/ |
|
97
|
|
|
public function __set($name, $value) |
|
98
|
|
|
{ |
|
99
|
|
|
$setter = 'set' . $name; |
|
100
|
|
|
if (method_exists($this, $setter)) { |
|
101
|
|
|
$this->$setter($value); |
|
102
|
|
|
} elseif (method_exists($this, 'get' . $name)) { |
|
103
|
|
|
throw new InvalidCallException('Setting read-only property: ' . get_class($this) . '::' . $name); |
|
104
|
|
|
} else { |
|
105
|
|
|
throw new UnknownPropertyException('Setting unknown property: ' . get_class($this) . '::' . $name); |
|
106
|
|
|
} |
|
107
|
|
|
} |
|
108
|
|
|
|
|
109
|
|
|
/** |
|
110
|
|
|
* Checks if a property is set, i.e. defined and not null. |
|
111
|
|
|
* |
|
112
|
|
|
* Do not call this method directly as it is a PHP magic method that |
|
113
|
|
|
* will be implicitly called when executing `isset($object->property)`. |
|
114
|
|
|
* |
|
115
|
|
|
* Note that if the property is not defined, false will be returned. |
|
116
|
|
|
* |
|
117
|
|
|
* @param string $name the property name or the event name |
|
118
|
|
|
* |
|
119
|
|
|
* @return bool whether the named property is set (not null). |
|
120
|
|
|
* @see http://php.net/manual/en/function.isset.php |
|
121
|
|
|
*/ |
|
122
|
|
|
public function __isset($name) |
|
123
|
|
|
{ |
|
124
|
|
|
$getter = 'get' . $name; |
|
125
|
|
|
if (method_exists($this, $getter)) { |
|
126
|
|
|
return $this->$getter() !== null; |
|
127
|
|
|
} |
|
128
|
|
|
|
|
129
|
|
|
return false; |
|
130
|
|
|
} |
|
131
|
|
|
|
|
132
|
|
|
/** |
|
133
|
|
|
* Sets an object property to null. |
|
134
|
|
|
* |
|
135
|
|
|
* Do not call this method directly as it is a PHP magic method that |
|
136
|
|
|
* will be implicitly called when executing `unset($object->property)`. |
|
137
|
|
|
* |
|
138
|
|
|
* Note that if the property is not defined, this method will do nothing. |
|
139
|
|
|
* If the property is read-only, it will throw an exception. |
|
140
|
|
|
* |
|
141
|
|
|
* @param string $name the property name |
|
142
|
|
|
* |
|
143
|
|
|
* @throws InvalidCallException if the property is read only. |
|
144
|
|
|
* @see http://php.net/manual/en/function.unset.php |
|
145
|
|
|
*/ |
|
146
|
|
|
public function __unset($name) |
|
147
|
|
|
{ |
|
148
|
|
|
$setter = 'set' . $name; |
|
149
|
|
|
if (method_exists($this, $setter)) { |
|
150
|
|
|
$this->$setter(null); |
|
151
|
|
|
} elseif (method_exists($this, 'get' . $name)) { |
|
152
|
|
|
throw new InvalidCallException('Unsetting read-only property: ' . get_class($this) . '::' . $name); |
|
153
|
|
|
} |
|
154
|
|
|
} |
|
155
|
|
|
|
|
156
|
|
|
/** |
|
157
|
|
|
* Calls the named method which is not a class method. |
|
158
|
|
|
* |
|
159
|
|
|
* Do not call this method directly as it is a PHP magic method that |
|
160
|
|
|
* will be implicitly called when an unknown method is being invoked. |
|
161
|
|
|
* |
|
162
|
|
|
* @param string $name the method name |
|
163
|
|
|
* @param array $params method parameters |
|
164
|
|
|
* |
|
165
|
|
|
* @throws UnknownMethodException when calling unknown method |
|
166
|
|
|
* @return mixed the method return value |
|
167
|
|
|
*/ |
|
168
|
|
|
public function __call($name, $params) |
|
169
|
|
|
{ |
|
170
|
|
|
throw new UnknownMethodException('Calling unknown method: ' . get_class($this) . "::$name()"); |
|
171
|
|
|
} |
|
172
|
|
|
|
|
173
|
|
|
/** |
|
174
|
|
|
* @return string the string representation of the object |
|
175
|
|
|
*/ |
|
176
|
|
|
public function __toString() |
|
177
|
|
|
{ |
|
178
|
|
|
return $this->getText(); |
|
179
|
|
|
} |
|
180
|
|
|
|
|
181
|
|
|
/** |
|
182
|
|
|
* Returns a value indicating whether a property is defined. |
|
183
|
|
|
* A property is defined if: |
|
184
|
|
|
* |
|
185
|
|
|
* - the class has a getter or setter method associated with the specified name |
|
186
|
|
|
* (in this case, property name is case-insensitive); |
|
187
|
|
|
* - the class has a member variable with the specified name (when `$checkVars` is true); |
|
188
|
|
|
* |
|
189
|
|
|
* @param string $name the property name |
|
190
|
|
|
* @param bool $checkVars whether to treat member variables as properties |
|
191
|
|
|
* |
|
192
|
|
|
* @return bool whether the property is defined |
|
193
|
|
|
* @see canGetProperty() |
|
194
|
|
|
* @see canSetProperty() |
|
195
|
|
|
*/ |
|
196
|
|
|
public function hasProperty($name, $checkVars = true) |
|
197
|
|
|
{ |
|
198
|
|
|
return $this->canGetProperty($name, $checkVars) || $this->canSetProperty($name, false); |
|
199
|
|
|
} |
|
200
|
|
|
|
|
201
|
|
|
/** |
|
202
|
|
|
* Returns a value indicating whether a property can be read. |
|
203
|
|
|
* A property is readable if: |
|
204
|
|
|
* |
|
205
|
|
|
* - the class has a getter method associated with the specified name |
|
206
|
|
|
* (in this case, property name is case-insensitive); |
|
207
|
|
|
* - the class has a member variable with the specified name (when `$checkVars` is true); |
|
208
|
|
|
* |
|
209
|
|
|
* @param string $name the property name |
|
210
|
|
|
* @param bool $checkVars whether to treat member variables as properties |
|
211
|
|
|
* |
|
212
|
|
|
* @return bool whether the property can be read |
|
213
|
|
|
* @see canSetProperty() |
|
214
|
|
|
*/ |
|
215
|
|
|
public function canGetProperty($name, $checkVars = true) |
|
216
|
|
|
{ |
|
217
|
|
|
return method_exists($this, 'get' . $name) || $checkVars && property_exists($this, $name); |
|
218
|
|
|
} |
|
219
|
|
|
|
|
220
|
|
|
/** |
|
221
|
|
|
* Returns a value indicating whether a property can be set. |
|
222
|
|
|
* A property is writable if: |
|
223
|
|
|
* |
|
224
|
|
|
* - the class has a setter method associated with the specified name |
|
225
|
|
|
* (in this case, property name is case-insensitive); |
|
226
|
|
|
* - the class has a member variable with the specified name (when `$checkVars` is true); |
|
227
|
|
|
* |
|
228
|
|
|
* @param string $name the property name |
|
229
|
|
|
* @param bool $checkVars whether to treat member variables as properties |
|
230
|
|
|
* |
|
231
|
|
|
* @return bool whether the property can be written |
|
232
|
|
|
* @see canGetProperty() |
|
233
|
|
|
*/ |
|
234
|
|
|
public function canSetProperty($name, $checkVars = true) |
|
235
|
|
|
{ |
|
236
|
|
|
return method_exists($this, 'set' . $name) || $checkVars && property_exists($this, $name); |
|
237
|
|
|
} |
|
238
|
|
|
|
|
239
|
|
|
/** |
|
240
|
|
|
* Returns a value indicating whether a method is defined. |
|
241
|
|
|
* |
|
242
|
|
|
* The default implementation is a call to php function `method_exists()`. |
|
243
|
|
|
* You may override this method when you implemented the php magic method `__call()`. |
|
244
|
|
|
* |
|
245
|
|
|
* @param string $name the method name |
|
246
|
|
|
* |
|
247
|
|
|
* @return bool whether the method is defined |
|
248
|
|
|
*/ |
|
249
|
|
|
public function hasMethod($name) |
|
250
|
|
|
{ |
|
251
|
|
|
return method_exists($this, $name); |
|
252
|
|
|
} |
|
253
|
|
|
} |
|
254
|
|
|
|