Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
0.00% |
0 / 34 |
|
0.00% |
0 / 2 |
CRAP | |
0.00% |
0 / 1 |
| tao_helpers_form_validators_Length | |
0.00% |
0 / 34 |
|
0.00% |
0 / 2 |
306 | |
0.00% |
0 / 1 |
| setOptions | |
0.00% |
0 / 21 |
|
0.00% |
0 / 1 |
56 | |||
| evaluate | |
0.00% |
0 / 13 |
|
0.00% |
0 / 1 |
110 | |||
| 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 | |
| 25 | /** |
| 26 | * Validate string lenght |
| 27 | * |
| 28 | * @access public |
| 29 | * @author Joel Bout, <joel.bout@tudor.lu> |
| 30 | * @package tao |
| 31 | */ |
| 32 | class tao_helpers_form_validators_Length extends tao_helpers_form_Validator |
| 33 | { |
| 34 | public function setOptions(array $options) |
| 35 | { |
| 36 | parent::setOptions($options); |
| 37 | |
| 38 | if ($this->hasOption('min') && $this->hasOption('max')) { |
| 39 | $this->setMessage(sprintf( |
| 40 | '%s (%s, %s)', |
| 41 | __('Invalid field length'), |
| 42 | __('minimum %s', $this->getOption('min')), |
| 43 | __('maximum %s', $this->getOption('max')) |
| 44 | )); |
| 45 | } elseif ($this->hasOption('min') && !$this->hasOption('max')) { |
| 46 | $this->setMessage(sprintf( |
| 47 | '%s (%s)', |
| 48 | __('This field is too short'), |
| 49 | __('minimum %s', $this->getOption('min')) |
| 50 | )); |
| 51 | } elseif (!$this->hasOption('min') && $this->hasOption('max')) { |
| 52 | $this->setMessage(sprintf( |
| 53 | '%s (%s)', |
| 54 | __('This field is too long'), |
| 55 | __('maximum %s', $this->getOption('max')) |
| 56 | )); |
| 57 | } else { |
| 58 | throw new Exception(__("Please set 'min' and/or 'max' options!")); |
| 59 | } |
| 60 | } |
| 61 | |
| 62 | /** |
| 63 | * Short description of method evaluate |
| 64 | * |
| 65 | * @access public |
| 66 | * @author Joel Bout, <joel.bout@tudor.lu> |
| 67 | * @param values |
| 68 | * @return boolean |
| 69 | */ |
| 70 | public function evaluate($values) |
| 71 | { |
| 72 | $returnValue = (bool) false; |
| 73 | |
| 74 | |
| 75 | $returnValue = true; |
| 76 | |
| 77 | $values = is_array($values) ? $values : [$values]; |
| 78 | foreach ($values as $value) { |
| 79 | if ($this->hasOption('min') && mb_strlen($value) < $this->getOption('min')) { |
| 80 | if ($this->hasOption('allowEmpty') && $this->getOption('allowEmpty') && empty($value)) { |
| 81 | continue; |
| 82 | } else { |
| 83 | $returnValue = false; |
| 84 | break; |
| 85 | } |
| 86 | } |
| 87 | if ($this->hasOption('max') && mb_strlen($value) > $this->getOption('max')) { |
| 88 | $returnValue = false; |
| 89 | break; |
| 90 | } |
| 91 | } |
| 92 | |
| 93 | |
| 94 | return (bool) $returnValue; |
| 95 | } |
| 96 | } |