Barcode.php 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227
  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_Abstract
  23. */
  24. #require_once 'Zend/Validate/Abstract.php';
  25. /**
  26. * @see Zend_Loader
  27. */
  28. #require_once 'Zend/Loader.php';
  29. /**
  30. * @category Zend
  31. * @package Zend_Validate
  32. * @copyright Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com)
  33. * @license http://framework.zend.com/license/new-bsd New BSD License
  34. */
  35. class Zend_Validate_Barcode extends Zend_Validate_Abstract
  36. {
  37. const INVALID = 'barcodeInvalid';
  38. const FAILED = 'barcodeFailed';
  39. const INVALID_CHARS = 'barcodeInvalidChars';
  40. const INVALID_LENGTH = 'barcodeInvalidLength';
  41. protected $_messageTemplates = array(
  42. self::FAILED => "'%value%' failed checksum validation",
  43. self::INVALID_CHARS => "'%value%' contains invalid characters",
  44. self::INVALID_LENGTH => "'%value%' should have a length of %length% characters",
  45. self::INVALID => "Invalid type given. String expected",
  46. );
  47. /**
  48. * Additional variables available for validation failure messages
  49. *
  50. * @var array
  51. */
  52. protected $_messageVariables = array(
  53. 'length' => '_length'
  54. );
  55. /**
  56. * Length for the set subtype
  57. *
  58. * @var integer
  59. */
  60. protected $_length;
  61. /**
  62. * Barcode adapter
  63. *
  64. * @var Zend_Validate_Barcode_BarcodeAdapter
  65. */
  66. protected $_adapter;
  67. /**
  68. * Generates the standard validator object
  69. *
  70. * @param string|Zend_Config|
  71. * Zend_Validate_Barcode_BarcodeAdapter $adapter Barcode adapter to use
  72. * @throws Zend_Validate_Exception
  73. */
  74. public function __construct($adapter)
  75. {
  76. if ($adapter instanceof Zend_Config) {
  77. $adapter = $adapter->toArray();
  78. }
  79. $options = null;
  80. $checksum = null;
  81. if (is_array($adapter)) {
  82. if (array_key_exists('options', $adapter)) {
  83. $options = $adapter['options'];
  84. }
  85. if (array_key_exists('checksum', $adapter)) {
  86. $checksum = $adapter['checksum'];
  87. }
  88. if (array_key_exists('adapter', $adapter)) {
  89. $adapter = $adapter['adapter'];
  90. } else {
  91. #require_once 'Zend/Validate/Exception.php';
  92. throw new Zend_Validate_Exception("Missing option 'adapter'");
  93. }
  94. }
  95. $this->setAdapter($adapter, $options);
  96. if ($checksum !== null) {
  97. $this->setChecksum($checksum);
  98. }
  99. }
  100. /**
  101. * Returns the set adapter
  102. *
  103. * @return Zend_Validate_Barcode_BarcodeAdapter
  104. */
  105. public function getAdapter()
  106. {
  107. return $this->_adapter;
  108. }
  109. /**
  110. * Sets a new barcode adapter
  111. *
  112. * @param string|Zend_Validate_Barcode $adapter Barcode adapter to use
  113. * @param array $options Options for this adapter
  114. * @return $this
  115. * @throws Zend_Validate_Exception
  116. */
  117. public function setAdapter($adapter, $options = null)
  118. {
  119. $adapter = ucfirst(strtolower($adapter));
  120. #require_once 'Zend/Loader.php';
  121. if (Zend_Loader::isReadable('Zend/Validate/Barcode/' . $adapter. '.php')) {
  122. $adapter = 'Zend_Validate_Barcode_' . $adapter;
  123. }
  124. if (!class_exists($adapter)) {
  125. Zend_Loader::loadClass($adapter);
  126. }
  127. $this->_adapter = new $adapter($options);
  128. if (!$this->_adapter instanceof Zend_Validate_Barcode_AdapterInterface) {
  129. #require_once 'Zend/Validate/Exception.php';
  130. throw new Zend_Validate_Exception(
  131. "Adapter " . $adapter . " does not implement Zend_Validate_Barcode_AdapterInterface"
  132. );
  133. }
  134. return $this;
  135. }
  136. /**
  137. * Returns the checksum option
  138. *
  139. * @return boolean
  140. */
  141. public function getChecksum()
  142. {
  143. return $this->getAdapter()->getCheck();
  144. }
  145. /**
  146. * Sets the checksum option
  147. *
  148. * @param boolean $checksum
  149. * @return Zend_Validate_Barcode
  150. */
  151. public function setChecksum($checksum)
  152. {
  153. $this->getAdapter()->setCheck($checksum);
  154. return $this;
  155. }
  156. /**
  157. * Defined by Zend_Validate_Interface
  158. *
  159. * Returns true if and only if $value contains a valid barcode
  160. *
  161. * @param string $value
  162. * @return boolean
  163. */
  164. public function isValid($value)
  165. {
  166. if (!is_string($value)) {
  167. $this->_error(self::INVALID);
  168. return false;
  169. }
  170. $this->_setValue($value);
  171. $adapter = $this->getAdapter();
  172. $this->_length = $adapter->getLength();
  173. $result = $adapter->checkLength($value);
  174. if (!$result) {
  175. if (is_array($this->_length)) {
  176. $temp = $this->_length;
  177. $this->_length = "";
  178. foreach($temp as $length) {
  179. $this->_length .= "/";
  180. $this->_length .= $length;
  181. }
  182. $this->_length = substr($this->_length, 1);
  183. }
  184. $this->_error(self::INVALID_LENGTH);
  185. return false;
  186. }
  187. $result = $adapter->checkChars($value);
  188. if (!$result) {
  189. $this->_error(self::INVALID_CHARS);
  190. return false;
  191. }
  192. if ($this->getChecksum()) {
  193. $result = $adapter->checksum($value);
  194. if (!$result) {
  195. $this->_error(self::FAILED);
  196. return false;
  197. }
  198. }
  199. return true;
  200. }
  201. }