PageRenderTime 53ms CodeModel.GetById 25ms RepoModel.GetById 0ms app.codeStats 0ms

/library/Zend/Matrixcode/Abstract.php

https://github.com/lamminpaa/iBooker
PHP | 389 lines | 160 code | 53 blank | 176 comment | 18 complexity | f6f3fb63debd3ac1ac87cc8c1e9af55f MD5 | raw file
  1. <?php
  2. /**
  3. * Zend Framework
  4. *
  5. * LICENSE
  6. *
  7. * This source file is subject to version 1.0 of the Zend Framework
  8. * license, that is bundled with this package in the file LICENSE.txt, and
  9. * is available through the world-wide-web at the following URL:
  10. * http://framework.zend.com/license/new-bsd. If you did not receive
  11. * a copy of the Zend Framework license and are unable to obtain it
  12. * through the world-wide-web, please send a note to license@zend.com
  13. * so we can mail you a copy immediately.
  14. *
  15. * @package Zend_Matrixcode
  16. * @copyright Copyright (c) 2009-2011 Peter Minne <peter@inthepocket.mobi>
  17. * @license http://framework.zend.com/license/new-bsd New BSD License
  18. */
  19. /**
  20. * Zend_Matrixcode_Abstract
  21. *
  22. * @package Zend_Matrixcode
  23. * @copyright Copyright (c) 2009-2011 Peter Minne <peter@inthepocket.mobi>
  24. * @license http://framework.zend.com/license/new-bsd New BSD License
  25. */
  26. abstract class Zend_Matrixcode_Abstract
  27. {
  28. /**
  29. * Namespace of the matrixcode for autoloading
  30. * @var string
  31. */
  32. protected $_matrixcodeNamespace = 'Zend_Matrixcode';
  33. /**
  34. * Matrixcode type
  35. * @var string
  36. */
  37. protected $_type = null;
  38. /**
  39. * Size (in units) of a single matrixcode module (the 'black dots')
  40. * @var array array(width, height)
  41. */
  42. protected $_module_size = array(1,1);
  43. /**
  44. * Calculated width of the code
  45. * @var int
  46. */
  47. protected $_calculated_width;
  48. /**
  49. * Calculated height of the code
  50. * @var int
  51. */
  52. protected $_calculated_height;
  53. /**
  54. * Symbol color (fore color)
  55. * @var integer
  56. */
  57. protected $_fr_color = 0x000000;
  58. /**
  59. * Background color of the code (transparent if empty)
  60. * @var integer
  61. */
  62. protected $_bg_color;
  63. /**
  64. * Activate/deactivate border of the image
  65. * @var boolean
  66. */
  67. protected $_with_border = false;
  68. /**
  69. * Spacing between code and image borders. A value of '1' means a padding equal to the size of one module.
  70. * @var array array(top, right, bottom, left)
  71. */
  72. protected $_padding = array(1 , 1 , 1 , 1);
  73. /**
  74. * Text to encode
  75. * @var string
  76. */
  77. protected $_text = null;
  78. /**
  79. * Array containing the modules
  80. * (2 dimensional array representing the 2 dimensional code)
  81. * @var array
  82. */
  83. protected $_matrix_table = null;
  84. /**
  85. * Constructor
  86. * @param array | Zend_Config $options
  87. * @return void
  88. */
  89. public function __construct ($options = null)
  90. {
  91. if (is_array($options)) {
  92. $this->setOptions($options);
  93. } elseif ($options instanceof Zend_Config) {
  94. $this->setConfig($options);
  95. }
  96. $this->_type = strtolower(substr(get_class($this), strlen($this->_matrixcodeNamespace) + 1));
  97. }
  98. /**
  99. * Set matrixcode state from options array
  100. * @param Zend_Config $config
  101. * @return Zend_Matrixcode_Abstract
  102. */
  103. public function setOptions($options)
  104. {
  105. foreach ($options as $key => $value) {
  106. $normalized = ucfirst($key);
  107. $method = 'set' . $normalized;
  108. if (method_exists($this, $method)) {
  109. $this->$method($value);
  110. }
  111. }
  112. return $this;
  113. }
  114. /**
  115. * Set matrixcode state from config object
  116. * @param Zend_Config $config
  117. * @return Zend_Matrixcode_Abstract
  118. */
  119. public function setConfig(Zend_Config $config)
  120. {
  121. return $this->setOptions($config->toArray());
  122. }
  123. /**
  124. * Retrieve type of matrixcode
  125. * @return string
  126. */
  127. public function getType()
  128. {
  129. return $this->_type;
  130. }
  131. /**
  132. * Set module size
  133. * @param int | array $size
  134. */
  135. public function setModuleSize($value)
  136. {
  137. if(is_array($value) && count($value) == 2) {
  138. $this->_module_size = $value;
  139. }else if(is_int($value)){
  140. $this->_module_size = array($value,$value);
  141. }else{
  142. require_once 'Zend/Matrixcode/Exception.php';
  143. throw new Zend_Matrixcode_Exception(
  144. 'Invalid module size'
  145. );
  146. }
  147. return $this;
  148. }
  149. /**
  150. * Retrieve module size
  151. * @return array
  152. */
  153. public function getModuleSize()
  154. {
  155. return $this->_module_size;
  156. }
  157. /**
  158. * Set color of the code
  159. * @param string $value
  160. * @return Zend_Matrixcode_Abstract
  161. * @throw Zend_Matrixcode_Exception
  162. */
  163. public function setForeColor($value)
  164. {
  165. if (preg_match('`\#[0-9A-Fa-f]{6}`', $value)) {
  166. $this->_fr_color = hexdec($value);
  167. } elseif (is_numeric($value) && $value >= 0 && $value <= 16777125) {
  168. $this->_fr_color = intval($value);
  169. } else {
  170. require_once 'Zend/Matrixcode/Exception.php';
  171. throw new Zend_Matrixcode_Exception(
  172. 'Fore color must be set as #[0-9A-Fa-f]{6}'
  173. );
  174. }
  175. return $this;
  176. }
  177. /**
  178. * Retrieve color of the code
  179. * @return string
  180. */
  181. public function getForeColor()
  182. {
  183. return $this->_fr_color;
  184. }
  185. /**
  186. * Set the color of the background
  187. * @param integer $value
  188. * @return Zend_Matrixcode_Abstract
  189. * @throw Zend_Matrixcode_Exception
  190. */
  191. public function setBackgroundColor($value)
  192. {
  193. if (preg_match('`\#[0-9A-F]{6}`', $value)) {
  194. $this->_bg_color = hexdec($value);
  195. } elseif (is_numeric($value) && $value >= 0 && $value <= 16777125) {
  196. $this->_bg_color = intval($value);
  197. } else {
  198. require_once 'Zend/Matrixcode/Exception.php';
  199. throw new Zend_Matrixcode_Exception(
  200. 'Background color must be set as #[0-9A-F]{6}'
  201. );
  202. }
  203. return $this;
  204. }
  205. /**
  206. * Retrieve background color
  207. * @return integer
  208. */
  209. public function getBackgroundColor()
  210. {
  211. return $this->_bg_color;
  212. }
  213. /**
  214. * Activate/deactivate drawing of a border
  215. * @param boolean $value
  216. * @return Zend_Matrixcode_Abstract
  217. */
  218. public function setWithBorder($value)
  219. {
  220. $this->_withBorder = (bool) $value;
  221. return $this;
  222. }
  223. /**
  224. * Return if border needs to be drawn or not
  225. * @return boolean
  226. */
  227. public function getWithBorder()
  228. {
  229. return $this->_withBorder;
  230. }
  231. /**
  232. * Set the padding
  233. * @param float | array $value
  234. * @return Zend_Matrixcode_Abstract
  235. */
  236. public function setPadding($value)
  237. {
  238. if(is_array($value) && count($value) == 4) {
  239. $this->_padding = $value;
  240. }else if(is_int($value)){
  241. $this->_padding = array($value,$value,$value,$value);
  242. }else{
  243. require_once 'Zend/Matrixcode/Exception.php';
  244. throw new Zend_Matrixcode_Exception(
  245. 'Invalid padding value'
  246. );
  247. }
  248. return $this;
  249. }
  250. /**
  251. * Retrieve the padding
  252. * @return array
  253. */
  254. public function getPadding()
  255. {
  256. return $this->_padding;
  257. }
  258. /**
  259. * Set text to encode
  260. * @param string $value
  261. * @return Zend_Matrixcode_Abstract
  262. */
  263. public function setText($value)
  264. {
  265. $this->_text = trim($value);
  266. return $this;
  267. }
  268. /**
  269. * Retrieve text to encode
  270. * @return string
  271. */
  272. public function getText()
  273. {
  274. return $this->_text;
  275. }
  276. /**
  277. * Set the calculated width
  278. * @param float $width
  279. */
  280. protected function _setCalculatedWidth($value)
  281. {
  282. $this->_calculated_width = $value;
  283. }
  284. /**
  285. * Set the calculated height
  286. * @param float $height
  287. */
  288. protected function _setCalculatedHeight($value)
  289. {
  290. $this->_calculated_height = $value;
  291. }
  292. /**
  293. * Retrieve the calculated width of the code
  294. * @return float
  295. */
  296. public function getWidth()
  297. {
  298. return $this->_calculated_width;
  299. }
  300. /**
  301. * Retrieve the calculated height of the code
  302. * @return float
  303. */
  304. public function getHeight()
  305. {
  306. return $this->_calculated_height;
  307. }
  308. /**
  309. * Retrieve the matrix
  310. * @return array
  311. */
  312. public function getMatrix()
  313. {
  314. return $this->_matrix_table;
  315. }
  316. /**
  317. * Complete drawing of the matrixcode
  318. * @return resource
  319. */
  320. public function draw ()
  321. {
  322. $this->_checkParams();
  323. $this->_matrix_table = $this->_prepareMatrixcode();
  324. }
  325. /**
  326. * Checking of parameters after all settings
  327. *
  328. * @return void
  329. */
  330. abstract protected function _checkParams();
  331. /**
  332. * Method that prepares the matrix
  333. * @return array
  334. */
  335. abstract protected function _prepareMatrixcode();
  336. }