Barcode.php 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  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 extends Zend_Validate_Abstract
  32. {
  33. /**
  34. * Barcode validator
  35. *
  36. * @var Zend_Validate_Abstract
  37. */
  38. protected $_barcodeValidator;
  39. /**
  40. * Generates the standard validator object
  41. *
  42. * @param string|Zend_Config $barcode Barcode validator to use
  43. * @return void
  44. * @throws Zend_Validate_Exception
  45. */
  46. public function __construct($barcode)
  47. {
  48. if ($barcode instanceof Zend_Config) {
  49. $barcode = $barcode->toArray();
  50. if (array_key_exists('barcode', $barcode)) {
  51. $barcode = $barcode['barcode'];
  52. } else {
  53. require_once 'Zend/Validate/Exception.php';
  54. throw new Zend_Validate_Exception("Missing option 'barcode'");
  55. }
  56. }
  57. $this->setType($barcode);
  58. }
  59. /**
  60. * Sets a new barcode validator
  61. *
  62. * @param string $barcode - Barcode validator to use
  63. * @return void
  64. * @throws Zend_Validate_Exception
  65. */
  66. public function setType($barcode)
  67. {
  68. switch (strtolower($barcode)) {
  69. case 'upc':
  70. case 'upc-a':
  71. require_once 'Zend/Validate/Barcode/UpcA.php';
  72. $class = 'Zend_Validate_Barcode_UpcA';
  73. break;
  74. case 'ean13':
  75. case 'ean-13':
  76. require_once 'Zend/Validate/Barcode/Ean13.php';
  77. $class = 'Zend_Validate_Barcode_Ean13';
  78. break;
  79. default:
  80. require_once 'Zend/Validate/Exception.php';
  81. throw new Zend_Validate_Exception("Barcode type '$barcode' is not supported'");
  82. break;
  83. }
  84. $this->_barcodeValidator = new $class;
  85. }
  86. /**
  87. * Defined by Zend_Validate_Interface
  88. *
  89. * Returns true if and only if $value contains a valid barcode
  90. *
  91. * @param string $value
  92. * @return boolean
  93. */
  94. public function isValid($value)
  95. {
  96. return call_user_func(array($this->_barcodeValidator, 'isValid'), $value);
  97. }
  98. }