Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
61.11% |
22 / 36 |
|
33.33% |
4 / 12 |
CRAP | |
0.00% |
0 / 1 |
| PropertyListValidator | |
61.11% |
22 / 36 |
|
33.33% |
4 / 12 |
31.06 | |
0.00% |
0 / 1 |
| __construct | |
100.00% |
5 / 5 |
|
100.00% |
1 / 1 |
1 | |||
| getName | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| getMessage | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| setMessage | |
0.00% |
0 / 6 |
|
0.00% |
0 / 1 |
2 | |||
| evaluate | |
60.00% |
3 / 5 |
|
0.00% |
0 / 1 |
3.58 | |||
| acknowledge | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| withElementDecorator | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| getOptions | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| setOptions | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| setElement | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| isRemoteListForPrimaryOrSecondary | |
100.00% |
2 / 2 |
|
100.00% |
1 / 1 |
2 | |||
| isPrimaryOrSecondary | |
100.00% |
11 / 11 |
|
100.00% |
1 / 1 |
2 | |||
| 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) 2021 (original work) Open Assessment Technologies SA; |
| 19 | */ |
| 20 | |
| 21 | declare(strict_types=1); |
| 22 | |
| 23 | namespace oat\tao\model\Lists\Business\Validation; |
| 24 | |
| 25 | use InvalidArgumentException; |
| 26 | use oat\generis\model\data\Ontology; |
| 27 | use oat\oatbox\validator\ValidatorInterface; |
| 28 | use oat\tao\helpers\form\Decorator\ElementDecorator; |
| 29 | use oat\tao\helpers\form\elements\FormElementAware; |
| 30 | use oat\tao\helpers\form\validators\CrossElementEvaluationAware; |
| 31 | use oat\tao\model\featureFlag\FeatureFlagCheckerInterface; |
| 32 | use oat\tao\model\Lists\Business\Specification\PrimaryPropertySpecification; |
| 33 | use oat\tao\model\Lists\Business\Specification\PropertySpecificationContext; |
| 34 | use oat\tao\model\Lists\Business\Specification\RemoteListClassSpecification; |
| 35 | use oat\tao\model\Lists\Business\Specification\SecondaryPropertySpecification; |
| 36 | use tao_helpers_form_Form; |
| 37 | use tao_helpers_form_FormElement; |
| 38 | |
| 39 | class PropertyListValidator implements ValidatorInterface, CrossElementEvaluationAware, FormElementAware |
| 40 | { |
| 41 | /** @var Ontology */ |
| 42 | private $ontology; |
| 43 | |
| 44 | /** @var PrimaryPropertySpecification */ |
| 45 | private $primaryPropertySpecification; |
| 46 | |
| 47 | /** @var SecondaryPropertySpecification */ |
| 48 | private $secondaryPropertySpecification; |
| 49 | |
| 50 | /** @var tao_helpers_form_FormElement */ |
| 51 | private $element; |
| 52 | |
| 53 | /** @var array */ |
| 54 | private $options; |
| 55 | |
| 56 | /** @var ElementDecorator */ |
| 57 | private $elementDecorator; |
| 58 | |
| 59 | /** @var RemoteListClassSpecification */ |
| 60 | private $remoteListClassSpecification; |
| 61 | |
| 62 | /** @var FeatureFlagCheckerInterface */ |
| 63 | private $featureFlagChecker; |
| 64 | |
| 65 | public function __construct( |
| 66 | Ontology $ontology, |
| 67 | PrimaryPropertySpecification $primaryPropertySpecification, |
| 68 | SecondaryPropertySpecification $secondaryPropertySpecification, |
| 69 | RemoteListClassSpecification $remoteListClassSpecification, |
| 70 | FeatureFlagCheckerInterface $featureFlagChecker |
| 71 | ) { |
| 72 | $this->ontology = $ontology; |
| 73 | $this->primaryPropertySpecification = $primaryPropertySpecification; |
| 74 | $this->secondaryPropertySpecification = $secondaryPropertySpecification; |
| 75 | $this->remoteListClassSpecification = $remoteListClassSpecification; |
| 76 | $this->featureFlagChecker = $featureFlagChecker; |
| 77 | } |
| 78 | |
| 79 | /** |
| 80 | * @inheritdoc |
| 81 | */ |
| 82 | public function getName() |
| 83 | { |
| 84 | return self::class; |
| 85 | } |
| 86 | |
| 87 | /** |
| 88 | * @inheritdoc |
| 89 | */ |
| 90 | public function getMessage() |
| 91 | { |
| 92 | return __('Invalid value'); |
| 93 | } |
| 94 | |
| 95 | /** |
| 96 | * @inheritdoc |
| 97 | */ |
| 98 | public function setMessage($message) |
| 99 | { |
| 100 | throw new InvalidArgumentException( |
| 101 | sprintf( |
| 102 | 'Message for validator %s cannot be set.', |
| 103 | self::class |
| 104 | ) |
| 105 | ); |
| 106 | } |
| 107 | |
| 108 | /** |
| 109 | * @inheritdoc |
| 110 | */ |
| 111 | public function evaluate($values) |
| 112 | { |
| 113 | if ( |
| 114 | !$this->featureFlagChecker->isEnabled(FeatureFlagCheckerInterface::FEATURE_FLAG_LISTS_DEPENDENCY_ENABLED) |
| 115 | ) { |
| 116 | return true; |
| 117 | } |
| 118 | |
| 119 | if (!$this->isPrimaryOrSecondary()) { |
| 120 | return true; |
| 121 | } |
| 122 | |
| 123 | return $this->isRemoteListForPrimaryOrSecondary(); |
| 124 | } |
| 125 | |
| 126 | public function acknowledge(tao_helpers_form_Form $form): void |
| 127 | { |
| 128 | $this->elementDecorator = new ElementDecorator($this->ontology, $form, $this->element); |
| 129 | } |
| 130 | |
| 131 | public function withElementDecorator(ElementDecorator $elementDecorator): void |
| 132 | { |
| 133 | $this->elementDecorator = $elementDecorator; |
| 134 | } |
| 135 | |
| 136 | public function getOptions() |
| 137 | { |
| 138 | return $this->options; |
| 139 | } |
| 140 | |
| 141 | public function setOptions(array $options) |
| 142 | { |
| 143 | $this->options = $options; |
| 144 | } |
| 145 | |
| 146 | public function setElement(tao_helpers_form_FormElement $element): void |
| 147 | { |
| 148 | $this->element = $element; |
| 149 | } |
| 150 | |
| 151 | private function isRemoteListForPrimaryOrSecondary(): bool |
| 152 | { |
| 153 | $class = $this->elementDecorator->getRangeClass(); |
| 154 | |
| 155 | return $class && $this->remoteListClassSpecification->isSatisfiedBy($class); |
| 156 | } |
| 157 | |
| 158 | private function isPrimaryOrSecondary(): bool |
| 159 | { |
| 160 | $property = $this->elementDecorator->getProperty(); |
| 161 | |
| 162 | return $this->primaryPropertySpecification->isSatisfiedBy($property) || |
| 163 | $this->secondaryPropertySpecification->isSatisfiedBy( |
| 164 | new PropertySpecificationContext( |
| 165 | [ |
| 166 | PropertySpecificationContext::PARAM_PROPERTY => $property, |
| 167 | PropertySpecificationContext::PARAM_FORM_INDEX => $this->elementDecorator->getIndex(), |
| 168 | PropertySpecificationContext::PARAM_FORM_DATA => $this->elementDecorator->getFormData() |
| 169 | ] |
| 170 | ) |
| 171 | ); |
| 172 | } |
| 173 | } |