File indexing completed on 2024-12-22 05:37:12
0001 <?php 0002 /** 0003 * Zend Framework 0004 * 0005 * LICENSE 0006 * 0007 * This source file is subject to the new BSD license that is bundled 0008 * with this package in the file LICENSE.txt. 0009 * It is also available through the world-wide-web at this URL: 0010 * http://framework.zend.com/license/new-bsd 0011 * If you did not receive a copy of the license and are unable to 0012 * obtain it through the world-wide-web, please send an email 0013 * to license@zend.com so we can send you a copy immediately. 0014 * 0015 * @category Zend 0016 * @package Zend_Validate 0017 * @copyright Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com) 0018 * @license http://framework.zend.com/license/new-bsd New BSD License 0019 * @version $Id$ 0020 */ 0021 0022 /** 0023 * @see Zend_Validate_Abstract 0024 */ 0025 // require_once 'Zend/Validate/Abstract.php'; 0026 0027 /** 0028 * @see Zend_Loader 0029 */ 0030 // require_once 'Zend/Loader.php'; 0031 0032 /** 0033 * @category Zend 0034 * @package Zend_Validate 0035 * @copyright Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com) 0036 * @license http://framework.zend.com/license/new-bsd New BSD License 0037 */ 0038 class Zend_Validate_Barcode extends Zend_Validate_Abstract 0039 { 0040 const INVALID = 'barcodeInvalid'; 0041 const FAILED = 'barcodeFailed'; 0042 const INVALID_CHARS = 'barcodeInvalidChars'; 0043 const INVALID_LENGTH = 'barcodeInvalidLength'; 0044 0045 protected $_messageTemplates = array( 0046 self::FAILED => "'%value%' failed checksum validation", 0047 self::INVALID_CHARS => "'%value%' contains invalid characters", 0048 self::INVALID_LENGTH => "'%value%' should have a length of %length% characters", 0049 self::INVALID => "Invalid type given. String expected", 0050 ); 0051 0052 /** 0053 * Additional variables available for validation failure messages 0054 * 0055 * @var array 0056 */ 0057 protected $_messageVariables = array( 0058 'length' => '_length' 0059 ); 0060 0061 /** 0062 * Length for the set subtype 0063 * 0064 * @var integer 0065 */ 0066 protected $_length; 0067 0068 /** 0069 * Barcode adapter 0070 * 0071 * @var Zend_Validate_Barcode_BarcodeAdapter 0072 */ 0073 protected $_adapter; 0074 0075 /** 0076 * Generates the standard validator object 0077 * 0078 * @param string|Zend_Config| 0079 * Zend_Validate_Barcode_BarcodeAdapter $adapter Barcode adapter to use 0080 * @throws Zend_Validate_Exception 0081 */ 0082 public function __construct($adapter) 0083 { 0084 if ($adapter instanceof Zend_Config) { 0085 $adapter = $adapter->toArray(); 0086 } 0087 0088 $options = null; 0089 $checksum = null; 0090 if (is_array($adapter)) { 0091 if (array_key_exists('options', $adapter)) { 0092 $options = $adapter['options']; 0093 } 0094 0095 if (array_key_exists('checksum', $adapter)) { 0096 $checksum = $adapter['checksum']; 0097 } 0098 0099 if (array_key_exists('adapter', $adapter)) { 0100 $adapter = $adapter['adapter']; 0101 } else { 0102 // require_once 'Zend/Validate/Exception.php'; 0103 throw new Zend_Validate_Exception("Missing option 'adapter'"); 0104 } 0105 } 0106 0107 $this->setAdapter($adapter, $options); 0108 if ($checksum !== null) { 0109 $this->setChecksum($checksum); 0110 } 0111 } 0112 0113 /** 0114 * Returns the set adapter 0115 * 0116 * @return Zend_Validate_Barcode_BarcodeAdapter 0117 */ 0118 public function getAdapter() 0119 { 0120 return $this->_adapter; 0121 } 0122 0123 /** 0124 * Sets a new barcode adapter 0125 * 0126 * @param string|Zend_Validate_Barcode $adapter Barcode adapter to use 0127 * @param array $options Options for this adapter 0128 * @return $this 0129 * @throws Zend_Validate_Exception 0130 */ 0131 public function setAdapter($adapter, $options = null) 0132 { 0133 $adapter = ucfirst(strtolower($adapter)); 0134 // require_once 'Zend/Loader.php'; 0135 if (Zend_Loader::isReadable('Zend/Validate/Barcode/' . $adapter. '.php')) { 0136 $adapter = 'Zend_Validate_Barcode_' . $adapter; 0137 } 0138 0139 if (!class_exists($adapter)) { 0140 Zend_Loader::loadClass($adapter); 0141 } 0142 0143 $this->_adapter = new $adapter($options); 0144 if (!$this->_adapter instanceof Zend_Validate_Barcode_AdapterInterface) { 0145 // require_once 'Zend/Validate/Exception.php'; 0146 throw new Zend_Validate_Exception( 0147 "Adapter " . $adapter . " does not implement Zend_Validate_Barcode_AdapterInterface" 0148 ); 0149 } 0150 0151 return $this; 0152 } 0153 0154 /** 0155 * Returns the checksum option 0156 * 0157 * @return boolean 0158 */ 0159 public function getChecksum() 0160 { 0161 return $this->getAdapter()->getCheck(); 0162 } 0163 0164 /** 0165 * Sets the checksum option 0166 * 0167 * @param boolean $checksum 0168 * @return Zend_Validate_Barcode 0169 */ 0170 public function setChecksum($checksum) 0171 { 0172 $this->getAdapter()->setCheck($checksum); 0173 return $this; 0174 } 0175 0176 /** 0177 * Defined by Zend_Validate_Interface 0178 * 0179 * Returns true if and only if $value contains a valid barcode 0180 * 0181 * @param string $value 0182 * @return boolean 0183 */ 0184 public function isValid($value) 0185 { 0186 if (!is_string($value)) { 0187 $this->_error(self::INVALID); 0188 return false; 0189 } 0190 0191 $this->_setValue($value); 0192 $adapter = $this->getAdapter(); 0193 $this->_length = $adapter->getLength(); 0194 $result = $adapter->checkLength($value); 0195 if (!$result) { 0196 if (is_array($this->_length)) { 0197 $temp = $this->_length; 0198 $this->_length = ""; 0199 foreach($temp as $length) { 0200 $this->_length .= "/"; 0201 $this->_length .= $length; 0202 } 0203 0204 $this->_length = substr($this->_length, 1); 0205 } 0206 0207 $this->_error(self::INVALID_LENGTH); 0208 return false; 0209 } 0210 0211 $result = $adapter->checkChars($value); 0212 if (!$result) { 0213 $this->_error(self::INVALID_CHARS); 0214 return false; 0215 } 0216 0217 if ($this->getChecksum()) { 0218 $result = $adapter->checksum($value); 0219 if (!$result) { 0220 $this->_error(self::FAILED); 0221 return false; 0222 } 0223 } 0224 0225 return true; 0226 } 0227 }