| 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 |
class sfNumberValidator extends sfValidator |
|---|
| 37 |
{ |
|---|
| 38 |
|
|---|
| 39 |
* Executes this validator. |
|---|
| 40 |
* |
|---|
| 41 |
* @param mixed A file or parameter value/array |
|---|
| 42 |
* @param error An error message reference |
|---|
| 43 |
* |
|---|
| 44 |
* @return bool true, if this validator executes successfully, otherwise false |
|---|
| 45 |
*/ |
|---|
| 46 |
public function execute(&$value, &$error) |
|---|
| 47 |
{ |
|---|
| 48 |
if (!preg_match('/^-?\d+(\.\d+)?$/', $value)) |
|---|
| 49 |
{ |
|---|
| 50 |
|
|---|
| 51 |
$error = $this->getParameterHolder()->get('nan_error'); |
|---|
| 52 |
|
|---|
| 53 |
return false; |
|---|
| 54 |
} |
|---|
| 55 |
|
|---|
| 56 |
$type = strtolower($this->getParameterHolder()->get('type')); |
|---|
| 57 |
|
|---|
| 58 |
switch ($type) |
|---|
| 59 |
{ |
|---|
| 60 |
case "decimal": |
|---|
| 61 |
case "float": |
|---|
| 62 |
{ |
|---|
| 63 |
if (substr_count($value, '.') != 1) |
|---|
| 64 |
{ |
|---|
| 65 |
|
|---|
| 66 |
$error = $this->getParameterHolder()->get('type_error'); |
|---|
| 67 |
return false; |
|---|
| 68 |
} |
|---|
| 69 |
|
|---|
| 70 |
|
|---|
| 71 |
$value = (float) $value; |
|---|
| 72 |
|
|---|
| 73 |
break; |
|---|
| 74 |
} |
|---|
| 75 |
|
|---|
| 76 |
case "int": |
|---|
| 77 |
case "integer": |
|---|
| 78 |
{ |
|---|
| 79 |
|
|---|
| 80 |
if ((float) $value != (int) $value) |
|---|
| 81 |
{ |
|---|
| 82 |
|
|---|
| 83 |
$error = $this->getParameterHolder()->get('type_error'); |
|---|
| 84 |
return false; |
|---|
| 85 |
} |
|---|
| 86 |
|
|---|
| 87 |
|
|---|
| 88 |
$value = (int) $value; |
|---|
| 89 |
|
|---|
| 90 |
break; |
|---|
| 91 |
} |
|---|
| 92 |
|
|---|
| 93 |
} |
|---|
| 94 |
|
|---|
| 95 |
$min = $this->getParameterHolder()->get('min'); |
|---|
| 96 |
|
|---|
| 97 |
if ($min !== null && $value < $min) |
|---|
| 98 |
{ |
|---|
| 99 |
|
|---|
| 100 |
$error = $this->getParameterHolder()->get('min_error'); |
|---|
| 101 |
|
|---|
| 102 |
return false; |
|---|
| 103 |
} |
|---|
| 104 |
|
|---|
| 105 |
$max = $this->getParameterHolder()->get('max'); |
|---|
| 106 |
|
|---|
| 107 |
if ($max !== null && $value > $max) |
|---|
| 108 |
{ |
|---|
| 109 |
|
|---|
| 110 |
$error = $this->getParameterHolder()->get('max_error'); |
|---|
| 111 |
|
|---|
| 112 |
return false; |
|---|
| 113 |
} |
|---|
| 114 |
|
|---|
| 115 |
return true; |
|---|
| 116 |
} |
|---|
| 117 |
|
|---|
| 118 |
|
|---|
| 119 |
* Initializes this validator. |
|---|
| 120 |
* |
|---|
| 121 |
* @param sfContext The current application context |
|---|
| 122 |
* @param array An associative array of initialization parameters |
|---|
| 123 |
* |
|---|
| 124 |
* @return bool true, if initialization completes successfully, otherwise false |
|---|
| 125 |
*/ |
|---|
| 126 |
public function initialize($context, $parameters = null) |
|---|
| 127 |
{ |
|---|
| 128 |
|
|---|
| 129 |
parent::initialize($context); |
|---|
| 130 |
|
|---|
| 131 |
|
|---|
| 132 |
$this->getParameterHolder()->set('max', null); |
|---|
| 133 |
$this->getParameterHolder()->set('max_error', 'Input is too large'); |
|---|
| 134 |
$this->getParameterHolder()->set('min', null); |
|---|
| 135 |
$this->getParameterHolder()->set('min_error', 'Input is too small'); |
|---|
| 136 |
$this->getParameterHolder()->set('nan_error', 'Input is not a number'); |
|---|
| 137 |
$this->getParameterHolder()->set('type', 'any'); |
|---|
| 138 |
$this->getParameterHolder()->set('type_error', 'Input is not a number'); |
|---|
| 139 |
|
|---|
| 140 |
$this->getParameterHolder()->add($parameters); |
|---|
| 141 |
|
|---|
| 142 |
|
|---|
| 143 |
$type = strtolower($this->getParameterHolder()->get('type')); |
|---|
| 144 |
|
|---|
| 145 |
|
|---|
| 146 |
$allowed_types = array('any', 'decimal', 'float', 'int', 'integer'); |
|---|
| 147 |
|
|---|
| 148 |
if (!in_array(strtolower($type), $allowed_types)) |
|---|
| 149 |
{ |
|---|
| 150 |
|
|---|
| 151 |
throw new sfValidatorException(sprintf('Unknown number type "%s" in NumberValidator.', $this->getParameterHolder()->get('type'))); |
|---|
| 152 |
} |
|---|
| 153 |
|
|---|
| 154 |
return true; |
|---|
| 155 |
} |
|---|
| 156 |
} |
|---|
| 157 |
|
|---|