[ 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 * Class for Database record validation 29 * 30 * @category Zend 31 * @package Zend_Validate 32 * @uses Zend_Validate_Abstract 33 * @copyright Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com) 34 * @license http://framework.zend.com/license/new-bsd New BSD License 35 */ 36 abstract class Zend_Validate_Db_Abstract extends Zend_Validate_Abstract 37 { 38 /** 39 * Error constants 40 */ 41 const ERROR_NO_RECORD_FOUND = 'noRecordFound'; 42 const ERROR_RECORD_FOUND = 'recordFound'; 43 44 /** 45 * @var array Message templates 46 */ 47 protected $_messageTemplates = array( 48 self::ERROR_NO_RECORD_FOUND => 'No record matching %value% was found', 49 self::ERROR_RECORD_FOUND => 'A record matching %value% was found', 50 ); 51 52 /** 53 * @var string 54 */ 55 protected $_schema = null; 56 57 /** 58 * @var string 59 */ 60 protected $_table = ''; 61 62 /** 63 * @var string 64 */ 65 protected $_field = ''; 66 67 /** 68 * @var mixed 69 */ 70 protected $_exclude = null; 71 72 /** 73 * Database adapter to use. If null isValid() will use Zend_Db::getInstance instead 74 * 75 * @var unknown_type 76 */ 77 protected $_adapter = null; 78 79 /** 80 * Provides basic configuration for use with Zend_Validate_Db Validators 81 * Setting $exclude allows a single record to be excluded from matching. 82 * Exclude can either be a String containing a where clause, or an array with `field` and `value` keys 83 * to define the where clause added to the sql. 84 * A database adapter may optionally be supplied to avoid using the registered default adapter. 85 * 86 * The following option keys are supported: 87 * 'table' => The database table to validate against 88 * 'schema' => The schema keys 89 * 'field' => The field to check for a match 90 * 'exclude' => An optional where clause or field/value pair to exclude from the query 91 * 'adapter' => An optional database adapter to use 92 * 93 * @param array|Zend_Config $options Options to use for this validator 94 */ 95 public function __construct($options) 96 { 97 if ($options instanceof Zend_Config) { 98 $options = $options->toArray(); 99 } else if (func_num_args() > 1) { 100 $options = func_get_args(); 101 $temp['table'] = array_shift($options); 102 $temp['field'] = array_shift($options); 103 if (!empty($options)) { 104 $temp['exclude'] = array_shift($options); 105 } 106 107 if (!empty($options)) { 108 $temp['adapter'] = array_shift($options); 109 } 110 111 $options = $temp; 112 } 113 114 if (!array_key_exists('table', $options) && !array_key_exists('schema', $options)) { 115 require_once 'Zend/Validate/Exception.php'; 116 throw new Zend_Validate_Exception('Table or Schema option missing!'); 117 } 118 119 if (!array_key_exists('field', $options)) { 120 require_once 'Zend/Validate/Exception.php'; 121 throw new Zend_Validate_Exception('Field option missing!'); 122 } 123 124 if (array_key_exists('adapter', $options)) { 125 $this->setAdapter($options['adapter']); 126 } 127 128 if (array_key_exists('exclude', $options)) { 129 $this->setExclude($options['exclude']); 130 } 131 132 $this->setField($options['field']); 133 if (array_key_exists('table', $options)) { 134 $this->setTable($options['table']); 135 } 136 137 if (array_key_exists('schema', $options)) { 138 $this->setSchema($options['schema']); 139 } 140 } 141 142 /** 143 * Returns the set adapter 144 * 145 * @return Zend_Db_Adapter 146 */ 147 public function getAdapter() 148 { 149 return $this->_adapter; 150 } 151 152 /** 153 * Sets a new database adapter 154 * 155 * @param Zend_Db_Adapter_Abstract $adapter 156 * @return Zend_Validate_Db_Abstract 157 */ 158 public function setAdapter($adapter) 159 { 160 if (!($adapter instanceof Zend_Db_Adapter_Abstract)) { 161 require_once 'Zend/Validate/Exception.php'; 162 throw new Zend_Validate_Exception('Adapter option must be a database adapter!'); 163 } 164 165 $this->_adapter = $adapter; 166 return $this; 167 } 168 169 /** 170 * Returns the set exclude clause 171 * 172 * @return string|array 173 */ 174 public function getExclude() 175 { 176 return $this->_exclude; 177 } 178 179 /** 180 * Sets a new exclude clause 181 * 182 * @param string|array $exclude 183 * @return Zend_Validate_Db_Abstract 184 */ 185 public function setExclude($exclude) 186 { 187 $this->_exclude = $exclude; 188 return $this; 189 } 190 191 /** 192 * Returns the set field 193 * 194 * @return string|array 195 */ 196 public function getField() 197 { 198 return $this->_field; 199 } 200 201 /** 202 * Sets a new field 203 * 204 * @param string $field 205 * @return Zend_Validate_Db_Abstract 206 */ 207 public function setField($field) 208 { 209 $this->_field = (string) $field; 210 return $this; 211 } 212 213 /** 214 * Returns the set table 215 * 216 * @return string 217 */ 218 public function getTable() 219 { 220 return $this->_table; 221 } 222 223 /** 224 * Sets a new table 225 * 226 * @param string $table 227 * @return Zend_Validate_Db_Abstract 228 */ 229 public function setTable($table) 230 { 231 $this->_table = (string) $table; 232 return $this; 233 } 234 235 /** 236 * Returns the set schema 237 * 238 * @return string 239 */ 240 public function getSchema() 241 { 242 return $this->_schema; 243 } 244 245 /** 246 * Sets a new schema 247 * 248 * @param string $schema 249 * @return Zend_Validate_Db_Abstract 250 */ 251 public function setSchema($schema) 252 { 253 $this->_schema = $schema; 254 return $this; 255 } 256 257 /** 258 * Run query and returns matches, or null if no matches are found. 259 * 260 * @param String $value 261 * @return Array when matches are found. 262 */ 263 protected function _query($value) 264 { 265 /** 266 * Check for an adapter being defined. if not, fetch the default adapter. 267 */ 268 if ($this->_adapter === null) { 269 $this->_adapter = Zend_Db_Table_Abstract::getDefaultAdapter(); 270 if (null === $this->_adapter) { 271 require_once 'Zend/Validate/Exception.php'; 272 throw new Zend_Validate_Exception('No database adapter present'); 273 } 274 } 275 276 /** 277 * Build select object 278 */ 279 $select = new Zend_Db_Select($this->_adapter); 280 $select->from($this->_table, array($this->_field), $this->_schema) 281 ->where($this->_adapter->quoteIdentifier($this->_field, true).' = ?', $value); 282 if ($this->_exclude !== null) { 283 if (is_array($this->_exclude)) { 284 $select->where($this->_adapter->quoteIdentifier($this->_exclude['field'], true).' != ?', $this->_exclude['value']); 285 } else { 286 $select->where($this->_exclude); 287 } 288 } 289 $select->limit(1); 290 291 /** 292 * Run query 293 */ 294 $result = $this->_adapter->fetchRow($select, array(), Zend_Db::FETCH_ASSOC); 295 296 return $result; 297 } 298 }
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 |