Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
0.00% |
0 / 24 |
|
0.00% |
0 / 7 |
CRAP | |
0.00% |
0 / 1 |
| AbstractFormValidator | |
0.00% |
0 / 24 |
|
0.00% |
0 / 7 |
210 | |
0.00% |
0 / 1 |
| validate | |
0.00% |
0 / 5 |
|
0.00% |
0 / 1 |
12 | |||
| addError | |
0.00% |
0 / 4 |
|
0.00% |
0 / 1 |
6 | |||
| validField | |
0.00% |
0 / 7 |
|
0.00% |
0 / 1 |
12 | |||
| executeTest | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| getErrors | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| getError | |
0.00% |
0 / 3 |
|
0.00% |
0 / 1 |
6 | |||
| isValid | |
0.00% |
0 / 3 |
|
0.00% |
0 / 1 |
6 | |||
| 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) 2016 (original work) Open Assessment Technologies SA; |
| 19 | * |
| 20 | */ |
| 21 | |
| 22 | namespace oat\oatbox\validator; |
| 23 | |
| 24 | /** |
| 25 | * base of validator |
| 26 | * @author Christophe GARCIA <christopheg@taotesting.com> |
| 27 | */ |
| 28 | abstract class AbstractFormValidator |
| 29 | { |
| 30 | /** |
| 31 | * configuration of validation |
| 32 | * @var array |
| 33 | */ |
| 34 | protected $validation = []; |
| 35 | /** |
| 36 | * |
| 37 | * @var array |
| 38 | */ |
| 39 | protected $errors = []; |
| 40 | /** |
| 41 | * |
| 42 | * @var boolean |
| 43 | */ |
| 44 | protected $isValid; |
| 45 | |
| 46 | |
| 47 | /** |
| 48 | * valid a form. |
| 49 | * @param array $form |
| 50 | * @return $this |
| 51 | */ |
| 52 | public function validate(array $form) |
| 53 | { |
| 54 | $this->isValid = true; |
| 55 | foreach ($form as $field => $value) { |
| 56 | if (array_key_exists($field, $this->validation)) { |
| 57 | $this->validField($field, $value, $this->validation[$field]); |
| 58 | } |
| 59 | } |
| 60 | return $this; |
| 61 | } |
| 62 | |
| 63 | /** |
| 64 | * add an error message |
| 65 | * @param type $field |
| 66 | * @param type $message |
| 67 | * @return \oat\oatbox\validator\AbstractFormValidator |
| 68 | */ |
| 69 | protected function addError($field, $message) |
| 70 | { |
| 71 | if (array_key_exists($field, $this->errors)) { |
| 72 | $this->errors[$field][] = $message; |
| 73 | } else { |
| 74 | $this->errors[$field] = [$message]; |
| 75 | } |
| 76 | return $this; |
| 77 | } |
| 78 | |
| 79 | /** |
| 80 | * execute all validation for a field |
| 81 | * @param string $field |
| 82 | * @param mixed $value |
| 83 | * @param array $config |
| 84 | */ |
| 85 | protected function validField($field, $value, $config) |
| 86 | { |
| 87 | foreach ($config as $validator) { |
| 88 | $class = $validator['class']; |
| 89 | $option = $validator['options']; |
| 90 | /* @var $test ValidatorInterface */ |
| 91 | $test = new $class($option); |
| 92 | if (!$this->executeTest($value, $test)) { |
| 93 | $this->isValid = false; |
| 94 | $this->addError($field, $test->getMessage()); |
| 95 | } |
| 96 | } |
| 97 | } |
| 98 | /** |
| 99 | * execute a validator |
| 100 | * @param mixed $value |
| 101 | * @param \oat\oatbox\validator\ValidatorInterface $validator |
| 102 | * @return boolena |
| 103 | */ |
| 104 | protected function executeTest($value, ValidatorInterface $validator) |
| 105 | { |
| 106 | return $validator->evaluate($value); |
| 107 | } |
| 108 | |
| 109 | /** |
| 110 | * return all errors |
| 111 | * @return array |
| 112 | */ |
| 113 | public function getErrors() |
| 114 | { |
| 115 | return $this->errors; |
| 116 | } |
| 117 | /** |
| 118 | * return error message for a field |
| 119 | * @param string $field |
| 120 | * @return array |
| 121 | */ |
| 122 | public function getError($field) |
| 123 | { |
| 124 | if (array_key_exists($field, $this->errors)) { |
| 125 | return $this->errors[$field]; |
| 126 | } |
| 127 | return null; |
| 128 | } |
| 129 | /** |
| 130 | * return form valiation status |
| 131 | * @return boolean |
| 132 | * @throws \RuntimeException |
| 133 | */ |
| 134 | public function isValid() |
| 135 | { |
| 136 | if (is_null($this->isValid)) { |
| 137 | throw new \RuntimeException('you must validate a form'); |
| 138 | } |
| 139 | return $this->isValid; |
| 140 | } |
| 141 | } |