Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
0.00% |
0 / 54 |
|
0.00% |
0 / 1 |
CRAP | |
0.00% |
0 / 1 |
| tao_helpers_form_validators_DateTime | |
0.00% |
0 / 54 |
|
0.00% |
0 / 1 |
506 | |
0.00% |
0 / 1 |
| evaluate | |
0.00% |
0 / 54 |
|
0.00% |
0 / 1 |
506 | |||
| 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 | * Short description of class tao_helpers_form_validators_DateTime |
| 27 | * |
| 28 | * @access public |
| 29 | * @author Joel Bout, <joel.bout@tudor.lu> |
| 30 | * @package tao |
| 31 | |
| 32 | */ |
| 33 | class tao_helpers_form_validators_DateTime extends tao_helpers_form_Validator |
| 34 | { |
| 35 | /** |
| 36 | * (non-PHPdoc) |
| 37 | * @see tao_helpers_form_Validator::evaluate() |
| 38 | */ |
| 39 | public function evaluate($values) |
| 40 | { |
| 41 | $returnValue = (bool) false; |
| 42 | |
| 43 | |
| 44 | $value = trim($values); |
| 45 | |
| 46 | try { |
| 47 | $dateTime = new DateTime($value); |
| 48 | |
| 49 | // if no date given no need to go further. To check if not empty, use the NotEmpty validator |
| 50 | if ( |
| 51 | !empty($value) |
| 52 | && !empty($this->getOption('comparator')) |
| 53 | && $this->getOption('datetime2_ref') instanceof tao_helpers_form_FormElement |
| 54 | ) { |
| 55 | //try comparison: |
| 56 | try { |
| 57 | $dateTime2 = new DateTime($this->getOption('datetime2_ref')->getRawValue()); |
| 58 | } catch (Exception $e) { |
| 59 | } |
| 60 | |
| 61 | if ($dateTime2 instanceof DateTimeInterface) { |
| 62 | switch ($this->getOption('comparator')) { |
| 63 | case 'after': |
| 64 | case 'later': |
| 65 | case 'sup': |
| 66 | case '>': |
| 67 | if ($dateTime > $dateTime2) { |
| 68 | $returnValue = true; |
| 69 | } else { |
| 70 | $this->setMessage( |
| 71 | __('Invalid date range (must be after: %s)', $dateTime2->format('Y-m-d')) |
| 72 | ); |
| 73 | } |
| 74 | break; |
| 75 | |
| 76 | case '>=': |
| 77 | if ($dateTime >= $dateTime2) { |
| 78 | $returnValue = true; |
| 79 | } else { |
| 80 | $this->setMessage( |
| 81 | // phpcs:disable Generic.Files.LineLength |
| 82 | __('Invalid date range (must be after or the same as: %s)', $dateTime2->format('Y-m-d')) |
| 83 | // phpcs:enable Generic.Files.LineLength |
| 84 | ); |
| 85 | } |
| 86 | break; |
| 87 | |
| 88 | case 'before': |
| 89 | case 'earlier': |
| 90 | case 'inf': |
| 91 | case '<': |
| 92 | if ($dateTime < $dateTime2) { |
| 93 | $returnValue = true; |
| 94 | } else { |
| 95 | $this->setMessage( |
| 96 | __('Invalid date range (must be before: %s)', $dateTime2->format('Y-m-d')) |
| 97 | ); |
| 98 | } |
| 99 | break; |
| 100 | |
| 101 | case '<=': |
| 102 | if ($dateTime <= $dateTime2) { |
| 103 | $returnValue = true; |
| 104 | } else { |
| 105 | $this->setMessage( |
| 106 | // phpcs:disable Generic.Files.LineLength |
| 107 | __('Invalid date range (must be before or the same as: %s)', $dateTime2->format('Y-m-d')) |
| 108 | // phpcs:enable Generic.Files.LineLength |
| 109 | ); |
| 110 | } |
| 111 | break; |
| 112 | |
| 113 | default: |
| 114 | throw new common_Exception( |
| 115 | 'Usuported comparator in DateTime Validator: ' . $this->getOption('comparator') |
| 116 | ); |
| 117 | } |
| 118 | } |
| 119 | } else { |
| 120 | $returnValue = true; |
| 121 | } |
| 122 | } catch (Exception $e) { |
| 123 | $this->setMessage(__('The value of this field must be a valid date format, e.g. YYYY-MM-DD')); |
| 124 | $returnValue = false; |
| 125 | } |
| 126 | |
| 127 | |
| 128 | |
| 129 | return (bool) $returnValue; |
| 130 | } |
| 131 | } |