File indexing completed on 2024-04-28 06:00:00

0001 <?php
0002 /**
0003  * Zend Framework
0004  *
0005  * LICENSE
0006  *
0007  * This source file is subject to the new BSD license that is bundled
0008  * with this package in the file LICENSE.txt.
0009  * It is also available through the world-wide-web at this URL:
0010  * http://framework.zend.com/license/new-bsd
0011  * If you did not receive a copy of the license and are unable to
0012  * obtain it through the world-wide-web, please send an email
0013  * to license@zend.com so we can send you a copy immediately.
0014  *
0015  * @category   Zend
0016  * @package    Zend_Barcode
0017  * @copyright  Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com)
0018  * @license    http://framework.zend.com/license/new-bsd     New BSD License
0019  * @version    $Id$
0020  */
0021 
0022 /**
0023  * Class for generate Barcode
0024  *
0025  * @category   Zend
0026  * @package    Zend_Barcode
0027  * @copyright  Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com)
0028  * @license    http://framework.zend.com/license/new-bsd     New BSD License
0029  */
0030 class Zend_Barcode
0031 {
0032     /**
0033      * Factory for Zend_Barcode classes.
0034      *
0035      * First argument may be a string containing the base of the adapter class
0036      * name, e.g. 'int25' corresponds to class Zend_Barcode_Object_Int25.  This
0037      * is case-insensitive.
0038      *
0039      * First argument may alternatively be an object of type Zend_Config.
0040      * The barcode class base name is read from the 'barcode' property.
0041      * The barcode config parameters are read from the 'params' property.
0042      *
0043      * Second argument is optional and may be an associative array of key-value
0044      * pairs.  This is used as the argument to the barcode constructor.
0045      *
0046      * If the first argument is of type Zend_Config, it is assumed to contain
0047      * all parameters, and the second argument is ignored.
0048      *
0049      * @param  mixed $barcode         String name of barcode class, or Zend_Config object.
0050      * @param  mixed $renderer        String name of renderer class
0051      * @param  mixed $barcodeConfig   OPTIONAL; an array or Zend_Config object with barcode parameters.
0052      * @param  mixed $rendererConfig  OPTIONAL; an array or Zend_Config object with renderer parameters.
0053      * @param  boolean $automaticRenderError  OPTIONAL; set the automatic rendering of exception
0054      * @return Zend_Barcode
0055      * @throws Zend_Barcode_Exception
0056      */
0057     public static function factory(
0058         $barcode,
0059         $renderer = 'image',
0060         $barcodeConfig = array(),
0061         $rendererConfig = array(),
0062         $automaticRenderError = true
0063     ) {
0064         /*
0065          * Convert Zend_Config argument to plain string
0066          * barcode name and separate config object.
0067          */
0068         if ($barcode instanceof Zend_Config) {
0069             if (isset($barcode->rendererParams)) {
0070                 $rendererConfig = $barcode->rendererParams->toArray();
0071             }
0072             if (isset($barcode->renderer)) {
0073                 $renderer = (string) $barcode->renderer;
0074             }
0075             if (isset($barcode->barcodeParams)) {
0076                 $barcodeConfig = $barcode->barcodeParams->toArray();
0077             }
0078             if (isset($barcode->barcode)) {
0079                 $barcode = (string) $barcode->barcode;
0080             } else {
0081                 $barcode = null;
0082             }
0083         }
0084 
0085         try {
0086             $barcode  = self::makeBarcode($barcode, $barcodeConfig);
0087             $renderer = self::makeRenderer($renderer, $rendererConfig);
0088         } catch (Zend_Exception $e) {
0089             $renderable = ($e instanceof Zend_Barcode_Exception) ? $e->isRenderable() : false;
0090             if ($automaticRenderError && $renderable) {
0091                 $barcode = self::makeBarcode('error', array(
0092                     'text' => $e->getMessage()
0093                 ));
0094                 $renderer = self::makeRenderer($renderer, array());
0095             } else {
0096                 throw $e;
0097             }
0098         }
0099 
0100         $renderer->setAutomaticRenderError($automaticRenderError);
0101         return $renderer->setBarcode($barcode);
0102     }
0103 
0104     /**
0105      * Barcode Constructor
0106      *
0107      * @param mixed $barcode        String name of barcode class, or Zend_Config object.
0108      * @param mixed $barcodeConfig  OPTIONAL; an array or Zend_Config object with barcode parameters.
0109      * @return Zend_Barcode_Object
0110      */
0111     public static function makeBarcode($barcode, $barcodeConfig = array())
0112     {
0113         if ($barcode instanceof Zend_Barcode_Object_ObjectAbstract) {
0114             return $barcode;
0115         }
0116 
0117         /*
0118          * Convert Zend_Config argument to plain string
0119          * barcode name and separate config object.
0120          */
0121         if ($barcode instanceof Zend_Config) {
0122             if (isset($barcode->barcodeParams) && $barcode->barcodeParams instanceof Zend_Config) {
0123                 $barcodeConfig = $barcode->barcodeParams->toArray();
0124             }
0125             if (isset($barcode->barcode)) {
0126                 $barcode = (string) $barcode->barcode;
0127             } else {
0128                 $barcode = null;
0129             }
0130         }
0131         if ($barcodeConfig instanceof Zend_Config) {
0132             $barcodeConfig = $barcodeConfig->toArray();
0133         }
0134 
0135         /*
0136          * Verify that barcode parameters are in an array.
0137          */
0138         if (!is_array($barcodeConfig)) {
0139             /**
0140              * @see Zend_Barcode_Exception
0141              */
0142             // require_once 'Zend/Barcode/Exception.php';
0143             throw new Zend_Barcode_Exception(
0144                 'Barcode parameters must be in an array or a Zend_Config object'
0145             );
0146         }
0147 
0148         /*
0149          * Verify that an barcode name has been specified.
0150          */
0151         if (!is_string($barcode) || empty($barcode)) {
0152             /**
0153              * @see Zend_Barcode_Exception
0154              */
0155             // require_once 'Zend/Barcode/Exception.php';
0156             throw new Zend_Barcode_Exception(
0157                 'Barcode name must be specified in a string'
0158             );
0159         }
0160         /*
0161          * Form full barcode class name
0162          */
0163         $barcodeNamespace = 'Zend_Barcode_Object';
0164         if (isset($barcodeConfig['barcodeNamespace'])) {
0165             $barcodeNamespace = $barcodeConfig['barcodeNamespace'];
0166         }
0167 
0168         $barcodeName = strtolower($barcodeNamespace . '_' . $barcode);
0169         $barcodeName = str_replace(' ', '_', ucwords(
0170             str_replace( '_', ' ', $barcodeName)
0171         ));
0172 
0173         /*
0174          * Load the barcode class.  This throws an exception
0175          * if the specified class cannot be loaded.
0176          */
0177         if (!class_exists($barcodeName)) {
0178             // require_once 'Zend/Loader.php';
0179             Zend_Loader::loadClass($barcodeName);
0180         }
0181 
0182         /*
0183          * Create an instance of the barcode class.
0184          * Pass the config to the barcode class constructor.
0185          */
0186         $bcAdapter = new $barcodeName($barcodeConfig);
0187 
0188         /*
0189          * Verify that the object created is a descendent of the abstract barcode type.
0190          */
0191         if (!$bcAdapter instanceof Zend_Barcode_Object_ObjectAbstract) {
0192             /**
0193              * @see Zend_Barcode_Exception
0194              */
0195             // require_once 'Zend/Barcode/Exception.php';
0196             throw new Zend_Barcode_Exception(
0197                 "Barcode class '$barcodeName' does not extend Zend_Barcode_Object_ObjectAbstract"
0198             );
0199         }
0200         return $bcAdapter;
0201     }
0202 
0203     /**
0204      * Renderer Constructor
0205      *
0206      * @param mixed $renderer           String name of renderer class, or Zend_Config object.
0207      * @param mixed $rendererConfig     OPTIONAL; an array or Zend_Config object with renderer parameters.
0208      * @return Zend_Barcode_Renderer
0209      */
0210     public static function makeRenderer($renderer = 'image', $rendererConfig = array())
0211     {
0212         if ($renderer instanceof Zend_Barcode_Renderer_RendererAbstract) {
0213             return $renderer;
0214         }
0215 
0216         /*
0217          * Convert Zend_Config argument to plain string
0218          * barcode name and separate config object.
0219          */
0220         if ($renderer instanceof Zend_Config) {
0221             if (isset($renderer->rendererParams)) {
0222                 $rendererConfig = $renderer->rendererParams->toArray();
0223             }
0224             if (isset($renderer->renderer)) {
0225                 $renderer = (string) $renderer->renderer;
0226             }
0227         }
0228         if ($rendererConfig instanceof Zend_Config) {
0229             $rendererConfig = $rendererConfig->toArray();
0230         }
0231 
0232         /*
0233          * Verify that barcode parameters are in an array.
0234          */
0235         if (!is_array($rendererConfig)) {
0236             /**
0237              * @see Zend_Barcode_Exception
0238              */
0239             // require_once 'Zend/Barcode/Exception.php';
0240             $e = new Zend_Barcode_Exception(
0241                 'Barcode parameters must be in an array or a Zend_Config object'
0242             );
0243             $e->setIsRenderable(false);
0244             throw $e;
0245         }
0246 
0247         /*
0248          * Verify that an barcode name has been specified.
0249          */
0250         if (!is_string($renderer) || empty($renderer)) {
0251             /**
0252              * @see Zend_Barcode_Exception
0253              */
0254             // require_once 'Zend/Barcode/Exception.php';
0255             $e = new Zend_Barcode_Exception(
0256                 'Renderer name must be specified in a string'
0257             );
0258             $e->setIsRenderable(false);
0259             throw $e;
0260         }
0261 
0262         /*
0263          * Form full barcode class name
0264          */
0265         $rendererNamespace = 'Zend_Barcode_Renderer';
0266         if (isset($rendererConfig['rendererNamespace'])) {
0267             $rendererNamespace = $rendererConfig['rendererNamespace'];
0268         }
0269 
0270         $rendererName = strtolower($rendererNamespace . '_' . $renderer);
0271         $rendererName = str_replace(' ', '_', ucwords(
0272             str_replace( '_', ' ', $rendererName)
0273         ));
0274 
0275         /*
0276          * Load the barcode class.  This throws an exception
0277          * if the specified class cannot be loaded.
0278          */
0279         if (!class_exists($rendererName)) {
0280             // require_once 'Zend/Loader.php';
0281             Zend_Loader::loadClass($rendererName);
0282         }
0283 
0284         /*
0285          * Create an instance of the barcode class.
0286          * Pass the config to the barcode class constructor.
0287          */
0288         $rdrAdapter = new $rendererName($rendererConfig);
0289 
0290         /*
0291          * Verify that the object created is a descendent of the abstract barcode type.
0292          */
0293         if (!$rdrAdapter instanceof Zend_Barcode_Renderer_RendererAbstract) {
0294             /**
0295              * @see Zend_Barcode_Exception
0296              */
0297             // require_once 'Zend/Barcode/Exception.php';
0298             $e = new Zend_Barcode_Exception(
0299                 "Renderer class '$rendererName' does not extend Zend_Barcode_Renderer_RendererAbstract"
0300             );
0301             $e->setIsRenderable(false);
0302             throw $e;
0303         }
0304         return $rdrAdapter;
0305     }
0306 
0307     /**
0308      * Proxy to renderer render() method
0309      *
0310      * @param string | Zend_Barcode_Object | array | Zend_Config $barcode
0311      * @param string | Zend_Barcode_Renderer $renderer
0312      * @param array | Zend_Config $barcodeConfig
0313      * @param array | Zend_Config $rendererConfig
0314      */
0315     public static function render(
0316         $barcode,
0317         $renderer,
0318         $barcodeConfig = array(),
0319         $rendererConfig = array()
0320     ) {
0321         self::factory($barcode, $renderer, $barcodeConfig, $rendererConfig)->render();
0322     }
0323 
0324     /**
0325      * Proxy to renderer draw() method
0326      *
0327      * @param string | Zend_Barcode_Object | array | Zend_Config $barcode
0328      * @param string | Zend_Barcode_Renderer $renderer
0329      * @param array | Zend_Config $barcodeConfig
0330      * @param array | Zend_Config $rendererConfig
0331      * @return mixed
0332      */
0333     public static function draw(
0334         $barcode,
0335         $renderer,
0336         $barcodeConfig = array(),
0337         $rendererConfig = array()
0338     ) {
0339         return self::factory($barcode, $renderer, $barcodeConfig, $rendererConfig)->draw();
0340     }
0341 
0342     /**
0343      * Proxy for setBarcodeFont of Zend_Barcode_Object
0344      * @param string $font
0345      * @eturn void
0346      */
0347     public static function setBarcodeFont($font)
0348     {
0349         // require_once 'Zend/Barcode/Object/ObjectAbstract.php';
0350         Zend_Barcode_Object_ObjectAbstract::setBarcodeFont($font);
0351     }
0352 }