PageRenderTime 26ms CodeModel.GetById 8ms RepoModel.GetById 1ms app.codeStats 0ms

/library/Zend/Barcode/Barcode.php

https://github.com/mrbanzai/zf2
PHP | 323 lines | 148 code | 26 blank | 149 comment | 29 complexity | 7cb57071675c7b4d3a6b7f12607783c8 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-2012 Zend Technologies USA Inc. (http://www.zend.com)
  18. * @license http://framework.zend.com/license/new-bsd New BSD License
  19. */
  20. /**
  21. * @namespace
  22. */
  23. namespace Zend\Barcode;
  24. use Traversable,
  25. Zend,
  26. Zend\Loader\Broker,
  27. Zend\Loader\ShortNameLocator,
  28. Zend\Stdlib\IteratorToArray;
  29. /**
  30. * Class for generate Barcode
  31. *
  32. * @category Zend
  33. * @package Zend_Barcode
  34. * @copyright Copyright (c) 2005-2012 Zend Technologies USA Inc. (http://www.zend.com)
  35. * @license http://framework.zend.com/license/new-bsd New BSD License
  36. */
  37. class Barcode
  38. {
  39. const OBJECT = 'OBJECT';
  40. const RENDERER = 'RENDERER';
  41. /**
  42. * Default barcode TTF font name
  43. *
  44. * It's used by standard barcode objects derived from
  45. * {@link Object\AbstractObject} class
  46. * if corresponding constructor option is not provided.
  47. *
  48. * @var string
  49. */
  50. protected static $staticFont = null;
  51. /**
  52. * The parser broker
  53. *
  54. * @var Broker
  55. */
  56. protected static $objectBroker;
  57. /**
  58. * The renderer broker
  59. *
  60. * @var Broker
  61. */
  62. protected static $rendererBroker;
  63. /**
  64. * Get the parser broker
  65. *
  66. * @return Broker
  67. */
  68. public static function getObjectBroker()
  69. {
  70. if (!self::$objectBroker instanceof Broker) {
  71. self::$objectBroker = new ObjectBroker();
  72. }
  73. return self::$objectBroker;
  74. }
  75. /**
  76. * Get the renderer broker
  77. *
  78. * @return Broker
  79. */
  80. public static function getRendererBroker()
  81. {
  82. if (!self::$rendererBroker instanceof Broker) {
  83. self::$rendererBroker = new RendererBroker();
  84. }
  85. return self::$rendererBroker;
  86. }
  87. /**
  88. * Factory for Zend_Barcode classes.
  89. *
  90. * First argument may be a string containing the base of the adapter class
  91. * name, e.g. 'int25' corresponds to class Object\Int25. This
  92. * is case-insensitive.
  93. *
  94. * First argument may alternatively be an object of type Traversable.
  95. * The barcode class base name is read from the 'barcode' property.
  96. * The barcode config parameters are read from the 'params' property.
  97. *
  98. * Second argument is optional and may be an associative array of key-value
  99. * pairs. This is used as the argument to the barcode constructor.
  100. *
  101. * If the first argument is of type Traversable, it is assumed to contain
  102. * all parameters, and the second argument is ignored.
  103. *
  104. * @param mixed $barcode String name of barcode class, or Traversable object.
  105. * @param mixed $renderer String name of renderer class
  106. * @param mixed $barcodeConfig OPTIONAL; an array or Traversable object with barcode parameters.
  107. * @param mixed $rendererConfig OPTIONAL; an array or Traversable object with renderer parameters.
  108. * @param boolean $automaticRenderError OPTIONAL; set the automatic rendering of exception
  109. * @return Barcode
  110. * @throws Exception
  111. */
  112. public static function factory($barcode,
  113. $renderer = 'image',
  114. $barcodeConfig = array(),
  115. $rendererConfig = array(),
  116. $automaticRenderError = true)
  117. {
  118. /*
  119. * Convert Traversable argument to plain string
  120. * barcode name and separate config object.
  121. */
  122. if ($barcode instanceof Traversable) {
  123. $barcode = IteratorToArray::convert($barcode);
  124. if (isset($barcode['rendererParams'])) {
  125. $rendererConfig = $barcode['rendererParams'];
  126. }
  127. if (isset($barcode['renderer'])) {
  128. $renderer = (string) $barcode['renderer'];
  129. }
  130. if (isset($barcode['barcodeParams'])) {
  131. $barcodeConfig = $barcode['barcodeParams'];
  132. }
  133. if (isset($barcode['barcode'])) {
  134. $barcode = (string) $barcode['barcode'];
  135. } else {
  136. $barcode = null;
  137. }
  138. }
  139. try {
  140. $barcode = self::makeBarcode($barcode, $barcodeConfig);
  141. $renderer = self::makeRenderer($renderer, $rendererConfig);
  142. } catch (Exception $e) {
  143. if ($automaticRenderError && !($e instanceof Exception\RendererCreationException)) {
  144. $barcode = self::makeBarcode('error', array( 'text' => $e->getMessage() ));
  145. $renderer = self::makeRenderer($renderer, array());
  146. } else {
  147. throw $e;
  148. }
  149. }
  150. $renderer->setAutomaticRenderError($automaticRenderError);
  151. return $renderer->setBarcode($barcode);
  152. }
  153. /**
  154. * Barcode Constructor
  155. *
  156. * @param mixed $barcode String name of barcode class, or Traversable object, or barcode object.
  157. * @param mixed $barcodeConfig OPTIONAL; an array or Traversable object with barcode parameters.
  158. * @return Object
  159. */
  160. public static function makeBarcode($barcode, $barcodeConfig = array())
  161. {
  162. if ($barcode instanceof Object) {
  163. return $barcode;
  164. }
  165. /*
  166. * Convert Traversable argument to plain string
  167. * barcode name and separate configuration.
  168. */
  169. if ($barcode instanceof Traversable) {
  170. $barcode = IteratorToArray::convert($barcode);
  171. if (isset($barcode['barcodeParams']) && is_array($barcode['barcodeParams'])) {
  172. $barcodeConfig = $barcode['barcodeParams'];
  173. }
  174. if (isset($barcode['barcode'])) {
  175. $barcode = (string) $barcode['barcode'];
  176. } else {
  177. $barcode = null;
  178. }
  179. }
  180. if ($barcodeConfig instanceof Traversable) {
  181. $barcodeConfig = IteratorToArray::convert($barcodeConfig);
  182. }
  183. /*
  184. * Verify that barcode parameters are in an array.
  185. */
  186. if (!is_array($barcodeConfig)) {
  187. throw new Exception\InvalidArgumentException(
  188. 'Barcode parameters must be in an array or a Traversable object'
  189. );
  190. }
  191. /*
  192. * Verify that an barcode name has been specified.
  193. */
  194. if (!is_string($barcode) || empty($barcode)) {
  195. throw new Exception\InvalidArgumentException(
  196. 'Barcode name must be specified in a string'
  197. );
  198. }
  199. return self::getObjectBroker()->load($barcode, $barcodeConfig);
  200. }
  201. /**
  202. * Renderer Constructor
  203. *
  204. * @param mixed $renderer String name of renderer class, or Traversable object.
  205. * @param mixed $rendererConfig OPTIONAL; an array or Traversable object with renderer parameters.
  206. * @return Renderer
  207. */
  208. public static function makeRenderer($renderer = 'image', $rendererConfig = array())
  209. {
  210. if ($renderer instanceof Renderer) {
  211. return $renderer;
  212. }
  213. /*
  214. * Convert Traversable argument to plain string
  215. * barcode name and separate config object.
  216. */
  217. if ($renderer instanceof Traversable) {
  218. $renderer = IteratorToArray::convert($renderer);
  219. if (isset($renderer['rendererParams'])) {
  220. $rendererConfig = $renderer['rendererParams'];
  221. }
  222. if (isset($renderer['renderer'])) {
  223. $renderer = (string) $renderer['renderer'];
  224. }
  225. }
  226. if ($rendererConfig instanceof Traversable) {
  227. $rendererConfig = IteratorToArray::convert($rendererConfig);
  228. }
  229. /*
  230. * Verify that barcode parameters are in an array.
  231. */
  232. if (!is_array($rendererConfig)) {
  233. throw new Exception\RendererCreationException(
  234. 'Barcode parameters must be in an array or a Traversable object'
  235. );
  236. }
  237. /*
  238. * Verify that an barcode name has been specified.
  239. */
  240. if (!is_string($renderer) || empty($renderer)) {
  241. throw new Exception\RendererCreationException(
  242. 'Renderer name must be specified in a string'
  243. );
  244. }
  245. return self::getRendererBroker()->load($renderer, $rendererConfig);
  246. }
  247. /**
  248. * Proxy to renderer render() method
  249. *
  250. * @param string | Object | array | Traversable $barcode
  251. * @param string | Renderer $renderer
  252. * @param array | Traversable $barcodeConfig
  253. * @param array | Traversable $rendererConfig
  254. */
  255. public static function render($barcode,
  256. $renderer,
  257. $barcodeConfig = array(),
  258. $rendererConfig = array())
  259. {
  260. self::factory($barcode, $renderer, $barcodeConfig, $rendererConfig)->render();
  261. }
  262. /**
  263. * Proxy to renderer draw() method
  264. *
  265. * @param string | Object | array | Traversable $barcode
  266. * @param string | Renderer $renderer
  267. * @param array | Traversable $barcodeConfig
  268. * @param array | Traversable $rendererConfig
  269. * @return mixed
  270. */
  271. public static function draw($barcode,
  272. $renderer,
  273. $barcodeConfig = array(),
  274. $rendererConfig = array())
  275. {
  276. return self::factory($barcode, $renderer, $barcodeConfig, $rendererConfig)->draw();
  277. }
  278. /**
  279. * Set the default font for new instances of barcode
  280. *
  281. * @param string $font
  282. * @return void
  283. */
  284. public static function setBarcodeFont($font)
  285. {
  286. self::$staticFont = $font;
  287. }
  288. /**
  289. * Get current default font
  290. *
  291. * @return string
  292. */
  293. public static function getBarcodeFont()
  294. {
  295. return self::$staticFont;
  296. }
  297. }