Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
22.00% |
11 / 50 |
|
0.00% |
0 / 1 |
CRAP | |
0.00% |
0 / 1 |
| tao_helpers_form_validators_Numeric | |
22.00% |
11 / 50 |
|
0.00% |
0 / 1 |
428.10 | |
0.00% |
0 / 1 |
| evaluate | |
22.00% |
11 / 50 |
|
0.00% |
0 / 1 |
428.10 | |||
| 1 | <?php |
| 2 | |
| 3 | /** |
| 4 | * This program is free software; you can redistribute it and/or |
| 5 | * modify it under the terms of the GNU General Public License |
| 6 | * as published by the Free Software Foundation; under version 2 |
| 7 | * of the License (non-upgradable). |
| 8 | * |
| 9 | * This program is distributed in the hope that it will be useful, |
| 10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 12 | * GNU General Public License for more details. |
| 13 | * |
| 14 | * You should have received a copy of the GNU General Public License |
| 15 | * along with this program; if not, write to the Free Software |
| 16 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. |
| 17 | * |
| 18 | * Copyright (c) 2008-2010 (original work) Deutsche Institut für Internationale Pädagogische Forschung |
| 19 | * (under the project TAO-TRANSFER); |
| 20 | * 2009-2012 (update and modification) Public Research Centre Henri Tudor |
| 21 | * (under the project TAO-SUSTAIN & TAO-DEV); |
| 22 | * |
| 23 | * |
| 24 | * The validators enable you to perform a validation callback on a form element. |
| 25 | * It's provide a model of validation and must be overriden. |
| 26 | * |
| 27 | * @author Jehan Bihin, <jehan.bihin@tudor.lu> |
| 28 | * @package tao |
| 29 | |
| 30 | */ |
| 31 | // @codingStandardsIgnoreLine |
| 32 | class tao_helpers_form_validators_Numeric extends tao_helpers_form_Validator |
| 33 | { |
| 34 | /** |
| 35 | * Short description of method evaluate |
| 36 | * |
| 37 | * @access public |
| 38 | * @author Jehan Bihin, <jehan.bihin@tudor.lu> |
| 39 | * @param |
| 40 | * values |
| 41 | * @return boolean |
| 42 | */ |
| 43 | public function evaluate($values) |
| 44 | { |
| 45 | $returnValue = false; |
| 46 | |
| 47 | $rowValue = $values; |
| 48 | $value = tao_helpers_Numeric::parseFloat($rowValue); |
| 49 | if (empty($rowValue) && !is_numeric($rowValue)) { |
| 50 | // no need to go further. To check if not empty, use the NotEmpty validator |
| 51 | return true; |
| 52 | } |
| 53 | if (! is_numeric($rowValue) || $value != $rowValue) { |
| 54 | $this->setMessage(__('The value of this field must be numeric')); |
| 55 | $returnValue = false; |
| 56 | } else { |
| 57 | if ($this->hasOption('min') || $this->hasOption('max')) { |
| 58 | if ($this->hasOption('min') && $this->hasOption('max')) { |
| 59 | if ($this->getOption('min') <= $value && $value <= $this->getOption('max')) { |
| 60 | $returnValue = true; |
| 61 | } else { |
| 62 | $this->setMessage(__( |
| 63 | 'Invalid field range (minimum value: %1$s, maximum value: %2$s)', |
| 64 | $this->getOption('min'), |
| 65 | $this->getOption('max') |
| 66 | )); |
| 67 | } |
| 68 | } elseif ($this->hasOption('min') && ! $this->hasOption('max')) { |
| 69 | if ($this->getOption('min') <= $value) { |
| 70 | $returnValue = true; |
| 71 | } else { |
| 72 | $this->setMessage(__('Invalid field range (minimum value: %s)', $this->getOption('min'))); |
| 73 | } |
| 74 | } elseif (! $this->hasOption('min') && $this->hasOption('max')) { |
| 75 | if ($value <= $this->getOption('max')) { |
| 76 | $returnValue = true; |
| 77 | } else { |
| 78 | $this->setMessage(__('Invalid field range (maximum value: %s)', $this->getOption('max'))); |
| 79 | } |
| 80 | } |
| 81 | } else { |
| 82 | $returnValue = true; |
| 83 | } |
| 84 | } |
| 85 | |
| 86 | // Test less, greater, equal to another |
| 87 | if ( |
| 88 | $returnValue |
| 89 | && $this->hasOption('integer2_ref') |
| 90 | && $this->getOption('integer2_ref') instanceof tao_helpers_form_FormElement |
| 91 | ) { |
| 92 | $secondElement = $this->getOption('integer2_ref'); |
| 93 | switch ($this->getOption('comparator')) { |
| 94 | case '>': |
| 95 | case 'sup': |
| 96 | if ($value > $secondElement->getRawValue()) { |
| 97 | $returnValue = true; |
| 98 | } else { |
| 99 | $returnValue = false; |
| 100 | } |
| 101 | break; |
| 102 | |
| 103 | case '<': |
| 104 | case 'inf': |
| 105 | if ($value < $secondElement->getRawValue()) { |
| 106 | $returnValue = true; |
| 107 | } else { |
| 108 | $returnValue = false; |
| 109 | } |
| 110 | break; |
| 111 | |
| 112 | case '=': |
| 113 | case 'equal': |
| 114 | if ($value == $secondElement->getRawValue()) { |
| 115 | $returnValue = true; |
| 116 | } else { |
| 117 | $returnValue = false; |
| 118 | } |
| 119 | break; |
| 120 | } |
| 121 | } |
| 122 | |
| 123 | return (bool) $returnValue; |
| 124 | } |
| 125 | } |