[ 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 * @category Zend 29 * @package Zend_Validate 30 * @copyright Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com) 31 * @license http://framework.zend.com/license/new-bsd New BSD License 32 */ 33 class Zend_Validate_NotEmpty extends Zend_Validate_Abstract 34 { 35 const BOOLEAN = 1; 36 const INTEGER = 2; 37 const FLOAT = 4; 38 const STRING = 8; 39 const ZERO = 16; 40 const EMPTY_ARRAY = 32; 41 const NULL = 64; 42 const PHP = 127; 43 const SPACE = 128; 44 const ALL = 255; 45 46 const INVALID = 'notEmptyInvalid'; 47 const IS_EMPTY = 'isEmpty'; 48 49 protected $_constants = array( 50 self::BOOLEAN => 'boolean', 51 self::INTEGER => 'integer', 52 self::FLOAT => 'float', 53 self::STRING => 'string', 54 self::ZERO => 'zero', 55 self::EMPTY_ARRAY => 'array', 56 self::NULL => 'null', 57 self::PHP => 'php', 58 self::SPACE => 'space', 59 self::ALL => 'all' 60 ); 61 62 /** 63 * @var array 64 */ 65 protected $_messageTemplates = array( 66 self::IS_EMPTY => "Value is required and can't be empty", 67 self::INVALID => "Invalid type given, value should be float, string, array, boolean or integer", 68 ); 69 70 /** 71 * Internal type to detect 72 * 73 * @var integer 74 */ 75 protected $_type = 237; 76 77 /** 78 * Constructor 79 * 80 * @param string|array|Zend_Config $options OPTIONAL 81 */ 82 public function __construct($options = null) 83 { 84 if ($options instanceof Zend_Config) { 85 $options = $options->toArray(); 86 } else if (!is_array($options)) { 87 $options = func_get_args(); 88 $temp = array(); 89 if (!empty($options)) { 90 $temp['type'] = array_shift($options); 91 } 92 93 $options = $temp; 94 } 95 96 if (is_array($options) && array_key_exists('type', $options)) { 97 $this->setType($options['type']); 98 } 99 } 100 101 /** 102 * Returns the set types 103 * 104 * @return array 105 */ 106 public function getType() 107 { 108 return $this->_type; 109 } 110 111 /** 112 * Set the types 113 * 114 * @param integer|array $type 115 * @throws Zend_Validate_Exception 116 * @return Zend_Validate_NotEmpty 117 */ 118 public function setType($type = null) 119 { 120 if (is_array($type)) { 121 $detected = 0; 122 foreach($type as $value) { 123 if (is_int($value)) { 124 $detected += $value; 125 } else if (in_array($value, $this->_constants)) { 126 $detected += array_search($value, $this->_constants); 127 } 128 } 129 130 $type = $detected; 131 } else if (is_string($type) && in_array($type, $this->_constants)) { 132 $type = array_search($type, $this->_constants); 133 } 134 135 if (!is_int($type) || ($type < 0) || ($type > self::ALL)) { 136 require_once 'Zend/Validate/Exception.php'; 137 throw new Zend_Validate_Exception('Unknown type'); 138 } 139 140 $this->_type = $type; 141 return $this; 142 } 143 144 /** 145 * Defined by Zend_Validate_Interface 146 * 147 * Returns true if and only if $value is not an empty value. 148 * 149 * @param string $value 150 * @return boolean 151 */ 152 public function isValid($value) 153 { 154 if (!is_null($value) && !is_string($value) && !is_int($value) && !is_float($value) && 155 !is_bool($value) && !is_array($value)) { 156 $this->_error(self::INVALID); 157 return false; 158 } 159 160 $type = $this->getType(); 161 $this->_setValue($value); 162 163 // SPACE (' ') 164 if ($type >= self::SPACE) { 165 $type -= self::SPACE; 166 if (is_string($value) && (preg_match('/^\s+$/s', $value))) { 167 $this->_error(self::IS_EMPTY); 168 return false; 169 } 170 } 171 172 // NULL (null) 173 if ($type >= self::NULL) { 174 $type -= self::NULL; 175 if (is_null($value)) { 176 $this->_error(self::IS_EMPTY); 177 return false; 178 } 179 } 180 181 // EMPTY_ARRAY (array()) 182 if ($type >= self::EMPTY_ARRAY) { 183 $type -= self::EMPTY_ARRAY; 184 if (is_array($value) && ($value == array())) { 185 $this->_error(self::IS_EMPTY); 186 return false; 187 } 188 } 189 190 // ZERO ('0') 191 if ($type >= self::ZERO) { 192 $type -= self::ZERO; 193 if (is_string($value) && ($value == '0')) { 194 $this->_error(self::IS_EMPTY); 195 return false; 196 } 197 } 198 199 // STRING ('') 200 if ($type >= self::STRING) { 201 $type -= self::STRING; 202 if (is_string($value) && ($value == '')) { 203 $this->_error(self::IS_EMPTY); 204 return false; 205 } 206 } 207 208 // FLOAT (0.0) 209 if ($type >= self::FLOAT) { 210 $type -= self::FLOAT; 211 if (is_float($value) && ($value == 0.0)) { 212 $this->_error(self::IS_EMPTY); 213 return false; 214 } 215 } 216 217 // INTEGER (0) 218 if ($type >= self::INTEGER) { 219 $type -= self::INTEGER; 220 if (is_int($value) && ($value == 0)) { 221 $this->_error(self::IS_EMPTY); 222 return false; 223 } 224 } 225 226 // BOOLEAN (false) 227 if ($type >= self::BOOLEAN) { 228 $type -= self::BOOLEAN; 229 if (is_bool($value) && ($value == false)) { 230 $this->_error(self::IS_EMPTY); 231 return false; 232 } 233 } 234 235 return true; 236 } 237 }
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 |