Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
Total | |
100.00% |
15 / 15 |
|
100.00% |
3 / 3 |
CRAP | |
100.00% |
1 / 1 |
tao_helpers_form_validators_AnyOf | |
100.00% |
15 / 15 |
|
100.00% |
3 / 3 |
7 | |
100.00% |
1 / 1 |
setOptions | |
100.00% |
3 / 3 |
|
100.00% |
1 / 1 |
2 | |||
evaluate | |
100.00% |
4 / 4 |
|
100.00% |
1 / 1 |
2 | |||
acknowledge | |
100.00% |
8 / 8 |
|
100.00% |
1 / 1 |
3 |
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) 2020 (original work) (update and modification) Open Assessment Technologies SA |
19 | */ |
20 | |
21 | declare(strict_types=1); |
22 | |
23 | use oat\tao\helpers\form\validators\CrossElementEvaluationAware; |
24 | |
25 | class tao_helpers_form_validators_AnyOf extends tao_helpers_form_Validator implements CrossElementEvaluationAware |
26 | { |
27 | /** @var tao_helpers_form_FormElement[]|[] */ |
28 | private $references = []; |
29 | |
30 | /** |
31 | * @throws common_Exception |
32 | * @throws common_exception_Error |
33 | */ |
34 | public function setOptions(array $options) |
35 | { |
36 | parent::setOptions($options); |
37 | |
38 | if (!$this->hasOption('reference')) { |
39 | throw new common_Exception(sprintf("No reference provided for %s validator", $this->getName())); |
40 | } |
41 | } |
42 | |
43 | public function evaluate($values) |
44 | { |
45 | $isEmpty = [empty($values)]; |
46 | foreach ($this->references as $reference) { |
47 | $isEmpty[] = empty($reference->getRawValue()); |
48 | } |
49 | return count(array_filter($isEmpty)) <= 1; |
50 | } |
51 | |
52 | public function acknowledge(tao_helpers_form_Form $form): void |
53 | { |
54 | $message = []; |
55 | foreach ($this->getOption('reference', []) as $ref) { |
56 | $ref = $ref instanceof tao_helpers_form_FormElement ? $ref : $form->getElement( |
57 | tao_helpers_Uri::encode($ref) |
58 | ); |
59 | |
60 | $this->references[] = $ref; |
61 | $message[] = $ref->getDescription(); |
62 | } |
63 | |
64 | $this->setMessage(__('This or one of %s must have a value', implode(',', $message))); |
65 | } |
66 | } |