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