PageRenderTime 28ms CodeModel.GetById 0ms RepoModel.GetById 0ms app.codeStats 0ms

/library/Zend/Validator/Barcode/AbstractAdapter.php

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