PageRenderTime 54ms CodeModel.GetById 27ms RepoModel.GetById 0ms app.codeStats 0ms

/lib/zend/Zend/Validate/Barcode/AdapterAbstract.php

http://github.com/moodle/moodle
PHP | 315 lines | 164 code | 36 blank | 115 comment | 37 complexity | 5bfd870401242e859ce7b56d5a4305ae MD5 | raw file
Possible License(s): MIT, AGPL-3.0, MPL-2.0-no-copyleft-exception, LGPL-3.0, GPL-3.0, Apache-2.0, LGPL-2.1, BSD-3-Clause
  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_Validate
  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$
  20. */
  21. /**
  22. * @see Zend_Validate_Barcode_AdapterInterface
  23. */
  24. require_once 'Zend/Validate/Barcode/AdapterInterface.php';
  25. /**
  26. * @category Zend
  27. * @package Zend_Validate
  28. * @copyright Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com)
  29. * @license http://framework.zend.com/license/new-bsd New BSD License
  30. */
  31. abstract class Zend_Validate_Barcode_AdapterAbstract
  32. implements Zend_Validate_Barcode_AdapterInterface
  33. {
  34. /**
  35. * Allowed barcode lengths
  36. * @var integer|array|string
  37. */
  38. protected $_length;
  39. /**
  40. * Allowed barcode characters
  41. * @var string
  42. */
  43. protected $_characters;
  44. /**
  45. * Callback to checksum function
  46. * @var string|array
  47. */
  48. protected $_checksum;
  49. /**
  50. * Is a checksum value included?
  51. * @var boolean
  52. */
  53. protected $_hasChecksum = true;
  54. /**
  55. * Checks the length of a barcode
  56. *
  57. * @param string $value The barcode to check for proper length
  58. * @return boolean
  59. */
  60. public function checkLength($value)
  61. {
  62. if (!is_string($value)) {
  63. return false;
  64. }
  65. $fixum = strlen($value);
  66. $found = false;
  67. $length = $this->getLength();
  68. if (is_array($length)) {
  69. foreach ($length as $value) {
  70. if ($fixum == $value) {
  71. $found = true;
  72. }
  73. if ($value == -1) {
  74. $found = true;
  75. }
  76. }
  77. } elseif ($fixum == $length) {
  78. $found = true;
  79. } elseif ($length == -1) {
  80. $found = true;
  81. } elseif ($length == 'even') {
  82. $count = $fixum % 2;
  83. $found = ($count == 0) ? true : false;
  84. } elseif ($length == 'odd') {
  85. $count = $fixum % 2;
  86. $found = ($count == 1) ? true : false;
  87. }
  88. return $found;
  89. }
  90. /**
  91. * Checks for allowed characters within the barcode
  92. *
  93. * @param string $value The barcode to check for allowed characters
  94. * @return boolean
  95. */
  96. public function checkChars($value)
  97. {
  98. if (!is_string($value)) {
  99. return false;
  100. }
  101. $characters = $this->getCharacters();
  102. if ($characters == 128) {
  103. for ($x = 0; $x < 128; ++$x) {
  104. $value = str_replace(chr($x), '', $value);
  105. }
  106. } else {
  107. $chars = str_split($characters);
  108. foreach ($chars as $char) {
  109. $value = str_replace($char, '', $value);
  110. }
  111. }
  112. if (strlen($value) > 0) {
  113. return false;
  114. }
  115. return true;
  116. }
  117. /**
  118. * Validates the checksum
  119. *
  120. * @param string $value The barcode to check the checksum for
  121. * @return boolean
  122. */
  123. public function checksum($value)
  124. {
  125. $checksum = $this->getChecksum();
  126. if (!empty($checksum)) {
  127. if (method_exists($this, $checksum)) {
  128. return call_user_func(array($this, $checksum), $value);
  129. }
  130. }
  131. return false;
  132. }
  133. /**
  134. * Returns the allowed barcode length
  135. *
  136. * @return string
  137. */
  138. public function getLength()
  139. {
  140. return $this->_length;
  141. }
  142. /**
  143. * Returns the allowed characters
  144. *
  145. * @return integer|string
  146. */
  147. public function getCharacters()
  148. {
  149. return $this->_characters;
  150. }
  151. /**
  152. * Returns the checksum function name
  153. *
  154. */
  155. public function getChecksum()
  156. {
  157. return $this->_checksum;
  158. }
  159. /**
  160. * Returns if barcode uses checksum
  161. *
  162. * @return boolean
  163. */
  164. public function getCheck()
  165. {
  166. return $this->_hasChecksum;
  167. }
  168. /**
  169. * Sets the checksum validation
  170. *
  171. * @param boolean $check
  172. * @return Zend_Validate_Barcode_AdapterAbstract
  173. */
  174. public function setCheck($check)
  175. {
  176. $this->_hasChecksum = (boolean) $check;
  177. return $this;
  178. }
  179. /**
  180. * Validates the checksum (Modulo 10)
  181. * GTIN implementation factor 3
  182. *
  183. * @param string $value The barcode to validate
  184. * @return boolean
  185. */
  186. protected function _gtin($value)
  187. {
  188. $barcode = substr($value, 0, -1);
  189. $sum = 0;
  190. $length = strlen($barcode) - 1;
  191. for ($i = 0; $i <= $length; $i++) {
  192. if (($i % 2) === 0) {
  193. $sum += $barcode[$length - $i] * 3;
  194. } else {
  195. $sum += $barcode[$length - $i];
  196. }
  197. }
  198. $calc = $sum % 10;
  199. $checksum = ($calc === 0) ? 0 : (10 - $calc);
  200. if ($value[$length + 1] != $checksum) {
  201. return false;
  202. }
  203. return true;
  204. }
  205. /**
  206. * Validates the checksum (Modulo 10)
  207. * IDENTCODE implementation factors 9 and 4
  208. *
  209. * @param string $value The barcode to validate
  210. * @return boolean
  211. */
  212. protected function _identcode($value)
  213. {
  214. $barcode = substr($value, 0, -1);
  215. $sum = 0;
  216. $length = strlen($value) - 2;
  217. for ($i = 0; $i <= $length; $i++) {
  218. if (($i % 2) === 0) {
  219. $sum += $barcode[$length - $i] * 4;
  220. } else {
  221. $sum += $barcode[$length - $i] * 9;
  222. }
  223. }
  224. $calc = $sum % 10;
  225. $checksum = ($calc === 0) ? 0 : (10 - $calc);
  226. if ($value[$length + 1] != $checksum) {
  227. return false;
  228. }
  229. return true;
  230. }
  231. /**
  232. * Validates the checksum (Modulo 10)
  233. * CODE25 implementation factor 3
  234. *
  235. * @param string $value The barcode to validate
  236. * @return boolean
  237. */
  238. protected function _code25($value)
  239. {
  240. $barcode = substr($value, 0, -1);
  241. $sum = 0;
  242. $length = strlen($barcode) - 1;
  243. for ($i = 0; $i <= $length; $i++) {
  244. if (($i % 2) === 0) {
  245. $sum += $barcode[$i] * 3;
  246. } else {
  247. $sum += $barcode[$i];
  248. }
  249. }
  250. $calc = $sum % 10;
  251. $checksum = ($calc === 0) ? 0 : (10 - $calc);
  252. if ($value[$length + 1] != $checksum) {
  253. return false;
  254. }
  255. return true;
  256. }
  257. /**
  258. * Validates the checksum ()
  259. * POSTNET implementation
  260. *
  261. * @param string $value The barcode to validate
  262. * @return boolean
  263. */
  264. protected function _postnet($value)
  265. {
  266. $checksum = substr($value, -1, 1);
  267. $values = str_split(substr($value, 0, -1));
  268. $check = 0;
  269. foreach($values as $row) {
  270. $check += $row;
  271. }
  272. $check %= 10;
  273. $check = 10 - $check;
  274. if ($check == $checksum) {
  275. return true;
  276. }
  277. return false;
  278. }
  279. }