Royalmail.php 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  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-2015 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_AdapterAbstract
  23. */
  24. #require_once 'Zend/Validate/Barcode/AdapterAbstract.php';
  25. /**
  26. * @category Zend
  27. * @package Zend_Validate
  28. * @copyright Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com)
  29. * @license http://framework.zend.com/license/new-bsd New BSD License
  30. */
  31. class Zend_Validate_Barcode_Royalmail extends Zend_Validate_Barcode_AdapterAbstract
  32. {
  33. /**
  34. * Allowed barcode lengths
  35. * @var integer
  36. */
  37. protected $_length = -1;
  38. /**
  39. * Allowed barcode characters
  40. * @var string
  41. */
  42. protected $_characters = '0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ';
  43. protected $_rows = array(
  44. '0' => 1, '1' => 1, '2' => 1, '3' => 1, '4' => 1, '5' => 1,
  45. '6' => 2, '7' => 2, '8' => 2, '9' => 2, 'A' => 2, 'B' => 2,
  46. 'C' => 3, 'D' => 3, 'E' => 3, 'F' => 3, 'G' => 3, 'H' => 3,
  47. 'I' => 4, 'J' => 4, 'K' => 4, 'L' => 4, 'M' => 4, 'N' => 4,
  48. 'O' => 5, 'P' => 5, 'Q' => 5, 'R' => 5, 'S' => 5, 'T' => 5,
  49. 'U' => 0, 'V' => 0, 'W' => 0, 'X' => 0, 'Y' => 0, 'Z' => 0,
  50. );
  51. protected $_columns = array(
  52. '0' => 1, '1' => 2, '2' => 3, '3' => 4, '4' => 5, '5' => 0,
  53. '6' => 1, '7' => 2, '8' => 3, '9' => 4, 'A' => 5, 'B' => 0,
  54. 'C' => 1, 'D' => 2, 'E' => 3, 'F' => 4, 'G' => 5, 'H' => 0,
  55. 'I' => 1, 'J' => 2, 'K' => 3, 'L' => 4, 'M' => 5, 'N' => 0,
  56. 'O' => 1, 'P' => 2, 'Q' => 3, 'R' => 4, 'S' => 5, 'T' => 0,
  57. 'U' => 1, 'V' => 2, 'W' => 3, 'X' => 4, 'Y' => 5, 'Z' => 0,
  58. );
  59. /**
  60. * Checksum function
  61. * @var string
  62. */
  63. protected $_checksum = '_royalmail';
  64. /**
  65. * Validates the checksum ()
  66. *
  67. * @param string $value The barcode to validate
  68. * @return boolean
  69. */
  70. protected function _royalmail($value)
  71. {
  72. $checksum = substr($value, -1, 1);
  73. $values = str_split(substr($value, 0, -1));
  74. $rowvalue = 0;
  75. $colvalue = 0;
  76. foreach($values as $row) {
  77. $rowvalue += $this->_rows[$row];
  78. $colvalue += $this->_columns[$row];
  79. }
  80. $rowvalue %= 6;
  81. $colvalue %= 6;
  82. $rowchkvalue = array_keys($this->_rows, $rowvalue);
  83. $colchkvalue = array_keys($this->_columns, $colvalue);
  84. $chkvalue = current(array_intersect($rowchkvalue, $colchkvalue));
  85. if ($chkvalue == $checksum) {
  86. return true;
  87. }
  88. return false;
  89. }
  90. /**
  91. * Allows start and stop tag within checked chars
  92. *
  93. * @param string $value The barcode to check for allowed characters
  94. * @return boolean
  95. */
  96. public function checkChars($value)
  97. {
  98. if ($value[0] == '(') {
  99. $value = substr($value, 1);
  100. if ($value[strlen($value) - 1] == ')') {
  101. $value = substr($value, 0, -1);
  102. } else {
  103. return false;
  104. }
  105. }
  106. return parent::checkChars($value);
  107. }
  108. }