PageRenderTime 37ms CodeModel.GetById 13ms RepoModel.GetById 0ms app.codeStats 0ms

/src/application/libraries/Zend/Barcode.php

https://bitbucket.org/masnug/grc276-blog-laravel
PHP | 352 lines | 171 code | 23 blank | 158 comment | 33 complexity | 673ef14b7720b305c9da385ab2ba4771 MD5 | raw file
  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-2011 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 23775 2011-03-01 17:25:24Z ralph $
  20. */
  21. /**
  22. * Class for generate Barcode
  23. *
  24. * @category Zend
  25. * @package Zend_Barcode
  26. * @copyright Copyright (c) 2005-2011 Zend Technologies USA Inc. (http://www.zend.com)
  27. * @license http://framework.zend.com/license/new-bsd New BSD License
  28. */
  29. class Zend_Barcode
  30. {
  31. /**
  32. * Factory for Zend_Barcode classes.
  33. *
  34. * First argument may be a string containing the base of the adapter class
  35. * name, e.g. 'int25' corresponds to class Zend_Barcode_Object_Int25. This
  36. * is case-insensitive.
  37. *
  38. * First argument may alternatively be an object of type Zend_Config.
  39. * The barcode class base name is read from the 'barcode' property.
  40. * The barcode config parameters are read from the 'params' property.
  41. *
  42. * Second argument is optional and may be an associative array of key-value
  43. * pairs. This is used as the argument to the barcode constructor.
  44. *
  45. * If the first argument is of type Zend_Config, it is assumed to contain
  46. * all parameters, and the second argument is ignored.
  47. *
  48. * @param mixed $barcode String name of barcode class, or Zend_Config object.
  49. * @param mixed $renderer String name of renderer class
  50. * @param mixed $barcodeConfig OPTIONAL; an array or Zend_Config object with barcode parameters.
  51. * @param mixed $rendererConfig OPTIONAL; an array or Zend_Config object with renderer parameters.
  52. * @param boolean $automaticRenderError OPTIONAL; set the automatic rendering of exception
  53. * @return Zend_Barcode
  54. * @throws Zend_Barcode_Exception
  55. */
  56. public static function factory(
  57. $barcode,
  58. $renderer = 'image',
  59. $barcodeConfig = array(),
  60. $rendererConfig = array(),
  61. $automaticRenderError = true
  62. ) {
  63. /*
  64. * Convert Zend_Config argument to plain string
  65. * barcode name and separate config object.
  66. */
  67. if ($barcode instanceof Zend_Config) {
  68. if (isset($barcode->rendererParams)) {
  69. $rendererConfig = $barcode->rendererParams->toArray();
  70. }
  71. if (isset($barcode->renderer)) {
  72. $renderer = (string) $barcode->renderer;
  73. }
  74. if (isset($barcode->barcodeParams)) {
  75. $barcodeConfig = $barcode->barcodeParams->toArray();
  76. }
  77. if (isset($barcode->barcode)) {
  78. $barcode = (string) $barcode->barcode;
  79. } else {
  80. $barcode = null;
  81. }
  82. }
  83. try {
  84. $barcode = self::makeBarcode($barcode, $barcodeConfig);
  85. $renderer = self::makeRenderer($renderer, $rendererConfig);
  86. } catch (Zend_Exception $e) {
  87. $renderable = ($e instanceof Zend_Barcode_Exception) ? $e->isRenderable() : false;
  88. if ($automaticRenderError && $renderable) {
  89. $barcode = self::makeBarcode('error', array(
  90. 'text' => $e->getMessage()
  91. ));
  92. $renderer = self::makeRenderer($renderer, array());
  93. } else {
  94. throw $e;
  95. }
  96. }
  97. $renderer->setAutomaticRenderError($automaticRenderError);
  98. return $renderer->setBarcode($barcode);
  99. }
  100. /**
  101. * Barcode Constructor
  102. *
  103. * @param mixed $barcode String name of barcode class, or Zend_Config object.
  104. * @param mixed $barcodeConfig OPTIONAL; an array or Zend_Config object with barcode parameters.
  105. * @return Zend_Barcode_Object
  106. */
  107. public static function makeBarcode($barcode, $barcodeConfig = array())
  108. {
  109. if ($barcode instanceof Zend_Barcode_Object_ObjectAbstract) {
  110. return $barcode;
  111. }
  112. /*
  113. * Convert Zend_Config argument to plain string
  114. * barcode name and separate config object.
  115. */
  116. if ($barcode instanceof Zend_Config) {
  117. if (isset($barcode->barcodeParams) && $barcode->barcodeParams instanceof Zend_Config) {
  118. $barcodeConfig = $barcode->barcodeParams->toArray();
  119. }
  120. if (isset($barcode->barcode)) {
  121. $barcode = (string) $barcode->barcode;
  122. } else {
  123. $barcode = null;
  124. }
  125. }
  126. if ($barcodeConfig instanceof Zend_Config) {
  127. $barcodeConfig = $barcodeConfig->toArray();
  128. }
  129. /*
  130. * Verify that barcode parameters are in an array.
  131. */
  132. if (!is_array($barcodeConfig)) {
  133. /**
  134. * @see Zend_Barcode_Exception
  135. */
  136. require_once 'Zend/Barcode/Exception.php';
  137. throw new Zend_Barcode_Exception(
  138. 'Barcode parameters must be in an array or a Zend_Config object'
  139. );
  140. }
  141. /*
  142. * Verify that an barcode name has been specified.
  143. */
  144. if (!is_string($barcode) || empty($barcode)) {
  145. /**
  146. * @see Zend_Barcode_Exception
  147. */
  148. require_once 'Zend/Barcode/Exception.php';
  149. throw new Zend_Barcode_Exception(
  150. 'Barcode name must be specified in a string'
  151. );
  152. }
  153. /*
  154. * Form full barcode class name
  155. */
  156. $barcodeNamespace = 'Zend_Barcode_Object';
  157. if (isset($barcodeConfig['barcodeNamespace'])) {
  158. $barcodeNamespace = $barcodeConfig['barcodeNamespace'];
  159. }
  160. $barcodeName = strtolower($barcodeNamespace . '_' . $barcode);
  161. $barcodeName = str_replace(' ', '_', ucwords(
  162. str_replace( '_', ' ', $barcodeName)
  163. ));
  164. /*
  165. * Load the barcode class. This throws an exception
  166. * if the specified class cannot be loaded.
  167. */
  168. if (!class_exists($barcodeName)) {
  169. require_once 'Zend/Loader.php';
  170. Zend_Loader::loadClass($barcodeName);
  171. }
  172. /*
  173. * Create an instance of the barcode class.
  174. * Pass the config to the barcode class constructor.
  175. */
  176. $bcAdapter = new $barcodeName($barcodeConfig);
  177. /*
  178. * Verify that the object created is a descendent of the abstract barcode type.
  179. */
  180. if (!$bcAdapter instanceof Zend_Barcode_Object_ObjectAbstract) {
  181. /**
  182. * @see Zend_Barcode_Exception
  183. */
  184. require_once 'Zend/Barcode/Exception.php';
  185. throw new Zend_Barcode_Exception(
  186. "Barcode class '$barcodeName' does not extend Zend_Barcode_Object_ObjectAbstract"
  187. );
  188. }
  189. return $bcAdapter;
  190. }
  191. /**
  192. * Renderer Constructor
  193. *
  194. * @param mixed $renderer String name of renderer class, or Zend_Config object.
  195. * @param mixed $rendererConfig OPTIONAL; an array or Zend_Config object with renderer parameters.
  196. * @return Zend_Barcode_Renderer
  197. */
  198. public static function makeRenderer($renderer = 'image', $rendererConfig = array())
  199. {
  200. if ($renderer instanceof Zend_Barcode_Renderer_RendererAbstract) {
  201. return $renderer;
  202. }
  203. /*
  204. * Convert Zend_Config argument to plain string
  205. * barcode name and separate config object.
  206. */
  207. if ($renderer instanceof Zend_Config) {
  208. if (isset($renderer->rendererParams)) {
  209. $rendererConfig = $renderer->rendererParams->toArray();
  210. }
  211. if (isset($renderer->renderer)) {
  212. $renderer = (string) $renderer->renderer;
  213. }
  214. }
  215. if ($rendererConfig instanceof Zend_Config) {
  216. $rendererConfig = $rendererConfig->toArray();
  217. }
  218. /*
  219. * Verify that barcode parameters are in an array.
  220. */
  221. if (!is_array($rendererConfig)) {
  222. /**
  223. * @see Zend_Barcode_Exception
  224. */
  225. require_once 'Zend/Barcode/Exception.php';
  226. $e = new Zend_Barcode_Exception(
  227. 'Barcode parameters must be in an array or a Zend_Config object'
  228. );
  229. $e->setIsRenderable(false);
  230. throw $e;
  231. }
  232. /*
  233. * Verify that an barcode name has been specified.
  234. */
  235. if (!is_string($renderer) || empty($renderer)) {
  236. /**
  237. * @see Zend_Barcode_Exception
  238. */
  239. require_once 'Zend/Barcode/Exception.php';
  240. $e = new Zend_Barcode_Exception(
  241. 'Renderer name must be specified in a string'
  242. );
  243. $e->setIsRenderable(false);
  244. throw $e;
  245. }
  246. /*
  247. * Form full barcode class name
  248. */
  249. $rendererNamespace = 'Zend_Barcode_Renderer';
  250. if (isset($rendererConfig['rendererNamespace'])) {
  251. $rendererNamespace = $rendererConfig['rendererNamespace'];
  252. }
  253. $rendererName = strtolower($rendererNamespace . '_' . $renderer);
  254. $rendererName = str_replace(' ', '_', ucwords(
  255. str_replace( '_', ' ', $rendererName)
  256. ));
  257. /*
  258. * Load the barcode class. This throws an exception
  259. * if the specified class cannot be loaded.
  260. */
  261. if (!class_exists($rendererName)) {
  262. require_once 'Zend/Loader.php';
  263. Zend_Loader::loadClass($rendererName);
  264. }
  265. /*
  266. * Create an instance of the barcode class.
  267. * Pass the config to the barcode class constructor.
  268. */
  269. $rdrAdapter = new $rendererName($rendererConfig);
  270. /*
  271. * Verify that the object created is a descendent of the abstract barcode type.
  272. */
  273. if (!$rdrAdapter instanceof Zend_Barcode_Renderer_RendererAbstract) {
  274. /**
  275. * @see Zend_Barcode_Exception
  276. */
  277. require_once 'Zend/Barcode/Exception.php';
  278. $e = new Zend_Barcode_Exception(
  279. "Renderer class '$rendererName' does not extend Zend_Barcode_Renderer_RendererAbstract"
  280. );
  281. $e->setIsRenderable(false);
  282. throw $e;
  283. }
  284. return $rdrAdapter;
  285. }
  286. /**
  287. * Proxy to renderer render() method
  288. *
  289. * @param string | Zend_Barcode_Object | array | Zend_Config $barcode
  290. * @param string | Zend_Barcode_Renderer $renderer
  291. * @param array | Zend_Config $barcodeConfig
  292. * @param array | Zend_Config $rendererConfig
  293. */
  294. public static function render(
  295. $barcode,
  296. $renderer,
  297. $barcodeConfig = array(),
  298. $rendererConfig = array()
  299. ) {
  300. self::factory($barcode, $renderer, $barcodeConfig, $rendererConfig)->render();
  301. }
  302. /**
  303. * Proxy to renderer draw() method
  304. *
  305. * @param string | Zend_Barcode_Object | array | Zend_Config $barcode
  306. * @param string | Zend_Barcode_Renderer $renderer
  307. * @param array | Zend_Config $barcodeConfig
  308. * @param array | Zend_Config $rendererConfig
  309. * @return mixed
  310. */
  311. public static function draw(
  312. $barcode,
  313. $renderer,
  314. $barcodeConfig = array(),
  315. $rendererConfig = array()
  316. ) {
  317. return self::factory($barcode, $renderer, $barcodeConfig, $rendererConfig)->draw();
  318. }
  319. /**
  320. * Proxy for setBarcodeFont of Zend_Barcode_Object
  321. * @param string $font
  322. * @eturn void
  323. */
  324. public static function setBarcodeFont($font)
  325. {
  326. require_once 'Zend/Barcode/Object/ObjectAbstract.php';
  327. Zend_Barcode_Object_ObjectAbstract::setBarcodeFont($font);
  328. }
  329. }