| 1 |
<?php |
|---|
| 2 |
|
|---|
| 3 |
|
|---|
| 4 |
|
|---|
| 5 |
|
|---|
| 6 |
|
|---|
| 7 |
|
|---|
| 8 |
|
|---|
| 9 |
|
|---|
| 10 |
|
|---|
| 11 |
|
|---|
| 12 |
|
|---|
| 13 |
|
|---|
| 14 |
|
|---|
| 15 |
|
|---|
| 16 |
|
|---|
| 17 |
|
|---|
| 18 |
|
|---|
| 19 |
|
|---|
| 20 |
|
|---|
| 21 |
|
|---|
| 22 |
|
|---|
| 23 |
|
|---|
| 24 |
|
|---|
| 25 |
|
|---|
| 26 |
|
|---|
| 27 |
|
|---|
| 28 |
|
|---|
| 29 |
|
|---|
| 30 |
|
|---|
| 31 |
|
|---|
| 32 |
|
|---|
| 33 |
|
|---|
| 34 |
|
|---|
| 35 |
|
|---|
| 36 |
|
|---|
| 37 |
class sfCompareValidator extends sfValidator |
|---|
| 38 |
{ |
|---|
| 39 |
|
|---|
| 40 |
* Executes this validator. |
|---|
| 41 |
* |
|---|
| 42 |
* @param mixed A file or parameter value/array |
|---|
| 43 |
* @param error An error message reference |
|---|
| 44 |
* |
|---|
| 45 |
* @return bool true, if this validator executes successfully, otherwise false |
|---|
| 46 |
*/ |
|---|
| 47 |
public function execute(&$value, &$error) |
|---|
| 48 |
{ |
|---|
| 49 |
$check_param = $this->getParameter('check'); |
|---|
| 50 |
$check_operator = $this->getParameter('operator'); |
|---|
| 51 |
$check_value = $this->context->getRequest()->getParameter($check_param); |
|---|
| 52 |
|
|---|
| 53 |
switch ($check_operator) |
|---|
| 54 |
{ |
|---|
| 55 |
case '>': |
|---|
| 56 |
$valid = $value > $check_value; |
|---|
| 57 |
break; |
|---|
| 58 |
case '>=': |
|---|
| 59 |
$valid = $value >= $check_value; |
|---|
| 60 |
break; |
|---|
| 61 |
case '<': |
|---|
| 62 |
$valid = $value < $check_value; |
|---|
| 63 |
break; |
|---|
| 64 |
case '<=': |
|---|
| 65 |
$valid = $value <= $check_value; |
|---|
| 66 |
break; |
|---|
| 67 |
case '!=': |
|---|
| 68 |
$valid = $value != $check_value; |
|---|
| 69 |
break; |
|---|
| 70 |
case '==': |
|---|
| 71 |
default: |
|---|
| 72 |
$valid = $value == $check_value; |
|---|
| 73 |
break; |
|---|
| 74 |
} |
|---|
| 75 |
|
|---|
| 76 |
if (!$valid) |
|---|
| 77 |
{ |
|---|
| 78 |
$error = $this->getParameter('compare_error'); |
|---|
| 79 |
|
|---|
| 80 |
return false; |
|---|
| 81 |
} |
|---|
| 82 |
|
|---|
| 83 |
return true; |
|---|
| 84 |
} |
|---|
| 85 |
|
|---|
| 86 |
|
|---|
| 87 |
* Initializes this validator. |
|---|
| 88 |
* |
|---|
| 89 |
* @param sfContext The current application context |
|---|
| 90 |
* @param array An associative array of initialization parameters |
|---|
| 91 |
* |
|---|
| 92 |
* @return boolean true, if initialization completes successfully, otherwise false |
|---|
| 93 |
*/ |
|---|
| 94 |
public function initialize($context, $parameters = null) |
|---|
| 95 |
{ |
|---|
| 96 |
|
|---|
| 97 |
parent::initialize($context); |
|---|
| 98 |
|
|---|
| 99 |
|
|---|
| 100 |
$this->setParameter('operator', '=='); |
|---|
| 101 |
$this->setParameter('compare_error', 'Invalid input'); |
|---|
| 102 |
|
|---|
| 103 |
$this->getParameterHolder()->add($parameters); |
|---|
| 104 |
|
|---|
| 105 |
if (!$this->hasParameter('check')) |
|---|
| 106 |
{ |
|---|
| 107 |
throw new sfValidatorException('You must specify a "check" parameter for your sfCompareValidator.'); |
|---|
| 108 |
} |
|---|
| 109 |
|
|---|
| 110 |
if (!in_array($this->getParameter('operator'), array('==', '!=', '>', '<', '<=', '>='))) |
|---|
| 111 |
{ |
|---|
| 112 |
throw new sfValidatorException(sprintf('The operator "%s" is not available for your sfCompareValidator.', $this->getParameter('operator'))); |
|---|
| 113 |
} |
|---|
| 114 |
|
|---|
| 115 |
return true; |
|---|
| 116 |
} |
|---|
| 117 |
} |
|---|
| 118 |
|
|---|