|
Moodle
2.2.1
http://www.collinsharper.com
|
00001 <?php 00025 require_once 'Zend/Validate/Abstract.php'; 00026 00033 class Zend_Validate_NotEmpty extends Zend_Validate_Abstract 00034 { 00035 const BOOLEAN = 1; 00036 const INTEGER = 2; 00037 const FLOAT = 4; 00038 const STRING = 8; 00039 const ZERO = 16; 00040 const EMPTY_ARRAY = 32; 00041 const NULL = 64; 00042 const PHP = 127; 00043 const SPACE = 128; 00044 const ALL = 255; 00045 00046 const INVALID = 'notEmptyInvalid'; 00047 const IS_EMPTY = 'isEmpty'; 00048 00049 protected $_constants = array( 00050 self::BOOLEAN => 'boolean', 00051 self::INTEGER => 'integer', 00052 self::FLOAT => 'float', 00053 self::STRING => 'string', 00054 self::ZERO => 'zero', 00055 self::EMPTY_ARRAY => 'array', 00056 self::NULL => 'null', 00057 self::PHP => 'php', 00058 self::SPACE => 'space', 00059 self::ALL => 'all' 00060 ); 00061 00065 protected $_messageTemplates = array( 00066 self::IS_EMPTY => "Value is required and can't be empty", 00067 self::INVALID => "Invalid type given, value should be float, string, array, boolean or integer", 00068 ); 00069 00075 protected $_type = 237; 00076 00082 public function __construct($options = null) 00083 { 00084 if ($options instanceof Zend_Config) { 00085 $options = $options->toArray(); 00086 } else if (!is_array($options)) { 00087 $options = func_get_args(); 00088 $temp = array(); 00089 if (!empty($options)) { 00090 $temp['type'] = array_shift($options); 00091 } 00092 00093 $options = $temp; 00094 } 00095 00096 if (is_array($options) && array_key_exists('type', $options)) { 00097 $this->setType($options['type']); 00098 } 00099 } 00100 00106 public function getType() 00107 { 00108 return $this->_type; 00109 } 00110 00118 public function setType($type = null) 00119 { 00120 if (is_array($type)) { 00121 $detected = 0; 00122 foreach($type as $value) { 00123 if (is_int($value)) { 00124 $detected += $value; 00125 } else if (in_array($value, $this->_constants)) { 00126 $detected += array_search($value, $this->_constants); 00127 } 00128 } 00129 00130 $type = $detected; 00131 } else if (is_string($type) && in_array($type, $this->_constants)) { 00132 $type = array_search($type, $this->_constants); 00133 } 00134 00135 if (!is_int($type) || ($type < 0) || ($type > self::ALL)) { 00136 require_once 'Zend/Validate/Exception.php'; 00137 throw new Zend_Validate_Exception('Unknown type'); 00138 } 00139 00140 $this->_type = $type; 00141 return $this; 00142 } 00143 00152 public function isValid($value) 00153 { 00154 if (!is_null($value) && !is_string($value) && !is_int($value) && !is_float($value) && 00155 !is_bool($value) && !is_array($value)) { 00156 $this->_error(self::INVALID); 00157 return false; 00158 } 00159 00160 $type = $this->getType(); 00161 $this->_setValue($value); 00162 00163 // SPACE (' ') 00164 if ($type >= self::SPACE) { 00165 $type -= self::SPACE; 00166 if (is_string($value) && (preg_match('/^\s+$/s', $value))) { 00167 $this->_error(self::IS_EMPTY); 00168 return false; 00169 } 00170 } 00171 00172 // NULL (null) 00173 if ($type >= self::NULL) { 00174 $type -= self::NULL; 00175 if (is_null($value)) { 00176 $this->_error(self::IS_EMPTY); 00177 return false; 00178 } 00179 } 00180 00181 // EMPTY_ARRAY (array()) 00182 if ($type >= self::EMPTY_ARRAY) { 00183 $type -= self::EMPTY_ARRAY; 00184 if (is_array($value) && ($value == array())) { 00185 $this->_error(self::IS_EMPTY); 00186 return false; 00187 } 00188 } 00189 00190 // ZERO ('0') 00191 if ($type >= self::ZERO) { 00192 $type -= self::ZERO; 00193 if (is_string($value) && ($value == '0')) { 00194 $this->_error(self::IS_EMPTY); 00195 return false; 00196 } 00197 } 00198 00199 // STRING ('') 00200 if ($type >= self::STRING) { 00201 $type -= self::STRING; 00202 if (is_string($value) && ($value == '')) { 00203 $this->_error(self::IS_EMPTY); 00204 return false; 00205 } 00206 } 00207 00208 // FLOAT (0.0) 00209 if ($type >= self::FLOAT) { 00210 $type -= self::FLOAT; 00211 if (is_float($value) && ($value == 0.0)) { 00212 $this->_error(self::IS_EMPTY); 00213 return false; 00214 } 00215 } 00216 00217 // INTEGER (0) 00218 if ($type >= self::INTEGER) { 00219 $type -= self::INTEGER; 00220 if (is_int($value) && ($value == 0)) { 00221 $this->_error(self::IS_EMPTY); 00222 return false; 00223 } 00224 } 00225 00226 // BOOLEAN (false) 00227 if ($type >= self::BOOLEAN) { 00228 $type -= self::BOOLEAN; 00229 if (is_bool($value) && ($value == false)) { 00230 $this->_error(self::IS_EMPTY); 00231 return false; 00232 } 00233 } 00234 00235 return true; 00236 } 00237 }