| Total Complexity | 58 |
| Total Lines | 283 |
| Duplicated Lines | 0 % |
| Changes | 0 | ||
Complex classes like TRangeValidator often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use TRangeValidator, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 52 | class TRangeValidator extends TBaseValidator |
||
| 53 | { |
||
| 54 | /** |
||
| 55 | * Gets the name of the javascript class responsible for performing validation for this control. |
||
| 56 | * This method overrides the parent implementation. |
||
| 57 | * @return string the javascript class name |
||
| 58 | */ |
||
| 59 | protected function getClientClassName() |
||
| 60 | { |
||
| 61 | return 'Prado.WebUI.TRangeValidator'; |
||
| 62 | } |
||
| 63 | |||
| 64 | /** |
||
| 65 | * @return string the minimum value of the validation range. |
||
| 66 | */ |
||
| 67 | public function getMinValue() |
||
| 68 | { |
||
| 69 | return $this->getViewState('MinValue', ''); |
||
| 70 | } |
||
| 71 | |||
| 72 | /** |
||
| 73 | * Sets the minimum value of the validation range. |
||
| 74 | * @param string the minimum value |
||
| 75 | */ |
||
| 76 | public function setMinValue($value) |
||
| 77 | { |
||
| 78 | $this->setViewState('MinValue', TPropertyValue::ensureString($value), ''); |
||
| 79 | } |
||
| 80 | |||
| 81 | /** |
||
| 82 | * @return string the maximum value of the validation range. |
||
| 83 | */ |
||
| 84 | public function getMaxValue() |
||
| 85 | { |
||
| 86 | return $this->getViewState('MaxValue', ''); |
||
| 87 | } |
||
| 88 | |||
| 89 | /** |
||
| 90 | * Sets the maximum value of the validation range. |
||
| 91 | * @param string the maximum value |
||
| 92 | */ |
||
| 93 | public function setMaxValue($value) |
||
| 94 | { |
||
| 95 | $this->setViewState('MaxValue', TPropertyValue::ensureString($value), ''); |
||
| 96 | } |
||
| 97 | |||
| 98 | /** |
||
| 99 | * @param boolean true to perform strict comparison (i.e. strictly less than max and/or strictly greater than min). |
||
| 100 | */ |
||
| 101 | public function setStrictComparison($value) |
||
| 102 | { |
||
| 103 | $this->setViewState('StrictComparison', TPropertyValue::ensureBoolean($value), false); |
||
| 104 | } |
||
| 105 | |||
| 106 | /** |
||
| 107 | * @return boolean true to perform strict comparison. |
||
| 108 | */ |
||
| 109 | public function getStrictComparison() |
||
| 110 | { |
||
| 111 | return $this->getViewState('StrictComparison', false); |
||
| 112 | } |
||
| 113 | |||
| 114 | /** |
||
| 115 | * @return TRangeValidationDataType the data type that the values being compared are |
||
| 116 | * converted to before the comparison is made. Defaults to TRangeValidationDataType::String. |
||
| 117 | */ |
||
| 118 | public function getDataType() |
||
| 119 | { |
||
| 120 | return $this->getViewState('DataType', TRangeValidationDataType::String); |
||
|
|
|||
| 121 | } |
||
| 122 | |||
| 123 | /** |
||
| 124 | * Sets the data type that the values being compared are converted to before the comparison is made. |
||
| 125 | * @param TRangeValidationDataType the data type |
||
| 126 | */ |
||
| 127 | public function setDataType($value) |
||
| 128 | { |
||
| 129 | $this->setViewState('DataType', TPropertyValue::ensureEnum($value, 'Prado\\Web\\UI\\WebControls\\TRangeValidationDataType'), TRangeValidationDataType::String); |
||
| 130 | } |
||
| 131 | |||
| 132 | /** |
||
| 133 | * Sets the date format for a date validation |
||
| 134 | * @param string the date format value |
||
| 135 | */ |
||
| 136 | public function setDateFormat($value) |
||
| 137 | { |
||
| 138 | $this->setViewState('DateFormat', $value, ''); |
||
| 139 | } |
||
| 140 | |||
| 141 | /** |
||
| 142 | * @return string the date validation date format if any |
||
| 143 | */ |
||
| 144 | public function getDateFormat() |
||
| 145 | { |
||
| 146 | return $this->getViewState('DateFormat', ''); |
||
| 147 | } |
||
| 148 | |||
| 149 | /** |
||
| 150 | * @param string charset for string length comparison. |
||
| 151 | */ |
||
| 152 | public function setCharset($value) |
||
| 153 | { |
||
| 154 | $this->setViewState('Charset', $value, ''); |
||
| 155 | } |
||
| 156 | |||
| 157 | /** |
||
| 158 | * @return string charset for string length comparison. |
||
| 159 | */ |
||
| 160 | public function getCharset() |
||
| 161 | { |
||
| 162 | return $this->getViewState('Charset', ''); |
||
| 163 | } |
||
| 164 | |||
| 165 | /** |
||
| 166 | * This method overrides the parent's implementation. |
||
| 167 | * The validation succeeds if the input data is within the range. |
||
| 168 | * The validation always succeeds if the input data is empty. |
||
| 169 | * @return boolean whether the validation succeeds |
||
| 170 | */ |
||
| 171 | protected function evaluateIsValid() |
||
| 189 | } |
||
| 190 | } |
||
| 191 | |||
| 192 | /** |
||
| 193 | * Determine if the value is within the integer range. |
||
| 194 | * @param string value to validate true |
||
| 195 | * @return boolean true if within integer range. |
||
| 196 | */ |
||
| 197 | protected function isValidInteger($value) |
||
| 198 | { |
||
| 199 | $minValue = $this->getMinValue(); |
||
| 200 | $maxValue = $this->getMaxValue(); |
||
| 201 | |||
| 202 | $valid = preg_match('/^[-+]?[0-9]+$/', trim($value)); |
||
| 203 | $value = intval($value); |
||
| 204 | if($minValue !== '') |
||
| 205 | $valid = $valid && $this->isGreaterThan($value, intval($minValue)); |
||
| 206 | if($maxValue !== '') |
||
| 207 | $valid = $valid && $this->isLessThan($value, intval($maxValue)); |
||
| 208 | return $valid; |
||
| 209 | } |
||
| 210 | |||
| 211 | protected function isLessThan($left, $right) |
||
| 212 | { |
||
| 213 | return $this->getStrictComparison() ? $left < $right : $left <= $right; |
||
| 214 | } |
||
| 215 | |||
| 216 | protected function isGreaterThan($left, $right) |
||
| 217 | { |
||
| 218 | return $this->getStrictComparison() ? $left > $right : $left >= $right; |
||
| 219 | } |
||
| 220 | |||
| 221 | /** |
||
| 222 | * Determine if the value is within the specified float range. |
||
| 223 | * @param string value to validate |
||
| 224 | * @return boolean true if within range. |
||
| 225 | */ |
||
| 226 | protected function isValidFloat($value) |
||
| 227 | { |
||
| 228 | $minValue = $this->getMinValue(); |
||
| 229 | $maxValue = $this->getMaxValue(); |
||
| 230 | |||
| 231 | $valid = preg_match('/^[-+]?([0-9]*\.)?[0-9]+([eE][-+]?[0-9]+)?$/', trim($value)); |
||
| 232 | $value = floatval($value); |
||
| 233 | if($minValue !== '') |
||
| 234 | $valid = $valid && $this->isGreaterThan($value, floatval($minValue)); |
||
| 235 | if($maxValue !== '') |
||
| 236 | $valid = $valid && $this->isLessThan($value, floatval($maxValue)); |
||
| 237 | return $valid; |
||
| 238 | } |
||
| 239 | |||
| 240 | /** |
||
| 241 | * Determine if the date is within the specified range. |
||
| 242 | * Uses pradoParseDate and strtotime to get the date from string. |
||
| 243 | * @param string date as string to validate |
||
| 244 | * @return boolean true if within range. |
||
| 245 | */ |
||
| 246 | protected function isValidDate($value) |
||
| 247 | { |
||
| 248 | $minValue = $this->getMinValue(); |
||
| 249 | $maxValue = $this->getMaxValue(); |
||
| 250 | |||
| 251 | $valid = true; |
||
| 252 | |||
| 253 | $dateFormat = $this->getDateFormat(); |
||
| 254 | if($dateFormat !== '') |
||
| 255 | { |
||
| 256 | $formatter = new TSimpleDateFormatter($dateFormat); |
||
| 257 | $value = $formatter->parse($value); |
||
| 258 | if($minValue !== '') |
||
| 259 | $valid = $valid && $this->isGreaterThan($value, $formatter->parse($minValue)); |
||
| 260 | if($maxValue !== '') |
||
| 261 | $valid = $valid && $this->isLessThan($value, $formatter->parse($maxValue)); |
||
| 262 | return $valid; |
||
| 263 | } |
||
| 264 | else |
||
| 265 | { |
||
| 266 | $value = strtotime($value); |
||
| 267 | if($minValue !== '') |
||
| 268 | $valid = $valid && $this->isGreaterThan($value, strtotime($minValue)); |
||
| 269 | if($maxValue !== '') |
||
| 270 | $valid = $valid && $this->isLessThan($value, strtotime($maxValue)); |
||
| 271 | return $valid; |
||
| 272 | } |
||
| 273 | } |
||
| 274 | |||
| 275 | /** |
||
| 276 | * Compare the string with a minimum and a maxiumum value. |
||
| 277 | * Uses strcmp for comparision. |
||
| 278 | * @param string value to compare with. |
||
| 279 | * @return boolean true if the string is within range. |
||
| 280 | */ |
||
| 281 | protected function isValidString($value) |
||
| 292 | } |
||
| 293 | |||
| 294 | /** |
||
| 295 | * @param string string for comparision |
||
| 296 | * @return boolean true if min and max string length are satisfied. |
||
| 297 | */ |
||
| 298 | protected function isValidStringLength($value) |
||
| 319 | } |
||
| 320 | |||
| 321 | /** |
||
| 322 | * Returns an array of javascript validator options. |
||
| 323 | * @return array javascript validator options. |
||
| 324 | */ |
||
| 325 | protected function getClientScriptOptions() |
||
| 335 | } |
||
| 336 | } |