| 1 |
<?php |
|---|
| 2 |
|
|---|
| 3 |
|
|---|
| 4 |
|
|---|
| 5 |
@copyright |
|---|
| 6 |
@author will@flourishlib.com |
|---|
| 7 |
@license http://flourishlib.com/license |
|---|
| 8 |
|
|---|
| 9 |
@package |
|---|
| 10 |
@link http://flourishlib.com/fValidation |
|---|
| 11 |
|
|---|
| 12 |
@version |
|---|
| 13 |
@changes |
|---|
| 14 |
@changes |
|---|
| 15 |
@changes |
|---|
| 16 |
@changes |
|---|
| 17 |
|
|---|
| 18 |
class fValidation |
|---|
| 19 |
{ |
|---|
| 20 |
|
|---|
| 21 |
|
|---|
| 22 |
|
|---|
| 23 |
@param |
|---|
| 24 |
@param |
|---|
| 25 |
@param |
|---|
| 26 |
@return |
|---|
| 27 |
|
|---|
| 28 |
static protected function compose($message) |
|---|
| 29 |
{ |
|---|
| 30 |
$args = array_slice(func_get_args(), 1); |
|---|
| 31 |
|
|---|
| 32 |
if (class_exists('fText', FALSE)) { |
|---|
| 33 |
return call_user_func_array( |
|---|
| 34 |
array('fText', 'compose'), |
|---|
| 35 |
array($message, $args) |
|---|
| 36 |
); |
|---|
| 37 |
} else { |
|---|
| 38 |
return vsprintf($message, $args); |
|---|
| 39 |
} |
|---|
| 40 |
} |
|---|
| 41 |
|
|---|
| 42 |
|
|---|
| 43 |
|
|---|
| 44 |
|
|---|
| 45 |
|
|---|
| 46 |
@param |
|---|
| 47 |
@return |
|---|
| 48 |
|
|---|
| 49 |
static protected function stringlike($value) |
|---|
| 50 |
{ |
|---|
| 51 |
if ((!is_string($value) && !is_object($value) && !is_numeric($value)) || !strlen(trim($value))) { |
|---|
| 52 |
return FALSE; |
|---|
| 53 |
} |
|---|
| 54 |
|
|---|
| 55 |
return TRUE; |
|---|
| 56 |
} |
|---|
| 57 |
|
|---|
| 58 |
|
|---|
| 59 |
|
|---|
| 60 |
|
|---|
| 61 |
|
|---|
| 62 |
@var |
|---|
| 63 |
|
|---|
| 64 |
private $date_fields = array(); |
|---|
| 65 |
|
|---|
| 66 |
|
|---|
| 67 |
|
|---|
| 68 |
|
|---|
| 69 |
@var |
|---|
| 70 |
|
|---|
| 71 |
private $email_fields = array(); |
|---|
| 72 |
|
|---|
| 73 |
|
|---|
| 74 |
|
|---|
| 75 |
|
|---|
| 76 |
@var |
|---|
| 77 |
|
|---|
| 78 |
private $email_header_fields = array(); |
|---|
| 79 |
|
|---|
| 80 |
|
|---|
| 81 |
|
|---|
| 82 |
|
|---|
| 83 |
@var |
|---|
| 84 |
|
|---|
| 85 |
private $required_fields = array(); |
|---|
| 86 |
|
|---|
| 87 |
|
|---|
| 88 |
|
|---|
| 89 |
|
|---|
| 90 |
@var |
|---|
| 91 |
|
|---|
| 92 |
private $url_fields = array(); |
|---|
| 93 |
|
|---|
| 94 |
|
|---|
| 95 |
|
|---|
| 96 |
|
|---|
| 97 |
|
|---|
| 98 |
@internal |
|---|
| 99 |
|
|---|
| 100 |
@param |
|---|
| 101 |
@return |
|---|
| 102 |
|
|---|
| 103 |
public function __get($method) |
|---|
| 104 |
{ |
|---|
| 105 |
return array($this, $method); |
|---|
| 106 |
} |
|---|
| 107 |
|
|---|
| 108 |
|
|---|
| 109 |
|
|---|
| 110 |
|
|---|
| 111 |
|
|---|
| 112 |
|
|---|
| 113 |
|
|---|
| 114 |
@param |
|---|
| 115 |
@param |
|---|
| 116 |
@return |
|---|
| 117 |
|
|---|
| 118 |
public function addDateFields() |
|---|
| 119 |
{ |
|---|
| 120 |
$args = func_get_args(); |
|---|
| 121 |
foreach ($args as $arg) { |
|---|
| 122 |
if (!self::stringlike($arg)) { |
|---|
| 123 |
throw new fProgrammerException( |
|---|
| 124 |
'The field specified, %s, does not appear to be a valid field name', |
|---|
| 125 |
$arg |
|---|
| 126 |
); |
|---|
| 127 |
} |
|---|
| 128 |
} |
|---|
| 129 |
$this->date_fields = array_merge($this->date_fields, $args); |
|---|
| 130 |
} |
|---|
| 131 |
|
|---|
| 132 |
|
|---|
| 133 |
|
|---|
| 134 |
|
|---|
| 135 |
|
|---|
| 136 |
|
|---|
| 137 |
|
|---|
| 138 |
@param |
|---|
| 139 |
@param |
|---|
| 140 |
@return |
|---|
| 141 |
|
|---|
| 142 |
public function addEmailFields() |
|---|
| 143 |
{ |
|---|
| 144 |
$args = func_get_args(); |
|---|
| 145 |
foreach ($args as $arg) { |
|---|
| 146 |
if (!self::stringlike($arg)) { |
|---|
| 147 |
throw new fProgrammerException( |
|---|
| 148 |
'The field specified, %s, does not appear to be a valid field name', |
|---|
| 149 |
$arg |
|---|
| 150 |
); |
|---|
| 151 |
} |
|---|
| 152 |
} |
|---|
| 153 |
$this->email_fields = array_merge($this->email_fields, $args); |
|---|
| 154 |
} |
|---|
| 155 |
|
|---|
| 156 |
|
|---|
| 157 |
|
|---|
| 158 |
|
|---|
| 159 |
|
|---|
| 160 |
|
|---|
| 161 |
|
|---|
| 162 |
|
|---|
| 163 |
@param |
|---|
| 164 |
@param |
|---|
| 165 |
@return |
|---|
| 166 |
|
|---|
| 167 |
public function addEmailHeaderFields() |
|---|
| 168 |
{ |
|---|
| 169 |
$args = func_get_args(); |
|---|
| 170 |
foreach ($args as $arg) { |
|---|
| 171 |
if (!self::stringlike($arg)) { |
|---|
| 172 |
throw new fProgrammerException( |
|---|
| 173 |
'The field specified, %s, does not appear to be a valid field name', |
|---|
| 174 |
$arg |
|---|
| 175 |
); |
|---|
| 176 |
} |
|---|
| 177 |
} |
|---|
| 178 |
$this->email_header_fields = array_merge($this->email_header_fields, $args); |
|---|
| 179 |
} |
|---|
| 180 |
|
|---|
| 181 |
|
|---|
| 182 |
|
|---|
| 183 |
|
|---|
| 184 |
|
|---|
| 185 |
|
|---|
| 186 |
|
|---|
| 187 |
|
|---|
| 188 |
|
|---|
| 189 |
|
|---|
| 190 |
|
|---|
| 191 |
|
|---|
| 192 |
|
|---|
| 193 |
|
|---|
| 194 |
|
|---|
| 195 |
|
|---|
| 196 |
|
|---|
| 197 |
|
|---|
| 198 |
|
|---|
| 199 |
|
|---|
| 200 |
@param |
|---|
| 201 |
@param |
|---|
| 202 |
@return |
|---|
| 203 |
|
|---|
| 204 |
public function addRequiredFields() |
|---|
| 205 |
{ |
|---|
| 206 |
$args = func_get_args(); |
|---|
| 207 |
$fixed_args = array(); |
|---|
| 208 |
|
|---|
| 209 |
foreach ($args as $arg) { |
|---|
| 210 |
|
|---|
| 211 |
if (self::stringlike($arg)) { |
|---|
| 212 |
$fixed_args[] = $arg; |
|---|
| 213 |
|
|---|
| 214 |
|
|---|
| 215 |
} elseif (is_array($arg) && sizeof($arg) > 1) { |
|---|
| 216 |
$fixed_args[] = $arg; |
|---|
| 217 |
|
|---|
| 218 |
|
|---|
| 219 |
} elseif (is_array($arg) && sizeof($arg) == 1 && self::stringlike(key($arg)) && is_array(reset($arg))) { |
|---|
| 220 |
$fixed_args[key($arg)] = reset($arg); |
|---|
| 221 |
|
|---|
| 222 |
} else { |
|---|
| 223 |
throw new fProgrammerException( |
|---|
| 224 |
'The field specified, %s, does not appear to be a valid required field definition', |
|---|
| 225 |
$arg |
|---|
| 226 |
); |
|---|
| 227 |
} |
|---|
| 228 |
} |
|---|
| 229 |
|
|---|
| 230 |
$this->required_fields = array_merge($this->required_fields, $fixed_args); |
|---|
| 231 |
} |
|---|
| 232 |
|
|---|
| 233 |
|
|---|
| 234 |
|
|---|
| 235 |
|
|---|
| 236 |
|
|---|
| 237 |
|
|---|
| 238 |
|
|---|
| 239 |
@param |
|---|
| 240 |
@param |
|---|
| 241 |
@return |
|---|
| 242 |
|
|---|
| 243 |
public function addURLFields() |
|---|
| 244 |
{ |
|---|
| 245 |
$args = func_get_args(); |
|---|
| 246 |
foreach ($args as $arg) { |
|---|
| 247 |
if (!self::stringlike($arg)) { |
|---|
| 248 |
throw new fProgrammerException( |
|---|
| 249 |
'The field specified, %s, does not appear to be a valid field name', |
|---|
| 250 |
$arg |
|---|
| 251 |
); |
|---|
| 252 |
} |
|---|
| 253 |
} |
|---|
| 254 |
$this->url_fields = array_merge($this->url_fields, $args); |
|---|
| 255 |
} |
|---|
| 256 |
|
|---|
| 257 |
|
|---|
| 258 |
|
|---|
| 259 |
|
|---|
| 260 |
|
|---|
| 261 |
@param |
|---|
| 262 |
@return |
|---|
| 263 |
|
|---|
| 264 |
private function checkDateFields(&$messages) |
|---|
| 265 |
{ |
|---|
| 266 |
foreach ($this->date_fields as $date_field) { |
|---|
| 267 |
$value = trim(fRequest::get($date_field)); |
|---|
| 268 |
if (self::stringlike($value)) { |
|---|
| 269 |
try { |
|---|
| 270 |
new fTimestamp($value); |
|---|
| 271 |
} catch (fValidationException $e) { |
|---|
| 272 |
$messages[] = self::compose( |
|---|
| 273 |
'%sPlease enter a date', |
|---|
| 274 |
fValidationException::formatField(fGrammar::humanize($date_field)) |
|---|
| 275 |
); |
|---|
| 276 |
} |
|---|
| 277 |
} |
|---|
| 278 |
} |
|---|
| 279 |
} |
|---|
| 280 |
|
|---|
| 281 |
|
|---|
| 282 |
|
|---|
| 283 |
|
|---|
| 284 |
|
|---|
| 285 |
@param |
|---|
| 286 |
@return |
|---|
| 287 |
|
|---|
| 288 |
private function checkEmailFields(&$messages) |
|---|
| 289 |
{ |
|---|
| 290 |
foreach ($this->email_fields as $email_field) { |
|---|
| 291 |
$value = trim(fRequest::get($email_field)); |
|---|
| 292 |
if (self::stringlike($value) && !preg_match(fEmail::EMAIL_REGEX, $value)) { |
|---|
| 293 |
$messages[] = self::compose( |
|---|
| 294 |
'%sPlease enter an email address in the form name@example.com', |
|---|
| 295 |
fValidationException::formatField(fGrammar::humanize($email_field)) |
|---|
| 296 |
); |
|---|
| 297 |
} |
|---|
| 298 |
} |
|---|
| 299 |
} |
|---|
| 300 |
|
|---|
| 301 |
|
|---|
| 302 |
|
|---|
| 303 |
|
|---|
| 304 |
|
|---|
| 305 |
@param |
|---|
| 306 |
@return |
|---|
| 307 |
|
|---|
| 308 |
private function checkEmailHeaderFields(&$messages) |
|---|
| 309 |
{ |
|---|
| 310 |
foreach ($this->email_header_fields as $email_header_field) { |
|---|
| 311 |
if (preg_match('#\r|\n#', fRequest::get($email_header_field))) { |
|---|
| 312 |
$messages[] = self::compose( |
|---|
| 313 |
'%sLine breaks are not allowed', |
|---|
| 314 |
fValidationException::formatField(fGrammar::humanize($email_header_field)) |
|---|
| 315 |
); |
|---|
| 316 |
} |
|---|
| 317 |
} |
|---|
| 318 |
} |
|---|
| 319 |
|
|---|
| 320 |
|
|---|
| 321 |
|
|---|
| 322 |
|
|---|
| 323 |
|
|---|
| 324 |
@param |
|---|
| 325 |
@return |
|---|
| 326 |
|
|---|
| 327 |
private function checkRequiredFields(&$messages) |
|---|
| 328 |
{ |
|---|
| 329 |
foreach ($this->required_fields as $key => $required_field) { |
|---|
| 330 |
|
|---|
| 331 |
if (is_numeric($key) && is_string($required_field)) { |
|---|
| 332 |
if (!self::hasValue($required_field)) { |
|---|
| 333 |
$messages[] = self::compose( |
|---|
| 334 |
'%sPlease enter a value', |
|---|
| 335 |
fValidationException::formatField(fGrammar::humanize($required_field)) |
|---|
| 336 |
); |
|---|
| 337 |
} |
|---|
| 338 |
|
|---|
| 339 |
|
|---|
| 340 |
} elseif (is_numeric($key) && is_array($required_field)) { |
|---|
| 341 |
$found = FALSE; |
|---|
| 342 |
foreach ($required_field as $individual_field) { |
|---|
| 343 |
if (self::hasValue($individual_field)) { |
|---|
| 344 |
$found = TRUE; |
|---|
| 345 |
} |
|---|
| 346 |
} |
|---|
| 347 |
|
|---|
| 348 |
if (!$found) { |
|---|
| 349 |
$required_field = array_map(array('fGrammar', 'humanize'), $required_field); |
|---|
| 350 |
$messages[] = self::compose( |
|---|
| 351 |
'%sPlease enter at least one', |
|---|
| 352 |
fValidationException::formatField(join(', ', $required_field)) |
|---|
| 353 |
); |
|---|
| 354 |
} |
|---|
| 355 |
|
|---|
| 356 |
|
|---|
| 357 |
} else { |
|---|
| 358 |
if (!self::hasValue($key)) { |
|---|
| 359 |
continue; |
|---|
| 360 |
} |
|---|
| 361 |
foreach ($required_field as $individual_field) { |
|---|
| 362 |
if (!self::hasValue($individual_field)) { |
|---|
| 363 |
$messages[] = self::compose( |
|---|
| 364 |
'%sPlease enter a value', |
|---|
| 365 |
fValidationException::formatField(fGrammar::humanize($individual_field)) |
|---|
| 366 |
); |
|---|
| 367 |
} |
|---|
| 368 |
} |
|---|
| 369 |
} |
|---|
| 370 |
} |
|---|
| 371 |
} |
|---|
| 372 |
|
|---|
| 373 |
|
|---|
| 374 |
|
|---|
| 375 |
|
|---|
| 376 |
|
|---|
| 377 |
@param |
|---|
| 378 |
@return |
|---|
| 379 |
|
|---|
| 380 |
private function checkURLFields(&$messages) |
|---|
| 381 |
{ |
|---|
| 382 |
foreach ($this->url_fields as $url_field) { |
|---|
| 383 |
$value = trim(fRequest::get($url_field)); |
|---|
| 384 |
if (self::stringlike($value) && !preg_match('#^https?://[^ ]+$#iD', $value)) { |
|---|
| 385 |
$messages[] = self::compose( |
|---|
| 386 |
'%sPlease enter a URL in the form http://www.example.com/page', |
|---|
| 387 |
fValidationException::formatField(fGrammar::humanize($url_field)) |
|---|
| 388 |
); |
|---|
| 389 |
} |
|---|
| 390 |
} |
|---|
| 391 |
} |
|---|
| 392 |
|
|---|
| 393 |
|
|---|
| 394 |
|
|---|
| 395 |
|
|---|
| 396 |
|
|---|
| 397 |
@param |
|---|
| 398 |
@return |
|---|
| 399 |
|
|---|
| 400 |
static private function hasValue($key) |
|---|
| 401 |
{ |
|---|
| 402 |
$value = fRequest::get($key); |
|---|
| 403 |
if (self::stringlike($value)) { |
|---|
| 404 |
return TRUE; |
|---|
| 405 |
} |
|---|
| 406 |
if (is_array($value)) { |
|---|
| 407 |
foreach ($value as $individual_value) { |
|---|
| 408 |
if (self::stringlike($individual_value)) { |
|---|
| 409 |
return TRUE; |
|---|
| 410 |
} |
|---|
| 411 |
} |
|---|
| 412 |
} |
|---|
| 413 |
return FALSE; |
|---|
| 414 |
} |
|---|
| 415 |
|
|---|
| 416 |
|
|---|
| 417 |
|
|---|
| 418 |
|
|---|
| 419 |
|
|---|
| 420 |
@throws |
|---|
| 421 |
|
|---|
| 422 |
@return |
|---|
| 423 |
|
|---|
| 424 |
public function validate() |
|---|
| 425 |
{ |
|---|
| 426 |
if (!$this->email_header_fields && |
|---|
| 427 |
!$this->required_fields && |
|---|
| 428 |
!$this->email_fields && |
|---|
| 429 |
!$this->date_fields && |
|---|
| 430 |
!$this->url_fields) { |
|---|
| 431 |
throw new fProgrammerException( |
|---|
| 432 |
'No fields have been set to be validated' |
|---|
| 433 |
); |
|---|
| 434 |
} |
|---|
| 435 |
|
|---|
| 436 |
$messages = array(); |
|---|
| 437 |
|
|---|
| 438 |
$this->checkRequiredFields($messages); |
|---|
| 439 |
$this->checkDateFields($messages); |
|---|
| 440 |
$this->checkEmailFields($messages); |
|---|
| 441 |
$this->checkEmailHeaderFields($messages); |
|---|
| 442 |
$this->checkURLFields($messages); |
|---|
| 443 |
|
|---|
| 444 |
if ($messages) { |
|---|
| 445 |
throw new fValidationException( |
|---|
| 446 |
sprintf( |
|---|
| 447 |
"<p>%1\$s</p>\n<ul>\n<li>%2\$s</li>\n</ul>", |
|---|
| 448 |
self::compose("The following problems were found:"), |
|---|
| 449 |
join("</li>\n<li>", $messages) |
|---|
| 450 |
) |
|---|
| 451 |
); |
|---|
| 452 |
} |
|---|
| 453 |
} |
|---|
| 454 |
} |
|---|
| 455 |
|
|---|
| 456 |
|
|---|
| 457 |
|
|---|
| 458 |
|
|---|
| 459 |
will@flourishlib.com |
|---|
| 460 |
|
|---|
| 461 |
|
|---|
| 462 |
|
|---|
| 463 |
|
|---|
| 464 |
|
|---|
| 465 |
|
|---|
| 466 |
|
|---|
| 467 |
|
|---|
| 468 |
|
|---|
| 469 |
|
|---|
| 470 |
|
|---|
| 471 |
|
|---|
| 472 |
|
|---|
| 473 |
|
|---|
| 474 |
|
|---|
| 475 |
|
|---|
| 476 |
|
|---|
| 477 |
|
|---|
| 478 |
|
|---|