PageRenderTime 32ms CodeModel.GetById 23ms RepoModel.GetById 0ms app.codeStats 1ms

/app/classes/Zend/Barcode/Renderer/AbstractRenderer.php

https://gitlab.com/jalon/doadoronline
PHP | 490 lines | 257 code | 41 blank | 192 comment | 26 complexity | 9f80e81d235ca063bca74b7cd69c0e5f MD5 | raw file
  1. <?php
  2. /**
  3. * Zend Framework (http://framework.zend.com/)
  4. *
  5. * @link http://github.com/zendframework/zf2 for the canonical source repository
  6. * @copyright Copyright (c) 2005-2013 Zend Technologies USA Inc. (http://www.zend.com)
  7. * @license http://framework.zend.com/license/new-bsd New BSD License
  8. */
  9. namespace Zend\Barcode\Renderer;
  10. use Traversable;
  11. use Zend\Barcode\Barcode;
  12. use Zend\Barcode\Exception as BarcodeException;
  13. use Zend\Barcode\Object;
  14. use Zend\Stdlib\ArrayUtils;
  15. /**
  16. * Class for rendering the barcode
  17. */
  18. abstract class AbstractRenderer implements RendererInterface
  19. {
  20. /**
  21. * Namespace of the renderer for autoloading
  22. * @var string
  23. */
  24. protected $rendererNamespace = 'Zend\Barcode\Renderer';
  25. /**
  26. * Renderer type
  27. * @var string
  28. */
  29. protected $type = null;
  30. /**
  31. * Activate/Deactivate the automatic rendering of exception
  32. * @var bool
  33. */
  34. protected $automaticRenderError = false;
  35. /**
  36. * Offset of the barcode from the top of the rendering resource
  37. * @var int
  38. */
  39. protected $topOffset = 0;
  40. /**
  41. * Offset of the barcode from the left of the rendering resource
  42. * @var int
  43. */
  44. protected $leftOffset = 0;
  45. /**
  46. * Horizontal position of the barcode in the rendering resource
  47. * @var int
  48. */
  49. protected $horizontalPosition = 'left';
  50. /**
  51. * Vertical position of the barcode in the rendering resource
  52. * @var int
  53. */
  54. protected $verticalPosition = 'top';
  55. /**
  56. * Module size rendering
  57. * @var float
  58. */
  59. protected $moduleSize = 1;
  60. /**
  61. * Barcode object
  62. * @var Object\ObjectInterface
  63. */
  64. protected $barcode;
  65. /**
  66. * Drawing resource
  67. */
  68. protected $resource;
  69. /**
  70. * Constructor
  71. * @param array|Traversable $options
  72. */
  73. public function __construct($options = null)
  74. {
  75. if ($options instanceof Traversable) {
  76. $options = ArrayUtils::iteratorToArray($options);
  77. }
  78. if (is_array($options)) {
  79. $this->setOptions($options);
  80. }
  81. $this->type = strtolower(substr(
  82. get_class($this),
  83. strlen($this->rendererNamespace) + 1
  84. ));
  85. }
  86. /**
  87. * Set renderer state from options array
  88. * @param array $options
  89. * @return AbstractRenderer
  90. */
  91. public function setOptions($options)
  92. {
  93. foreach ($options as $key => $value) {
  94. $method = 'set' . $key;
  95. if (method_exists($this, $method)) {
  96. $this->$method($value);
  97. }
  98. }
  99. return $this;
  100. }
  101. /**
  102. * Set renderer namespace for autoloading
  103. *
  104. * @param string $namespace
  105. * @return AbstractRenderer
  106. */
  107. public function setRendererNamespace($namespace)
  108. {
  109. $this->rendererNamespace = $namespace;
  110. return $this;
  111. }
  112. /**
  113. * Retrieve renderer namespace
  114. *
  115. * @return string
  116. */
  117. public function getRendererNamespace()
  118. {
  119. return $this->rendererNamespace;
  120. }
  121. /**
  122. * Retrieve renderer type
  123. * @return string
  124. */
  125. public function getType()
  126. {
  127. return $this->type;
  128. }
  129. /**
  130. * Manually adjust top position
  131. * @param int $value
  132. * @return AbstractRenderer
  133. * @throws Exception\OutOfRangeException
  134. */
  135. public function setTopOffset($value)
  136. {
  137. if (!is_numeric($value) || intval($value) < 0) {
  138. throw new Exception\OutOfRangeException(
  139. 'Vertical position must be greater than or equals 0'
  140. );
  141. }
  142. $this->topOffset = intval($value);
  143. return $this;
  144. }
  145. /**
  146. * Retrieve vertical adjustment
  147. * @return int
  148. */
  149. public function getTopOffset()
  150. {
  151. return $this->topOffset;
  152. }
  153. /**
  154. * Manually adjust left position
  155. * @param int $value
  156. * @return AbstractRenderer
  157. * @throws Exception\OutOfRangeException
  158. */
  159. public function setLeftOffset($value)
  160. {
  161. if (!is_numeric($value) || intval($value) < 0) {
  162. throw new Exception\OutOfRangeException(
  163. 'Horizontal position must be greater than or equals 0'
  164. );
  165. }
  166. $this->leftOffset = intval($value);
  167. return $this;
  168. }
  169. /**
  170. * Retrieve vertical adjustment
  171. * @return int
  172. */
  173. public function getLeftOffset()
  174. {
  175. return $this->leftOffset;
  176. }
  177. /**
  178. * Activate/Deactivate the automatic rendering of exception
  179. * @param bool $value
  180. * @return AbstractRenderer
  181. */
  182. public function setAutomaticRenderError($value)
  183. {
  184. $this->automaticRenderError = (bool) $value;
  185. return $this;
  186. }
  187. /**
  188. * Horizontal position of the barcode in the rendering resource
  189. * @param string $value
  190. * @return AbstractRenderer
  191. * @throws Exception\UnexpectedValueException
  192. */
  193. public function setHorizontalPosition($value)
  194. {
  195. if (!in_array($value, array('left', 'center', 'right'))) {
  196. throw new Exception\UnexpectedValueException(
  197. "Invalid barcode position provided must be 'left', 'center' or 'right'"
  198. );
  199. }
  200. $this->horizontalPosition = $value;
  201. return $this;
  202. }
  203. /**
  204. * Horizontal position of the barcode in the rendering resource
  205. * @return string
  206. */
  207. public function getHorizontalPosition()
  208. {
  209. return $this->horizontalPosition;
  210. }
  211. /**
  212. * Vertical position of the barcode in the rendering resource
  213. * @param string $value
  214. * @return AbstractRenderer
  215. * @throws Exception\UnexpectedValueException
  216. */
  217. public function setVerticalPosition($value)
  218. {
  219. if (!in_array($value, array('top', 'middle', 'bottom'))) {
  220. throw new Exception\UnexpectedValueException(
  221. "Invalid barcode position provided must be 'top', 'middle' or 'bottom'"
  222. );
  223. }
  224. $this->verticalPosition = $value;
  225. return $this;
  226. }
  227. /**
  228. * Vertical position of the barcode in the rendering resource
  229. * @return string
  230. */
  231. public function getVerticalPosition()
  232. {
  233. return $this->verticalPosition;
  234. }
  235. /**
  236. * Set the size of a module
  237. * @param float $value
  238. * @return AbstractRenderer
  239. * @throws Exception\OutOfRangeException
  240. */
  241. public function setModuleSize($value)
  242. {
  243. if (!is_numeric($value) || floatval($value) <= 0) {
  244. throw new Exception\OutOfRangeException(
  245. 'Float size must be greater than 0'
  246. );
  247. }
  248. $this->moduleSize = floatval($value);
  249. return $this;
  250. }
  251. /**
  252. * Set the size of a module
  253. * @return float
  254. */
  255. public function getModuleSize()
  256. {
  257. return $this->moduleSize;
  258. }
  259. /**
  260. * Retrieve the automatic rendering of exception
  261. * @return bool
  262. */
  263. public function getAutomaticRenderError()
  264. {
  265. return $this->automaticRenderError;
  266. }
  267. /**
  268. * Set the barcode object
  269. * @param Object\ObjectInterface $barcode
  270. * @return AbstractRenderer
  271. */
  272. public function setBarcode(Object\ObjectInterface $barcode)
  273. {
  274. $this->barcode = $barcode;
  275. return $this;
  276. }
  277. /**
  278. * Retrieve the barcode object
  279. * @return Object\ObjectInterface
  280. */
  281. public function getBarcode()
  282. {
  283. return $this->barcode;
  284. }
  285. /**
  286. * Checking of parameters after all settings
  287. * @return bool
  288. */
  289. public function checkParams()
  290. {
  291. $this->checkBarcodeObject();
  292. $this->checkSpecificParams();
  293. return true;
  294. }
  295. /**
  296. * Check if a barcode object is correctly provided
  297. * @return void
  298. * @throws Exception\RuntimeException
  299. */
  300. protected function checkBarcodeObject()
  301. {
  302. if ($this->barcode === null) {
  303. throw new Exception\RuntimeException(
  304. 'No barcode object provided'
  305. );
  306. }
  307. }
  308. /**
  309. * Calculate the left and top offset of the barcode in the
  310. * rendering support
  311. *
  312. * @param float $supportHeight
  313. * @param float $supportWidth
  314. * @return void
  315. */
  316. protected function adjustPosition($supportHeight, $supportWidth)
  317. {
  318. $barcodeHeight = $this->barcode->getHeight(true) * $this->moduleSize;
  319. if ($barcodeHeight != $supportHeight && $this->topOffset == 0) {
  320. switch ($this->verticalPosition) {
  321. case 'middle':
  322. $this->topOffset = floor(
  323. ($supportHeight - $barcodeHeight) / 2);
  324. break;
  325. case 'bottom':
  326. $this->topOffset = $supportHeight - $barcodeHeight;
  327. break;
  328. case 'top':
  329. default:
  330. $this->topOffset = 0;
  331. break;
  332. }
  333. }
  334. $barcodeWidth = $this->barcode->getWidth(true) * $this->moduleSize;
  335. if ($barcodeWidth != $supportWidth && $this->leftOffset == 0) {
  336. switch ($this->horizontalPosition) {
  337. case 'center':
  338. $this->leftOffset = floor(
  339. ($supportWidth - $barcodeWidth) / 2);
  340. break;
  341. case 'right':
  342. $this->leftOffset = $supportWidth - $barcodeWidth;
  343. break;
  344. case 'left':
  345. default:
  346. $this->leftOffset = 0;
  347. break;
  348. }
  349. }
  350. }
  351. /**
  352. * Draw the barcode in the rendering resource
  353. *
  354. * @throws BarcodeException\ExceptionInterface
  355. * @return mixed
  356. */
  357. public function draw()
  358. {
  359. try {
  360. $this->checkParams();
  361. $this->initRenderer();
  362. $this->drawInstructionList();
  363. } catch (BarcodeException\ExceptionInterface $e) {
  364. if ($this->automaticRenderError && !($e instanceof BarcodeException\RendererCreationException)) {
  365. $barcode = Barcode::makeBarcode(
  366. 'error',
  367. array('text' => $e->getMessage())
  368. );
  369. $this->setBarcode($barcode);
  370. $this->resource = null;
  371. $this->initRenderer();
  372. $this->drawInstructionList();
  373. } else {
  374. throw $e;
  375. }
  376. }
  377. return $this->resource;
  378. }
  379. /**
  380. * Sub process to draw the barcode instructions
  381. * Needed by the automatic error rendering
  382. */
  383. private function drawInstructionList()
  384. {
  385. $instructionList = $this->barcode->draw();
  386. foreach ($instructionList as $instruction) {
  387. switch ($instruction['type']) {
  388. case 'polygon':
  389. $this->drawPolygon(
  390. $instruction['points'],
  391. $instruction['color'],
  392. $instruction['filled']
  393. );
  394. break;
  395. case 'text': //$text, $size, $position, $font, $color, $alignment = 'center', $orientation = 0)
  396. $this->drawText(
  397. $instruction['text'],
  398. $instruction['size'],
  399. $instruction['position'],
  400. $instruction['font'],
  401. $instruction['color'],
  402. $instruction['alignment'],
  403. $instruction['orientation']
  404. );
  405. break;
  406. default:
  407. throw new Exception\UnexpectedValueException(
  408. 'Unkown drawing command'
  409. );
  410. }
  411. }
  412. }
  413. /**
  414. * Checking of parameters after all settings
  415. * @return void
  416. */
  417. abstract protected function checkSpecificParams();
  418. /**
  419. * Initialize the rendering resource
  420. * @return void
  421. */
  422. abstract protected function initRenderer();
  423. /**
  424. * Draw a polygon in the rendering resource
  425. * @param array $points
  426. * @param int $color
  427. * @param bool $filled
  428. */
  429. abstract protected function drawPolygon($points, $color, $filled = true);
  430. /**
  431. * Draw a polygon in the rendering resource
  432. * @param string $text
  433. * @param float $size
  434. * @param array $position
  435. * @param string $font
  436. * @param int $color
  437. * @param string $alignment
  438. * @param float $orientation
  439. */
  440. abstract protected function drawText(
  441. $text,
  442. $size,
  443. $position,
  444. $font,
  445. $color,
  446. $alignment = 'center',
  447. $orientation = 0
  448. );
  449. }