web/lib/Zend/Barcode.php
changeset 64 162c1de6545a
parent 19 1c2f13fd785c
child 68 ecaf28ffe26e
equal deleted inserted replaced
63:5b37998e522e 64:162c1de6545a
       
     1 <?php
       
     2 /**
       
     3  * Zend Framework
       
     4  *
       
     5  * LICENSE
       
     6  *
       
     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.
       
    14  *
       
    15  * @category   Zend
       
    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
       
    19  * @version    $Id: Barcode.php 20096 2010-01-06 02:05:09Z bkarwin $
       
    20  */
       
    21 
       
    22 /**
       
    23  * Class for generate Barcode
       
    24  *
       
    25  * @category   Zend
       
    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
       
    29  */
       
    30 class Zend_Barcode
       
    31 {
       
    32     /**
       
    33      * Factory for Zend_Barcode classes.
       
    34      *
       
    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.
       
    38      *
       
    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.
       
    42      *
       
    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.
       
    45      *
       
    46      * If the first argument is of type Zend_Config, it is assumed to contain
       
    47      * all parameters, and the second argument is ignored.
       
    48      *
       
    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
       
    56      */
       
    57     public static function factory(
       
    58         $barcode, 
       
    59         $renderer = 'image', 
       
    60         $barcodeConfig = array(), 
       
    61         $rendererConfig = array(), 
       
    62         $automaticRenderError = true
       
    63     ) {
       
    64         /*
       
    65          * Convert Zend_Config argument to plain string
       
    66          * barcode name and separate config object.
       
    67          */
       
    68         if ($barcode instanceof Zend_Config) {
       
    69             if (isset($barcode->rendererParams)) {
       
    70                 $rendererConfig = $barcode->rendererParams->toArray();
       
    71             }
       
    72             if (isset($barcode->renderer)) {
       
    73                 $renderer = (string) $barcode->renderer;
       
    74             }
       
    75             if (isset($barcode->barcodeParams)) {
       
    76                 $barcodeConfig = $barcode->barcodeParams->toArray();
       
    77             }
       
    78             if (isset($barcode->barcode)) {
       
    79                 $barcode = (string) $barcode->barcode;
       
    80             } else {
       
    81                 $barcode = null;
       
    82             }
       
    83         }
       
    84 
       
    85         try {
       
    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()
       
    93                 ));
       
    94                 $renderer = self::makeRenderer($renderer, array());
       
    95             } else {
       
    96                 throw $e;
       
    97             }
       
    98         }
       
    99 
       
   100         $renderer->setAutomaticRenderError($automaticRenderError);
       
   101         return $renderer->setBarcode($barcode);
       
   102     }
       
   103 
       
   104     /**
       
   105      * Barcode Constructor
       
   106      *
       
   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
       
   110      */
       
   111     public static function makeBarcode($barcode, $barcodeConfig = array())
       
   112     {
       
   113         if ($barcode instanceof Zend_Barcode_Object_ObjectAbstract) {
       
   114             return $barcode;
       
   115         }
       
   116 
       
   117         /*
       
   118          * Convert Zend_Config argument to plain string
       
   119          * barcode name and separate config object.
       
   120          */
       
   121         if ($barcode instanceof Zend_Config) {
       
   122             if (isset($barcode->barcodeParams) && $barcode->barcodeParams instanceof Zend_Config) {
       
   123                 $barcodeConfig = $barcode->barcodeParams->toArray();
       
   124             }
       
   125             if (isset($barcode->barcode)) {
       
   126                 $barcode = (string) $barcode->barcode;
       
   127             } else {
       
   128                 $barcode = null;
       
   129             }
       
   130         }
       
   131         if ($barcodeConfig instanceof Zend_Config) {
       
   132             $barcodeConfig = $barcodeConfig->toArray();
       
   133         }
       
   134 
       
   135         /*
       
   136          * Verify that barcode parameters are in an array.
       
   137          */
       
   138         if (!is_array($barcodeConfig)) {
       
   139             /**
       
   140              * @see Zend_Barcode_Exception
       
   141              */
       
   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'
       
   145             );
       
   146         }
       
   147 
       
   148         /*
       
   149          * Verify that an barcode name has been specified.
       
   150          */
       
   151         if (!is_string($barcode) || empty($barcode)) {
       
   152             /**
       
   153              * @see Zend_Barcode_Exception
       
   154              */
       
   155             require_once 'Zend/Barcode/Exception.php';
       
   156             throw new Zend_Barcode_Exception(
       
   157                 'Barcode name must be specified in a string'
       
   158             );
       
   159         }
       
   160         /*
       
   161          * Form full barcode class name
       
   162          */
       
   163         $barcodeNamespace = 'Zend_Barcode_Object';
       
   164         if (isset($barcodeConfig['barcodeNamespace'])) {
       
   165             $barcodeNamespace = $barcodeConfig['barcodeNamespace'];
       
   166         }
       
   167 
       
   168         $barcodeName = strtolower($barcodeNamespace . '_' . $barcode);
       
   169         $barcodeName = str_replace(' ', '_', ucwords(
       
   170             str_replace( '_', ' ', $barcodeName)
       
   171         ));
       
   172 
       
   173         /*
       
   174          * Load the barcode class.  This throws an exception
       
   175          * if the specified class cannot be loaded.
       
   176          */
       
   177         if (!class_exists($barcodeName)) {
       
   178             require_once 'Zend/Loader.php';
       
   179             Zend_Loader::loadClass($barcodeName);
       
   180         }
       
   181 
       
   182         /*
       
   183          * Create an instance of the barcode class.
       
   184          * Pass the config to the barcode class constructor.
       
   185          */
       
   186         $bcAdapter = new $barcodeName($barcodeConfig);
       
   187 
       
   188         /*
       
   189          * Verify that the object created is a descendent of the abstract barcode type.
       
   190          */
       
   191         if (!$bcAdapter instanceof Zend_Barcode_Object_ObjectAbstract) {
       
   192             /**
       
   193              * @see Zend_Barcode_Exception
       
   194              */
       
   195             require_once 'Zend/Barcode/Exception.php';
       
   196             throw new Zend_Barcode_Exception(
       
   197                 "Barcode class '$barcodeName' does not extend Zend_Barcode_Object_ObjectAbstract"
       
   198             );
       
   199         }
       
   200         return $bcAdapter;
       
   201     }
       
   202 
       
   203     /**
       
   204      * Renderer Constructor
       
   205      *
       
   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
       
   209      */
       
   210     public static function makeRenderer($renderer = 'image', $rendererConfig = array())
       
   211     {
       
   212         if ($renderer instanceof Zend_Barcode_Renderer_RendererAbstract) {
       
   213             return $renderer;
       
   214         }
       
   215 
       
   216         /*
       
   217          * Convert Zend_Config argument to plain string
       
   218          * barcode name and separate config object.
       
   219          */
       
   220         if ($renderer instanceof Zend_Config) {
       
   221             if (isset($renderer->rendererParams)) {
       
   222                 $rendererConfig = $renderer->rendererParams->toArray();
       
   223             }
       
   224             if (isset($renderer->renderer)) {
       
   225                 $renderer = (string) $renderer->renderer;
       
   226             }
       
   227         }
       
   228         if ($rendererConfig instanceof Zend_Config) {
       
   229             $rendererConfig = $rendererConfig->toArray();
       
   230         }
       
   231 
       
   232         /*
       
   233          * Verify that barcode parameters are in an array.
       
   234          */
       
   235         if (!is_array($rendererConfig)) {
       
   236             /**
       
   237              * @see Zend_Barcode_Exception
       
   238              */
       
   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'
       
   242             );
       
   243             $e->setIsRenderable(false);
       
   244             throw $e;
       
   245         }
       
   246 
       
   247         /*
       
   248          * Verify that an barcode name has been specified.
       
   249          */
       
   250         if (!is_string($renderer) || empty($renderer)) {
       
   251             /**
       
   252              * @see Zend_Barcode_Exception
       
   253              */
       
   254             require_once 'Zend/Barcode/Exception.php';
       
   255             $e = new Zend_Barcode_Exception(
       
   256                 'Renderer name must be specified in a string'
       
   257             );
       
   258             $e->setIsRenderable(false);
       
   259             throw $e;
       
   260         }
       
   261 
       
   262         /*
       
   263          * Form full barcode class name
       
   264          */
       
   265         $rendererNamespace = 'Zend_Barcode_Renderer';
       
   266         if (isset($rendererConfig['rendererNamespace'])) {
       
   267             $rendererNamespace = $rendererConfig['rendererNamespace'];
       
   268         }
       
   269 
       
   270         $rendererName = strtolower($rendererNamespace . '_' . $renderer);
       
   271         $rendererName = str_replace(' ', '_', ucwords(
       
   272             str_replace( '_', ' ', $rendererName)
       
   273         ));
       
   274 
       
   275         /*
       
   276          * Load the barcode class.  This throws an exception
       
   277          * if the specified class cannot be loaded.
       
   278          */
       
   279         if (!class_exists($rendererName)) {
       
   280             require_once 'Zend/Loader.php';
       
   281             Zend_Loader::loadClass($rendererName);
       
   282         }
       
   283 
       
   284         /*
       
   285          * Create an instance of the barcode class.
       
   286          * Pass the config to the barcode class constructor.
       
   287          */
       
   288         $rdrAdapter = new $rendererName($rendererConfig);
       
   289 
       
   290         /*
       
   291          * Verify that the object created is a descendent of the abstract barcode type.
       
   292          */
       
   293         if (!$rdrAdapter instanceof Zend_Barcode_Renderer_RendererAbstract) {
       
   294             /**
       
   295              * @see Zend_Barcode_Exception
       
   296              */
       
   297             require_once 'Zend/Barcode/Exception.php';
       
   298             $e = new Zend_Barcode_Exception(
       
   299                 "Renderer class '$rendererName' does not extend Zend_Barcode_Renderer_RendererAbstract"
       
   300             );
       
   301             $e->setIsRenderable(false);
       
   302             throw $e;
       
   303         }
       
   304         return $rdrAdapter;
       
   305     }
       
   306 
       
   307     /**
       
   308      * Proxy to renderer render() method
       
   309      *
       
   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
       
   314      */
       
   315     public static function render(
       
   316         $barcode, 
       
   317         $renderer, 
       
   318         $barcodeConfig = array(), 
       
   319         $rendererConfig = array()
       
   320     ) {
       
   321         self::factory($barcode, $renderer, $barcodeConfig, $rendererConfig)->render();
       
   322     }
       
   323 
       
   324     /**
       
   325      * Proxy to renderer draw() method
       
   326      *
       
   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
       
   331      * @return mixed
       
   332      */
       
   333     public static function draw(
       
   334         $barcode, 
       
   335         $renderer, 
       
   336         $barcodeConfig = array(), 
       
   337         $rendererConfig = array()
       
   338     ) {
       
   339         return self::factory($barcode, $renderer, $barcodeConfig, $rendererConfig)->draw();
       
   340     }
       
   341 
       
   342     /**
       
   343      * Proxy for setBarcodeFont of Zend_Barcode_Object
       
   344      * @param string $font
       
   345      * @eturn void
       
   346      */
       
   347     public static function setBarcodeFont($font)
       
   348     {
       
   349         require_once 'Zend/Barcode/Object/ObjectAbstract.php';
       
   350         Zend_Barcode_Object_ObjectAbstract::setBarcodeFont($font);
       
   351     }
       
   352 }