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
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
<?php
/**
* @see https://github.com/zendframework/zend-barcode for the canonical source repository
* @copyright Copyright (c) 2005-2019 Zend Technologies USA Inc. (http://www.zend.com)
* @license https://github.com/zendframework/zend-barcode/blob/master/LICENSE.md New BSD License
*/
namespace Zend\Barcode;
use Traversable;
use Zend\Barcode\Renderer\RendererInterface;
use Zend\Stdlib\ArrayUtils;
use Zend\ServiceManager\ServiceManager;
/**
* Class for generate Barcode
*/
abstract class Barcode
{
/**
* Default barcode TTF font name
*
* It's used by standard barcode objects derived from
* {@link Object\AbstractObject} class
* if corresponding constructor option is not provided.
*
* @var string
*/
protected static $staticFont = null;
/**
* The parser plugin manager
*
* @var ObjectPluginManager
*/
protected static $objectPlugins;
/**
* The renderer plugin manager
*
* @var RendererPluginManager
*/
protected static $rendererPlugins;
/**
* Get the parser plugin manager
*
* @return ObjectPluginManager
*/
public static function getObjectPluginManager()
{
if (! static::$objectPlugins instanceof ObjectPluginManager) {
static::$objectPlugins = new ObjectPluginManager(new ServiceManager);
}
return static::$objectPlugins;
}
/**
* Get the renderer plugin manager
*
* @return RendererPluginManager
*/
public static function getRendererPluginManager()
{
if (! static::$rendererPlugins instanceof RendererPluginManager) {
static::$rendererPlugins = new RendererPluginManager(new ServiceManager);
}
return static::$rendererPlugins;
}
/**
* Factory for Zend\Barcode classes.
*
* First argument may be a string containing the base of the adapter class
* name, e.g. 'code25' corresponds to class Object\Code25. This
* is case-insensitive.
*
* First argument may alternatively be an object of type Traversable.
* The barcode class base name is read from the 'barcode' property.
* The barcode 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 barcode constructor.
*
* If the first argument is of type Traversable, it is assumed to contain
* all parameters, and the second argument is ignored.
*
* @param mixed $barcode String name of barcode class, or Traversable object.
* @param mixed $renderer String name of renderer class
* @param mixed $barcodeConfig OPTIONAL; an array or Traversable object with barcode parameters.
* @param mixed $rendererConfig OPTIONAL; an array or Traversable object with renderer parameters.
* @param bool $automaticRenderError OPTIONAL; set the automatic rendering of exception
* @return RendererInterface
* @throws Exception\ExceptionInterface
*/
public static function factory(
$barcode,
$renderer = 'image',
$barcodeConfig = [],
$rendererConfig = [],
$automaticRenderError = true
) {
/*
* Convert Traversable argument to plain string
* barcode name and separate config object.
*/
if ($barcode instanceof Traversable) {
$barcode = ArrayUtils::iteratorToArray($barcode);
if (isset($barcode['rendererParams'])) {
$rendererConfig = $barcode['rendererParams'];
}
if (isset($barcode['renderer'])) {
$renderer = (string) $barcode['renderer'];
}
if (isset($barcode['barcodeParams'])) {
$barcodeConfig = $barcode['barcodeParams'];
}
if (isset($barcode['barcode'])) {
$barcode = (string) $barcode['barcode'];
} else {
$barcode = null;
}
}
try {
$barcode = static::makeBarcode($barcode, $barcodeConfig);
$renderer = static::makeRenderer($renderer, $rendererConfig);
} catch (\Exception $e) {
if ($automaticRenderError && ! ($e instanceof Exception\RendererCreationException)) {
$barcode = static::makeBarcode('error', ['text' => $e->getMessage()]);
$renderer = static::makeRenderer($renderer, []);
} else {
throw $e;
}
}
$renderer->setAutomaticRenderError($automaticRenderError);
return $renderer->setBarcode($barcode);
}
/**
* Barcode Constructor
*
* @param mixed $barcode String name of barcode class, or Traversable object, or barcode object.
* @param mixed $barcodeConfig OPTIONAL; an array or Traversable object with barcode parameters.
* @throws Exception\InvalidArgumentException
* @return Object\ObjectInterface
*/
public static function makeBarcode($barcode, $barcodeConfig = [])
{
if ($barcode instanceof Object\ObjectInterface) {
return $barcode;
}
/*
* Convert Traversable argument to plain string
* barcode name and separate configuration.
*/
if ($barcode instanceof Traversable) {
$barcode = ArrayUtils::iteratorToArray($barcode);
if (isset($barcode['barcodeParams']) && is_array($barcode['barcodeParams'])) {
$barcodeConfig = $barcode['barcodeParams'];
}
if (isset($barcode['barcode'])) {
$barcode = (string) $barcode['barcode'];
} else {
$barcode = null;
}
}
if ($barcodeConfig instanceof Traversable) {
$barcodeConfig = ArrayUtils::iteratorToArray($barcodeConfig);
}
/*
* Verify that barcode parameters are in an array.
*/
if (! is_array($barcodeConfig)) {
throw new Exception\InvalidArgumentException(
'Barcode parameters must be in an array or a Traversable object'
);
}
/*
* Verify that a barcode name has been specified.
*/
if (! is_string($barcode) || empty($barcode)) {
throw new Exception\InvalidArgumentException(
'Barcode name must be specified in a string'
);
}
return static::getObjectPluginManager()->get($barcode, $barcodeConfig);
}
/**
* Renderer Constructor
*
* @param mixed $renderer String name of renderer class, or Traversable object.
* @param mixed $rendererConfig OPTIONAL; an array or Traversable object with renderer parameters.
* @throws Exception\RendererCreationException
* @return Renderer\RendererInterface
*/
public static function makeRenderer($renderer = 'image', $rendererConfig = [])
{
if ($renderer instanceof Renderer\RendererInterface) {
return $renderer;
}
/*
* Convert Traversable argument to plain string
* barcode name and separate config object.
*/
if ($renderer instanceof Traversable) {
$renderer = ArrayUtils::iteratorToArray($renderer);
if (isset($renderer['rendererParams'])) {
$rendererConfig = $renderer['rendererParams'];
}
if (isset($renderer['renderer'])) {
$renderer = (string) $renderer['renderer'];
}
}
if ($rendererConfig instanceof Traversable) {
$rendererConfig = ArrayUtils::iteratorToArray($rendererConfig);
}
/*
* Verify that barcode parameters are in an array.
*/
if (! is_array($rendererConfig)) {
throw new Exception\RendererCreationException(
'Barcode parameters must be in an array or a Traversable object'
);
}
/*
* Verify that a barcode name has been specified.
*/
if (! is_string($renderer) || empty($renderer)) {
throw new Exception\RendererCreationException(
'Renderer name must be specified in a string'
);
}
return static::getRendererPluginManager()->get($renderer, $rendererConfig);
}
/**
* Proxy to renderer render() method
*
* @param string | Object\ObjectInterface | array | Traversable $barcode
* @param string | Renderer\RendererInterface $renderer
* @param array | Traversable $barcodeConfig
* @param array | Traversable $rendererConfig
*/
public static function render(
$barcode,
$renderer,
$barcodeConfig = [],
$rendererConfig = []
) {
static::factory($barcode, $renderer, $barcodeConfig, $rendererConfig)->render();
}
/**
* Proxy to renderer draw() method
*
* @param string | Object\ObjectInterface | array | Traversable $barcode
* @param string | Renderer\RendererInterface $renderer
* @param array | Traversable $barcodeConfig
* @param array | Traversable $rendererConfig
* @return mixed
*/
public static function draw(
$barcode,
$renderer,
$barcodeConfig = [],
$rendererConfig = []
) {
return static::factory($barcode, $renderer, $barcodeConfig, $rendererConfig)->draw();
}
/**
* Set the default font for new instances of barcode
*
* @param string $font
* @return void
*/
public static function setBarcodeFont($font)
{
static::$staticFont = $font;
}
/**
* Get current default font
*
* @return string
*/
public static function getBarcodeFont()
{
return static::$staticFont;
}
}