-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMatrixcode.php
287 lines (260 loc) · 9.85 KB
/
Matrixcode.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
<?php
/**
* Zend Framework
*
* LICENSE
*
* This source file is subject to the new BSD license that is bundled
* with this package in the file LICENSE.txt.
* It is also available through the world-wide-web at this URL:
* http://framework.zend.com/license/new-bsd
* If you did not receive a copy of the license and are unable to
* obtain it through the world-wide-web, please send an email
* to [email protected] so we can send you a copy immediately.
*
* @category Zend
* @package Zend_Matrixcode
* @copyright Copyright (c) 2009-2011 Peter Minne <[email protected]>
* @license http://framework.zend.com/license/new-bsd New BSD License
*/
/**
* Class for generating matrix codes (2 dimensional scan codes)
*
* @category Zend
* @package Zend_Matrixcode
* @copyright Copyright (c) 2009-2011 Peter Minne <[email protected]>
* @license http://framework.zend.com/license/new-bsd New BSD License
*/
class Zend_Matrixcode
{
/**
* Factory for Zend_Matrixcode_Abstract classes.
*
* First argument should be a string containing the base of the adapter class
* name, e.g. 'qrcode' corresponds to class Zend_Matrixcode_Qrcode. This
* is case-insensitive.
*
* First argument may alternatively be an object of type Zend_Config.
* The matrixcode class base name is read from the 'matrixcode' property.
* The matrixcode config parameters are read from the 'params' property.
*
* Second argument is optional and may be an associative array of key-value
* pairs. This is used as the argument to the matrixcode constructor.
*
* If the first argument is of type Zend_Config and contains a 'params' key, it is assumed to contain
* all parameters, and the second argument is ignored.
*
* The third parameter specifies the type of renderer, where the fourth parameter is an array or
* Zend_Config object containing the renderer parameters
*
* @param string | array | Zend_Config $matrixcode
* @param array | Zend_Config $matrixcodeConfig
* @param string $renderer OPTIONAL
* @param array | Zend_Config $rendererConfig OPTIONAL
* @return Zend_Matrixcode_Abstract
* @throws Zend_Matrixcode_Exception
*/
public static function factory (
$matrixcode,
$matrixcodeConfig = array(),
$renderer = 'image',
$rendererConfig = array()
) {
/*
* Convert Zend_Config argument to plain string
* matrixcode name and separate config object.
*/
if ($matrixcode instanceof Zend_Config) {
if (isset($matrixcode->rendererParams)) {
$rendererConfig = $matrixcode->rendererParams->toArray();
}
if (isset($matrixcode->renderer)) {
$renderer = (string) $matrixcode->renderer;
}
if (isset($matrixcode->matrixcodeParams)) {
$matrixcodeConfig = $matrixcode->matrixcodeParams->toArray();
}
if (isset($matrixcode->matrixcode)) {
$matrixcode = (string) $matrixcode->matrixcode;
} else {
$matrixcode = null;
}
}
try {
$matrixcode = self::getMatrixcode($matrixcode, $matrixcodeConfig);
$renderer = self::getRenderer($renderer, $rendererConfig);
} catch (Zend_Exception $e) {
throw $e;
}
$renderer->setMatrixcode($matrixcode);
return $renderer;
}
/**
* Matrixcode Constructor
*
* @param mixed $matrixcode String name of matrixcode class, or Zend_Config object.
* @param mixed $matrixcodeConfig OPTIONAL; an array or Zend_Config object with matrixcode parameters
* @return Zend_Matrixcode_Abstract
*/
public static function getMatrixcode($matrixcode, $matrixcodeConfig = array())
{
/*
* Convert Zend_Config argument to plain string
* matrixcode name and separate config object.
*/
if ($matrixcode instanceof Zend_Config) {
if (isset($matrixcode->matrixcodeParams) && $matrixcode->matrixcodeParams instanceof Zend_Config) {
$matrixcodeConfig = $matrixcode->matrixcodeParams->toArray();
}
if (isset($matrixcode->matrixcode)) {
$matrixcode = (string) $matrixcode->matrixcode;
} else {
$matrixcode = null;
}
}
if ($matrixcodeConfig instanceof Zend_Config) {
$matrixcodeConfig = $matrixcodeConfig->toArray();
}
/*
* Verify that matrixcode parameters are in an array.
*/
if (!is_array($matrixcodeConfig)) {
require_once 'Zend/Matrixcode/Exception.php';
throw new Zend_Matrixcode_Exception(
'Matrixcode parameters must be in an array or a Zend_Config object'
);
}
/*
* Verify that a matrixcode name has been specified.
*/
if (!is_string($matrixcode) || empty($matrixcode)) {
require_once 'Zend/Matrixcode/Exception.php';
throw new Zend_Matrixcode_Exception(
'Matrixcode name must be specified in a string'
);
}
/*
* Form full matrixcode class name
*/
$matrixcodeNamespace = 'Zend_Matrixcode';
if (isset($matrixcodeConfig['matrixcodeNamespace'])) {
$matrixcodeNamespace = $matrixcodeConfig['matrixcodeNamespace'];
}
$matrixcodeName = strtolower($matrixcodeNamespace . '_' . $matrixcode);
$matrixcodeName = str_replace(' ', '_', ucwords(str_replace('_', ' ', $matrixcodeName)));
/*
* Load the matrixcode class.
*/
if (!class_exists($matrixcodeName)) {
require_once 'Zend/Loader.php';
Zend_Loader::loadClass($matrixcodeName);
}
/*
* Create an instance of the matrixcode class.
*/
$mcAdapter = new $matrixcodeName($matrixcodeConfig);
/*
* Verify that the object created is a descendent of the abstract matrixcode type.
*/
if (!$mcAdapter instanceof Zend_Matrixcode_Abstract) {
require_once 'Zend/Matrixcode/Exception.php';
throw new Zend_Matrixcode_Exception(
"Matrixcode class '$matrixcodeName' does not extend Zend_Matrixcode_Abstract"
);
}
return $mcAdapter;
}
/**
* Renderer Constructor
*
* @param mixed $renderer String name of renderer class, or Zend_Config object.
* @param mixed $rendererConfig OPTIONAL; an array or Zend_Config object with renderer parameters.
* @return Zend_Matrixcode_Renderer_Abstract
*/
public static function getRenderer($renderer = 'image', $rendererConfig = array())
{
/*
* Convert Zend_Config argument to plain string
* renderer name and separate config object.
*/
if ($renderer instanceof Zend_Config) {
if (isset($renderer->rendererParams)) {
$rendererConfig = $renderer->rendererParams->toArray();
}
if (isset($renderer->renderer)) {
$renderer = (string) $renderer->renderer;
}
}
if ($rendererConfig instanceof Zend_Config) {
$rendererConfig = $rendererConfig->toArray();
}
/*
* Verify that renderer parameters are in an array.
*/
if (!is_array($rendererConfig)) {
require_once 'Zend/Matrixcode/Exception.php';
$e = new Zend_Matrixcode_Exception(
'Renderer parameters must be in an array or a Zend_Config object'
);
throw $e;
}
/*
* Verify that a renderer name has been specified.
*/
if (!is_string($renderer) || empty($renderer)) {
require_once 'Zend/Matrixcode/Exception.php';
$e = new Zend_Matrixcode_Exception(
'Renderer name must be specified in a string'
);
throw $e;
}
/*
* Form full renderer class name
*/
$rendererNamespace = 'Zend_Matrixcode_Renderer';
if (isset($rendererConfig['rendererNamespace'])) {
$rendererNamespace = $rendererConfig['rendererNamespace'];
}
$rendererName = strtolower($rendererNamespace . '_' . $renderer);
$rendererName = str_replace(' ', '_', ucwords(str_replace( '_', ' ', $rendererName)));
/*
* Load the renderer class.
*/
if (!class_exists($rendererName)) {
require_once 'Zend/Loader.php';
Zend_Loader::loadClass($rendererName);
}
/*
* Create an instance of the renderer class.
*/
$rdrAdapter = new $rendererName($rendererConfig);
/*
* Verify that the object created is a descendent of the abstract renderer type.
*/
if (!$rdrAdapter instanceof Zend_Matrixcode_Renderer_Abstract) {
require_once 'Zend/Matrixcode/Exception.php';
$e = new Zend_Matrixcode_Exception(
"Renderer class '$rendererName' does not extend Zend_Matrixcode_Renderer_Abstract"
);
throw $e;
}
return $rdrAdapter;
}
/**
* Proxy to renderer render() method
*
* @param string | array | Zend_Config $matrixcode
* @param array | Zend_Config $matrixcodeConfig
* @param string $renderer OPTIONAL
* @param array | Zend_Config $rendererConfig OPTIONAL
* @return mixed
*/
public static function render (
$matrixcode,
$matrixcodeConfig = array(),
$renderer = 'image',
$rendererConfig = array()
) {
return self::factory ($matrixcode, $matrixcodeConfig, $renderer, $rendererConfig)->render();
}
}