Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
100.00% covered (success)
100.00%
24 / 24
100.00% covered (success)
100.00%
8 / 8
CRAP
100.00% covered (success)
100.00%
1 / 1
SanitizerValidationFeeder
100.00% covered (success)
100.00%
24 / 24
100.00% covered (success)
100.00%
8 / 8
17
100.00% covered (success)
100.00%
1 / 1
 setForm
100.00% covered (success)
100.00%
2 / 2
100.00% covered (success)
100.00%
1 / 1
1
 addValidator
100.00% covered (success)
100.00%
2 / 2
100.00% covered (success)
100.00%
1 / 1
1
 addElement
100.00% covered (success)
100.00%
4 / 4
100.00% covered (success)
100.00%
1 / 1
3
 addElementByUri
100.00% covered (success)
100.00%
5 / 5
100.00% covered (success)
100.00%
1 / 1
3
 feed
100.00% covered (success)
100.00%
7 / 7
100.00% covered (success)
100.00%
1 / 1
5
 getValue
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 assertHasForm
100.00% covered (success)
100.00%
2 / 2
100.00% covered (success)
100.00%
1 / 1
2
 isElementValid
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
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) 2022 (original work) Open Assessment Technologies SA.
19 */
20
21declare(strict_types=1);
22
23namespace oat\tao\helpers\form\Feeder;
24
25use tao_helpers_Uri;
26use RuntimeException;
27use tao_helpers_form_Form;
28use InvalidArgumentException;
29use tao_helpers_form_Validator;
30use tao_helpers_form_FormElement;
31use tao_helpers_form_elements_Textbox;
32use tao_helpers_form_elements_Textarea;
33
34class SanitizerValidationFeeder implements SanitizerValidationFeederInterface
35{
36    private const ALLOWED_WIDGETS = [
37        tao_helpers_form_elements_Textbox::WIDGET_ID,
38        tao_helpers_form_elements_Textarea::WIDGET_ID,
39    ];
40
41    /** @var tao_helpers_form_Form */
42    private $form;
43
44    /** @var tao_helpers_form_Validator[] */
45    private $validators = [];
46
47    /** @var tao_helpers_form_FormElement[] */
48    private $elements = [];
49
50    public function setForm(tao_helpers_form_Form $form): SanitizerValidationFeederInterface
51    {
52        $this->form = $form;
53
54        return $this;
55    }
56
57    public function addValidator(tao_helpers_form_Validator $validator): SanitizerValidationFeederInterface
58    {
59        $this->validators[] = $validator;
60
61        return $this;
62    }
63
64    /**
65     * @inheritDoc
66     */
67    public function addElement(tao_helpers_form_FormElement $element): SanitizerValidationFeederInterface
68    {
69        $this->assertHasForm();
70
71        if ($this->isElementValid($element) && $this->form->hasElement(tao_helpers_Uri::encode($element->getName()))) {
72            $this->elements[] = $element;
73        }
74
75        return $this;
76    }
77
78    /**
79     * @inheritDoc
80     */
81    public function addElementByUri(string $elementUri): SanitizerValidationFeederInterface
82    {
83        $this->assertHasForm();
84
85        $element = $this->form->getElement(tao_helpers_Uri::encode($elementUri));
86
87        if ($element !== null && $this->isElementValid($element)) {
88            $this->elements[] = $element;
89        }
90
91        return $this;
92    }
93
94    public function feed(): void
95    {
96        if (empty($this->validators)) {
97            return;
98        }
99
100        foreach ($this->elements as $element) {
101            if (empty($this->getValue($element))) {
102                continue;
103            }
104
105            foreach ($this->validators as $validator) {
106                $element->addValidator($validator);
107            }
108        }
109    }
110
111    private function getValue(tao_helpers_form_FormElement $element): ?string
112    {
113        return $element->getInputValue() ?? $element->getRawValue();
114    }
115
116    private function assertHasForm(): void
117    {
118        if (!isset($this->form)) {
119            throw new RuntimeException('Cannot add element because form is not set');
120        }
121    }
122
123    private function isElementValid(tao_helpers_form_FormElement $element): bool
124    {
125        return in_array($element->getWidget(), self::ALLOWED_WIDGETS, true);
126    }
127}