Ean13.php 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  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-2009 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_Abstract
  23. */
  24. require_once 'Zend/Validate/Abstract.php';
  25. /**
  26. * @category Zend
  27. * @package Zend_Validate
  28. * @copyright Copyright (c) 2005-2009 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_Ean13 extends Zend_Validate_Abstract
  32. {
  33. /**
  34. * Validation failure message key for when the value is
  35. * an invalid barcode
  36. */
  37. const INVALID = 'ean13Invalid';
  38. /**
  39. * Validation failure message key for when the value is
  40. * not 13 characters long
  41. */
  42. const INVALID_LENGTH = 'ean13InvalidLength';
  43. /**
  44. * Validation failure message key for when the value
  45. * does not only contain numeric characters
  46. */
  47. const NOT_NUMERIC = 'ean13NotNumeric';
  48. /**
  49. * Validation failure message template definitions
  50. *
  51. * @var array
  52. */
  53. protected $_messageTemplates = array(
  54. self::INVALID => "'%value%' is an invalid EAN-13 barcode",
  55. self::INVALID_LENGTH => "'%value%' should be 13 characters",
  56. self::NOT_NUMERIC => "'%value%' should contain only numeric characters",
  57. );
  58. /**
  59. * Defined by Zend_Validate_Interface
  60. *
  61. * Returns true if and only if $value contains a valid barcode
  62. *
  63. * @param string $value
  64. * @return boolean
  65. */
  66. public function isValid($value)
  67. {
  68. if (!is_string($value) || !ctype_digit($value)) {
  69. $this->_error(self::NOT_NUMERIC);
  70. return false;
  71. }
  72. $this->_setValue($value);
  73. if (strlen($value) !== 13) {
  74. $this->_error(self::INVALID_LENGTH);
  75. return false;
  76. }
  77. $barcode = strrev(substr($value, 0, -1));
  78. $oddSum = 0;
  79. $evenSum = 0;
  80. for ($i = 0; $i < 12; $i++) {
  81. if ($i % 2 === 0) {
  82. $oddSum += $barcode[$i] * 3;
  83. } elseif ($i % 2 === 1) {
  84. $evenSum += $barcode[$i];
  85. }
  86. }
  87. $calculation = ($oddSum + $evenSum) % 10;
  88. $checksum = ($calculation === 0) ? 0 : 10 - $calculation;
  89. if ($value[12] != $checksum) {
  90. $this->_error(self::INVALID);
  91. return false;
  92. }
  93. return true;
  94. }
  95. }