| 1 |
<?php |
|---|
| 2 |
|
|---|
| 3 |
|
|---|
| 4 |
|
|---|
| 5 |
|
|---|
| 6 |
|
|---|
| 7 |
|
|---|
| 8 |
|
|---|
| 9 |
|
|---|
| 10 |
|
|---|
| 11 |
|
|---|
| 12 |
|
|---|
| 13 |
|
|---|
| 14 |
|
|---|
| 15 |
|
|---|
| 16 |
|
|---|
| 17 |
|
|---|
| 18 |
|
|---|
| 19 |
|
|---|
| 20 |
|
|---|
| 21 |
|
|---|
| 22 |
class sfValidatorManager |
|---|
| 23 |
{ |
|---|
| 24 |
protected |
|---|
| 25 |
$groups = array(), |
|---|
| 26 |
$names = array(), |
|---|
| 27 |
$request = null; |
|---|
| 28 |
|
|---|
| 29 |
|
|---|
| 30 |
* Class constructor. |
|---|
| 31 |
* |
|---|
| 32 |
* @see initialize() |
|---|
| 33 |
*/ |
|---|
| 34 |
public function __construct($context) |
|---|
| 35 |
{ |
|---|
| 36 |
$this->initialize($context); |
|---|
| 37 |
} |
|---|
| 38 |
|
|---|
| 39 |
|
|---|
| 40 |
* Initializes this validator manager. |
|---|
| 41 |
* |
|---|
| 42 |
* @param sfContext A sfContext instance |
|---|
| 43 |
*/ |
|---|
| 44 |
public function initialize($context) |
|---|
| 45 |
{ |
|---|
| 46 |
$this->request = $context->getRequest(); |
|---|
| 47 |
} |
|---|
| 48 |
|
|---|
| 49 |
|
|---|
| 50 |
* Clears this validator manager so it can be reused. |
|---|
| 51 |
*/ |
|---|
| 52 |
public function clear() |
|---|
| 53 |
{ |
|---|
| 54 |
$this->groups = null; |
|---|
| 55 |
$this->groups = array(); |
|---|
| 56 |
$this->names = null; |
|---|
| 57 |
$this->names = array(); |
|---|
| 58 |
} |
|---|
| 59 |
|
|---|
| 60 |
|
|---|
| 61 |
* Executes all validators and determine the validation status. |
|---|
| 62 |
* |
|---|
| 63 |
* @return bool true, if validation completed successfully, otherwise false |
|---|
| 64 |
*/ |
|---|
| 65 |
public function execute() |
|---|
| 66 |
{ |
|---|
| 67 |
if (sfConfig::get('sf_logging_enabled')) |
|---|
| 68 |
{ |
|---|
| 69 |
sfContext::getInstance()->getEventDispatcher()->notify(new sfEvent($this, 'application.log', array('Validation execution'))); |
|---|
| 70 |
} |
|---|
| 71 |
|
|---|
| 72 |
$retval = true; |
|---|
| 73 |
|
|---|
| 74 |
|
|---|
| 75 |
// if 1 or more groups exist, we'll have to do a second pass |
|---|
| 76 |
$pass = 1; |
|---|
| 77 |
|
|---|
| 78 |
while (true) |
|---|
| 79 |
{ |
|---|
| 80 |
foreach ($this->names as $name => &$data) |
|---|
| 81 |
{ |
|---|
| 82 |
if (isset($data['_is_parent'])) |
|---|
| 83 |
{ |
|---|
| 84 |
|
|---|
| 85 |
foreach ($data as $subname => &$subdata) |
|---|
| 86 |
{ |
|---|
| 87 |
if ($subname == '_is_parent') |
|---|
| 88 |
{ |
|---|
| 89 |
|
|---|
| 90 |
continue; |
|---|
| 91 |
} |
|---|
| 92 |
|
|---|
| 93 |
if ($subdata['validation_status'] == true && !$this->validate($subname, $subdata, $name)) |
|---|
| 94 |
{ |
|---|
| 95 |
|
|---|
| 96 |
$retval = false; |
|---|
| 97 |
} |
|---|
| 98 |
} |
|---|
| 99 |
} |
|---|
| 100 |
else |
|---|
| 101 |
{ |
|---|
| 102 |
|
|---|
| 103 |
if ($data['validation_status'] == true && !$this->validate($name, $data, null)) |
|---|
| 104 |
{ |
|---|
| 105 |
|
|---|
| 106 |
$retval = false; |
|---|
| 107 |
} |
|---|
| 108 |
} |
|---|
| 109 |
} |
|---|
| 110 |
|
|---|
| 111 |
if (count($this->groups) == 0 || $pass == 2) |
|---|
| 112 |
{ |
|---|
| 113 |
break; |
|---|
| 114 |
} |
|---|
| 115 |
|
|---|
| 116 |
|
|---|
| 117 |
++$pass; |
|---|
| 118 |
} |
|---|
| 119 |
|
|---|
| 120 |
return $retval; |
|---|
| 121 |
} |
|---|
| 122 |
|
|---|
| 123 |
|
|---|
| 124 |
* Registers a file or parameter. |
|---|
| 125 |
* |
|---|
| 126 |
* @param string A file or parameter name |
|---|
| 127 |
* @param bool The required status |
|---|
| 128 |
* @param string A required error message |
|---|
| 129 |
* @param string A group name |
|---|
| 130 |
* @param string A parent array |
|---|
| 131 |
*/ |
|---|
| 132 |
public function registerName($name, $required = true, $message = 'Required', $parent = null, $group = null, $isFile = false) |
|---|
| 133 |
{ |
|---|
| 134 |
|
|---|
| 135 |
$entry = array(); |
|---|
| 136 |
$entry['group'] = null; |
|---|
| 137 |
$entry['is_file'] = $isFile; |
|---|
| 138 |
$entry['required'] = $required; |
|---|
| 139 |
$entry['required_msg'] = $message; |
|---|
| 140 |
$entry['validation_status'] = true; |
|---|
| 141 |
$entry['validators'] = array(); |
|---|
| 142 |
|
|---|
| 143 |
if ($parent != null) |
|---|
| 144 |
{ |
|---|
| 145 |
|
|---|
| 146 |
if (!isset($this->names[$parent])) |
|---|
| 147 |
{ |
|---|
| 148 |
|
|---|
| 149 |
$this->names[$parent] = array('_is_parent' => true); |
|---|
| 150 |
} |
|---|
| 151 |
|
|---|
| 152 |
|
|---|
| 153 |
$this->names[$parent][$name] =& $entry; |
|---|
| 154 |
} |
|---|
| 155 |
else |
|---|
| 156 |
{ |
|---|
| 157 |
|
|---|
| 158 |
|
|---|
| 159 |
// register this parameter |
|---|
| 160 |
$this->names[$name] =& $entry; |
|---|
| 161 |
} |
|---|
| 162 |
|
|---|
| 163 |
if ($group != null) |
|---|
| 164 |
{ |
|---|
| 165 |
|
|---|
| 166 |
if (!isset($this->groups[$group])) |
|---|
| 167 |
{ |
|---|
| 168 |
|
|---|
| 169 |
$this->groups[$group] = array('_force' => false); |
|---|
| 170 |
} |
|---|
| 171 |
|
|---|
| 172 |
|
|---|
| 173 |
$this->groups[$group][] = $name; |
|---|
| 174 |
|
|---|
| 175 |
|
|---|
| 176 |
$entry['group'] =& $this->groups[$group]; |
|---|
| 177 |
} |
|---|
| 178 |
} |
|---|
| 179 |
|
|---|
| 180 |
|
|---|
| 181 |
* Registers a validator for a file or parameter. |
|---|
| 182 |
* |
|---|
| 183 |
* @param string A file or parameter name |
|---|
| 184 |
* @param Validator A validator implementation instance |
|---|
| 185 |
* @param string A parent array name |
|---|
| 186 |
*/ |
|---|
| 187 |
public function registerValidator($name, $validator, $parent = null) |
|---|
| 188 |
{ |
|---|
| 189 |
if ($parent != null) |
|---|
| 190 |
{ |
|---|
| 191 |
|
|---|
| 192 |
$this->names[$parent][$name]['validators'][] = $validator; |
|---|
| 193 |
} |
|---|
| 194 |
else |
|---|
| 195 |
{ |
|---|
| 196 |
|
|---|
| 197 |
$this->names[$name]['validators'][] = $validator; |
|---|
| 198 |
} |
|---|
| 199 |
} |
|---|
| 200 |
|
|---|
| 201 |
|
|---|
| 202 |
* Validates a file or parameter. |
|---|
| 203 |
* |
|---|
| 204 |
* @param string A file or parameter name |
|---|
| 205 |
* @param array Data associated with the file or parameter |
|---|
| 206 |
* @param string A parent name |
|---|
| 207 |
* |
|---|
| 208 |
* @return bool true, if validation completes successfully, otherwise false |
|---|
| 209 |
*/ |
|---|
| 210 |
protected function validate(&$name, &$data, $parent) |
|---|
| 211 |
{ |
|---|
| 212 |
|
|---|
| 213 |
$error = null; |
|---|
| 214 |
$errorName = null; |
|---|
| 215 |
$force = null !== $data['group'] ? $data['group']['_force'] : false; |
|---|
| 216 |
$retval = true; |
|---|
| 217 |
$value = null; |
|---|
| 218 |
|
|---|
| 219 |
|
|---|
| 220 |
if ($parent == null) |
|---|
| 221 |
{ |
|---|
| 222 |
|
|---|
| 223 |
$errorName = $name; |
|---|
| 224 |
|
|---|
| 225 |
if ($data['is_file']) |
|---|
| 226 |
{ |
|---|
| 227 |
|
|---|
| 228 |
$value = $this->request->getFile($name); |
|---|
| 229 |
} |
|---|
| 230 |
else |
|---|
| 231 |
{ |
|---|
| 232 |
|
|---|
| 233 |
$value = $this->request->getParameterHolder()->get($name); |
|---|
| 234 |
} |
|---|
| 235 |
} |
|---|
| 236 |
else |
|---|
| 237 |
{ |
|---|
| 238 |
|
|---|
| 239 |
$errorName = $parent.'{'.$name.'}'; |
|---|
| 240 |
|
|---|
| 241 |
if ($data['is_file']) |
|---|
| 242 |
{ |
|---|
| 243 |
|
|---|
| 244 |
$parent = $this->request->getFile($parent.'['.$name.']'); |
|---|
| 245 |
|
|---|
| 246 |
if ($parent != null) |
|---|
| 247 |
{ |
|---|
| 248 |
$value = $parent; |
|---|
| 249 |
} |
|---|
| 250 |
} |
|---|
| 251 |
else |
|---|
| 252 |
{ |
|---|
| 253 |
|
|---|
| 254 |
$parent = $this->request->getParameterHolder()->get($parent); |
|---|
| 255 |
|
|---|
| 256 |
if ($parent != null && isset($parent[$name])) |
|---|
| 257 |
{ |
|---|
| 258 |
$value = $parent[$name]; |
|---|
| 259 |
} |
|---|
| 260 |
} |
|---|
| 261 |
} |
|---|
| 262 |
|
|---|
| 263 |
|
|---|
| 264 |
if ( |
|---|
| 265 |
($data['is_file'] && !$value['name']) |
|---|
| 266 |
|| |
|---|
| 267 |
(!$data['is_file'] && (is_array($value) ? sfToolkit::isArrayValuesEmpty($value) : ($value === null || strlen($value) == 0))) |
|---|
| 268 |
) |
|---|
| 269 |
{ |
|---|
| 270 |
if ($data['required'] || $force) |
|---|
| 271 |
{ |
|---|
| 272 |
|
|---|
| 273 |
$error = $data['required_msg']; |
|---|
| 274 |
$retval = false; |
|---|
| 275 |
} |
|---|
| 276 |
else |
|---|
| 277 |
{ |
|---|
| 278 |
|
|---|
| 279 |
$retval = true; |
|---|
| 280 |
} |
|---|
| 281 |
} |
|---|
| 282 |
else |
|---|
| 283 |
{ |
|---|
| 284 |
|
|---|
| 285 |
$error = null; |
|---|
| 286 |
|
|---|
| 287 |
|
|---|
| 288 |
if ($data['group'] != null) |
|---|
| 289 |
{ |
|---|
| 290 |
|
|---|
| 291 |
$data['group']['_force'] = true; |
|---|
| 292 |
} |
|---|
| 293 |
|
|---|
| 294 |
if (count($data['validators']) > 0) |
|---|
| 295 |
{ |
|---|
| 296 |
|
|---|
| 297 |
foreach ($data['validators'] as $validator) |
|---|
| 298 |
{ |
|---|
| 299 |
if (!$validator->execute($value, $error)) |
|---|
| 300 |
{ |
|---|
| 301 |
$retval = false; |
|---|
| 302 |
|
|---|
| 303 |
break; |
|---|
| 304 |
} |
|---|
| 305 |
} |
|---|
| 306 |
} |
|---|
| 307 |
} |
|---|
| 308 |
|
|---|
| 309 |
if (!$retval) |
|---|
| 310 |
{ |
|---|
| 311 |
|
|---|
| 312 |
$data['validation_status'] = false; |
|---|
| 313 |
|
|---|
| 314 |
|
|---|
| 315 |
$this->request->setError($errorName, $error); |
|---|
| 316 |
} |
|---|
| 317 |
|
|---|
| 318 |
return $retval; |
|---|
| 319 |
} |
|---|
| 320 |
} |
|---|
| 321 |
|
|---|