/www/libs/Zend/Validate/Barcode.php

https://github.com/Riges/KawaiViewModel · PHP · 228 lines · 116 code · 26 blank · 86 comment · 16 complexity · 87f62635f7cf60c66d453ac1567f0ea2 MD5 · raw file

  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-2012 Zend Technologies USA Inc. (http://www.zend.com)
  18. * @license http://framework.zend.com/license/new-bsd New BSD License
  19. * @version $Id: Barcode.php 24593 2012-01-05 20:35:02Z matthew $
  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-2012 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. * @return void
  73. * @throws Zend_Validate_Exception
  74. */
  75. public function __construct($adapter)
  76. {
  77. if ($adapter instanceof Zend_Config) {
  78. $adapter = $adapter->toArray();
  79. }
  80. $options = null;
  81. $checksum = null;
  82. if (is_array($adapter)) {
  83. if (array_key_exists('options', $adapter)) {
  84. $options = $adapter['options'];
  85. }
  86. if (array_key_exists('checksum', $adapter)) {
  87. $checksum = $adapter['checksum'];
  88. }
  89. if (array_key_exists('adapter', $adapter)) {
  90. $adapter = $adapter['adapter'];
  91. } else {
  92. require_once 'Zend/Validate/Exception.php';
  93. throw new Zend_Validate_Exception("Missing option 'adapter'");
  94. }
  95. }
  96. $this->setAdapter($adapter, $options);
  97. if ($checksum !== null) {
  98. $this->setChecksum($checksum);
  99. }
  100. }
  101. /**
  102. * Returns the set adapter
  103. *
  104. * @return Zend_Validate_Barcode_BarcodeAdapter
  105. */
  106. public function getAdapter()
  107. {
  108. return $this->_adapter;
  109. }
  110. /**
  111. * Sets a new barcode adapter
  112. *
  113. * @param string|Zend_Validate_Barcode $adapter Barcode adapter to use
  114. * @param array $options Options for this adapter
  115. * @return void
  116. * @throws Zend_Validate_Exception
  117. */
  118. public function setAdapter($adapter, $options = null)
  119. {
  120. $adapter = ucfirst(strtolower($adapter));
  121. require_once 'Zend/Loader.php';
  122. if (Zend_Loader::isReadable('Zend/Validate/Barcode/' . $adapter. '.php')) {
  123. $adapter = 'Zend_Validate_Barcode_' . $adapter;
  124. }
  125. if (!class_exists($adapter)) {
  126. Zend_Loader::loadClass($adapter);
  127. }
  128. $this->_adapter = new $adapter($options);
  129. if (!$this->_adapter instanceof Zend_Validate_Barcode_AdapterInterface) {
  130. require_once 'Zend/Validate/Exception.php';
  131. throw new Zend_Validate_Exception(
  132. "Adapter " . $adapter . " does not implement Zend_Validate_Barcode_AdapterInterface"
  133. );
  134. }
  135. return $this;
  136. }
  137. /**
  138. * Returns the checksum option
  139. *
  140. * @return boolean
  141. */
  142. public function getChecksum()
  143. {
  144. return $this->getAdapter()->getCheck();
  145. }
  146. /**
  147. * Sets the checksum option
  148. *
  149. * @param boolean $checksum
  150. * @return Zend_Validate_Barcode
  151. */
  152. public function setChecksum($checksum)
  153. {
  154. $this->getAdapter()->setCheck($checksum);
  155. return $this;
  156. }
  157. /**
  158. * Defined by Zend_Validate_Interface
  159. *
  160. * Returns true if and only if $value contains a valid barcode
  161. *
  162. * @param string $value
  163. * @return boolean
  164. */
  165. public function isValid($value)
  166. {
  167. if (!is_string($value)) {
  168. $this->_error(self::INVALID);
  169. return false;
  170. }
  171. $this->_setValue($value);
  172. $adapter = $this->getAdapter();
  173. $this->_length = $adapter->getLength();
  174. $result = $adapter->checkLength($value);
  175. if (!$result) {
  176. if (is_array($this->_length)) {
  177. $temp = $this->_length;
  178. $this->_length = "";
  179. foreach($temp as $length) {
  180. $this->_length .= "/";
  181. $this->_length .= $length;
  182. }
  183. $this->_length = substr($this->_length, 1);
  184. }
  185. $this->_error(self::INVALID_LENGTH);
  186. return false;
  187. }
  188. $result = $adapter->checkChars($value);
  189. if (!$result) {
  190. $this->_error(self::INVALID_CHARS);
  191. return false;
  192. }
  193. if ($this->getChecksum()) {
  194. $result = $adapter->checksum($value);
  195. if (!$result) {
  196. $this->_error(self::FAILED);
  197. return false;
  198. }
  199. }
  200. return true;
  201. }
  202. }