7 * This source file is subject to the new BSD license that is bundled
8 * with this package in the file LICENSE.txt.
9 * It is also available through the world-wide-web at this URL:
10 * http://framework.zend.com/license/new-bsd
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 license@zend.com so we can send you a copy immediately.
16 * @package Zend_Barcode
17 * @copyright Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com)
18 * @license http://framework.zend.com/license/new-bsd New BSD License
23 * Class for generate Barcode
26 * @package Zend_Barcode
27 * @copyright Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com)
28 * @license http://framework.zend.com/license/new-bsd New BSD License
33 * Factory for Zend_Barcode classes.
35 * First argument may be a string containing the base of the adapter class
36 * name, e.g. 'int25' corresponds to class Zend_Barcode_Object_Int25. This
37 * is case-insensitive.
39 * First argument may alternatively be an object of type Zend_Config.
40 * The barcode class base name is read from the 'barcode' property.
41 * The barcode config parameters are read from the 'params' property.
43 * Second argument is optional and may be an associative array of key-value
44 * pairs. This is used as the argument to the barcode constructor.
46 * If the first argument is of type Zend_Config, it is assumed to contain
47 * all parameters, and the second argument is ignored.
49 * @param mixed $barcode String name of barcode class, or Zend_Config object.
50 * @param mixed $renderer String name of renderer class
51 * @param mixed $barcodeConfig OPTIONAL; an array or Zend_Config object with barcode parameters.
52 * @param mixed $rendererConfig OPTIONAL; an array or Zend_Config object with renderer parameters.
53 * @param boolean $automaticRenderError OPTIONAL; set the automatic rendering of exception
54 * @return Zend_Barcode
55 * @throws Zend_Barcode_Exception
57 public static function factory(
60 $barcodeConfig = array(),
61 $rendererConfig = array(),
62 $automaticRenderError = true
65 * Convert Zend_Config argument to plain string
66 * barcode name and separate config object.
68 if ($barcode instanceof Zend_Config
) {
69 if (isset($barcode->rendererParams
)) {
70 $rendererConfig = $barcode->rendererParams
->toArray();
72 if (isset($barcode->renderer
)) {
73 $renderer = (string) $barcode->renderer
;
75 if (isset($barcode->barcodeParams
)) {
76 $barcodeConfig = $barcode->barcodeParams
->toArray();
78 if (isset($barcode->barcode
)) {
79 $barcode = (string) $barcode->barcode
;
86 $barcode = self
::makeBarcode($barcode, $barcodeConfig);
87 $renderer = self
::makeRenderer($renderer, $rendererConfig);
88 } catch (Zend_Exception
$e) {
89 $renderable = ($e instanceof Zend_Barcode_Exception
) ?
$e->isRenderable() : false;
90 if ($automaticRenderError && $renderable) {
91 $barcode = self
::makeBarcode('error', array(
92 'text' => $e->getMessage()
94 $renderer = self
::makeRenderer($renderer, array());
100 $renderer->setAutomaticRenderError($automaticRenderError);
101 return $renderer->setBarcode($barcode);
105 * Barcode Constructor
107 * @param mixed $barcode String name of barcode class, or Zend_Config object.
108 * @param mixed $barcodeConfig OPTIONAL; an array or Zend_Config object with barcode parameters.
109 * @return Zend_Barcode_Object
111 public static function makeBarcode($barcode, $barcodeConfig = array())
113 if ($barcode instanceof Zend_Barcode_Object_ObjectAbstract
) {
118 * Convert Zend_Config argument to plain string
119 * barcode name and separate config object.
121 if ($barcode instanceof Zend_Config
) {
122 if (isset($barcode->barcodeParams
) && $barcode->barcodeParams
instanceof Zend_Config
) {
123 $barcodeConfig = $barcode->barcodeParams
->toArray();
125 if (isset($barcode->barcode
)) {
126 $barcode = (string) $barcode->barcode
;
131 if ($barcodeConfig instanceof Zend_Config
) {
132 $barcodeConfig = $barcodeConfig->toArray();
136 * Verify that barcode parameters are in an array.
138 if (!is_array($barcodeConfig)) {
140 * @see Zend_Barcode_Exception
142 require_once 'Zend/Barcode/Exception.php';
143 throw new Zend_Barcode_Exception(
144 'Barcode parameters must be in an array or a Zend_Config object'
149 * Verify that an barcode name has been specified.
151 if (!is_string($barcode) ||
empty($barcode)) {
153 * @see Zend_Barcode_Exception
155 require_once 'Zend/Barcode/Exception.php';
156 throw new Zend_Barcode_Exception(
157 'Barcode name must be specified in a string'
161 * Form full barcode class name
163 $barcodeNamespace = 'Zend_Barcode_Object';
164 if (isset($barcodeConfig['barcodeNamespace'])) {
165 $barcodeNamespace = $barcodeConfig['barcodeNamespace'];
168 $barcodeName = strtolower($barcodeNamespace . '_' . $barcode);
169 $barcodeName = str_replace(' ', '_', ucwords(
170 str_replace( '_', ' ', $barcodeName)
174 * Load the barcode class. This throws an exception
175 * if the specified class cannot be loaded.
177 if (!class_exists($barcodeName)) {
178 require_once 'Zend/Loader.php';
179 Zend_Loader
::loadClass($barcodeName);
183 * Create an instance of the barcode class.
184 * Pass the config to the barcode class constructor.
186 $bcAdapter = new $barcodeName($barcodeConfig);
189 * Verify that the object created is a descendent of the abstract barcode type.
191 if (!$bcAdapter instanceof Zend_Barcode_Object_ObjectAbstract
) {
193 * @see Zend_Barcode_Exception
195 require_once 'Zend/Barcode/Exception.php';
196 throw new Zend_Barcode_Exception(
197 "Barcode class '$barcodeName' does not extend Zend_Barcode_Object_ObjectAbstract"
204 * Renderer Constructor
206 * @param mixed $renderer String name of renderer class, or Zend_Config object.
207 * @param mixed $rendererConfig OPTIONAL; an array or Zend_Config object with renderer parameters.
208 * @return Zend_Barcode_Renderer
210 public static function makeRenderer($renderer = 'image', $rendererConfig = array())
212 if ($renderer instanceof Zend_Barcode_Renderer_RendererAbstract
) {
217 * Convert Zend_Config argument to plain string
218 * barcode name and separate config object.
220 if ($renderer instanceof Zend_Config
) {
221 if (isset($renderer->rendererParams
)) {
222 $rendererConfig = $renderer->rendererParams
->toArray();
224 if (isset($renderer->renderer
)) {
225 $renderer = (string) $renderer->renderer
;
228 if ($rendererConfig instanceof Zend_Config
) {
229 $rendererConfig = $rendererConfig->toArray();
233 * Verify that barcode parameters are in an array.
235 if (!is_array($rendererConfig)) {
237 * @see Zend_Barcode_Exception
239 require_once 'Zend/Barcode/Exception.php';
240 $e = new Zend_Barcode_Exception(
241 'Barcode parameters must be in an array or a Zend_Config object'
243 $e->setIsRenderable(false);
248 * Verify that an barcode name has been specified.
250 if (!is_string($renderer) ||
empty($renderer)) {
252 * @see Zend_Barcode_Exception
254 require_once 'Zend/Barcode/Exception.php';
255 $e = new Zend_Barcode_Exception(
256 'Renderer name must be specified in a string'
258 $e->setIsRenderable(false);
263 * Form full barcode class name
265 $rendererNamespace = 'Zend_Barcode_Renderer';
266 if (isset($rendererConfig['rendererNamespace'])) {
267 $rendererNamespace = $rendererConfig['rendererNamespace'];
270 $rendererName = strtolower($rendererNamespace . '_' . $renderer);
271 $rendererName = str_replace(' ', '_', ucwords(
272 str_replace( '_', ' ', $rendererName)
276 * Load the barcode class. This throws an exception
277 * if the specified class cannot be loaded.
279 if (!class_exists($rendererName)) {
280 require_once 'Zend/Loader.php';
281 Zend_Loader
::loadClass($rendererName);
285 * Create an instance of the barcode class.
286 * Pass the config to the barcode class constructor.
288 $rdrAdapter = new $rendererName($rendererConfig);
291 * Verify that the object created is a descendent of the abstract barcode type.
293 if (!$rdrAdapter instanceof Zend_Barcode_Renderer_RendererAbstract
) {
295 * @see Zend_Barcode_Exception
297 require_once 'Zend/Barcode/Exception.php';
298 $e = new Zend_Barcode_Exception(
299 "Renderer class '$rendererName' does not extend Zend_Barcode_Renderer_RendererAbstract"
301 $e->setIsRenderable(false);
308 * Proxy to renderer render() method
310 * @param string | Zend_Barcode_Object | array | Zend_Config $barcode
311 * @param string | Zend_Barcode_Renderer $renderer
312 * @param array | Zend_Config $barcodeConfig
313 * @param array | Zend_Config $rendererConfig
315 public static function render(
318 $barcodeConfig = array(),
319 $rendererConfig = array()
321 self
::factory($barcode, $renderer, $barcodeConfig, $rendererConfig)->render();
325 * Proxy to renderer draw() method
327 * @param string | Zend_Barcode_Object | array | Zend_Config $barcode
328 * @param string | Zend_Barcode_Renderer $renderer
329 * @param array | Zend_Config $barcodeConfig
330 * @param array | Zend_Config $rendererConfig
333 public static function draw(
336 $barcodeConfig = array(),
337 $rendererConfig = array()
339 return self
::factory($barcode, $renderer, $barcodeConfig, $rendererConfig)->draw();
343 * Proxy for setBarcodeFont of Zend_Barcode_Object
344 * @param string $font
347 public static function setBarcodeFont($font)
349 require_once 'Zend/Barcode/Object/ObjectAbstract.php';
350 Zend_Barcode_Object_ObjectAbstract
::setBarcodeFont($font);