Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
Total | |
100.00% |
29 / 29 |
|
100.00% |
7 / 7 |
CRAP | |
100.00% |
1 / 1 |
PropertyChangedValidator | |
100.00% |
29 / 29 |
|
100.00% |
7 / 7 |
20 | |
100.00% |
1 / 1 |
isPropertyChanged | |
100.00% |
6 / 6 |
|
100.00% |
1 / 1 |
6 | |||
isPropertyTypeChanged | |
100.00% |
10 / 10 |
|
100.00% |
1 / 1 |
7 | |||
isRangeChanged | |
100.00% |
3 / 3 |
|
100.00% |
1 / 1 |
2 | |||
isValidationRulesChanged | |
100.00% |
5 / 5 |
|
100.00% |
1 / 1 |
1 | |||
isAliasChanged | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
isDependsOnPropertyCollectionChanged | |
100.00% |
3 / 3 |
|
100.00% |
1 / 1 |
1 | |||
areArraysEqual | |
100.00% |
1 / 1 |
|
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\validator; |
24 | |
25 | use oat\generis\model\GenerisRdf; |
26 | use oat\generis\model\WidgetRdf; |
27 | use core_kernel_classes_Property; |
28 | use oat\tao\model\dto\OldProperty; |
29 | use oat\oatbox\service\ConfigurableService; |
30 | use oat\tao\helpers\form\ValidationRuleRegistry; |
31 | |
32 | class PropertyChangedValidator extends ConfigurableService |
33 | { |
34 | public function isPropertyChanged(core_kernel_classes_Property $property, OldProperty $oldProperty): bool |
35 | { |
36 | return $property->getLabel() !== $oldProperty->getLabel() |
37 | || $this->isAliasChanged($property, $oldProperty) |
38 | || $this->isPropertyTypeChanged($property, $oldProperty) |
39 | || $this->isRangeChanged($property, $oldProperty) |
40 | || $this->isValidationRulesChanged($property, $oldProperty) |
41 | || $this->isDependsOnPropertyCollectionChanged($property, $oldProperty); |
42 | } |
43 | |
44 | public function isPropertyTypeChanged(core_kernel_classes_Property $property, OldProperty $oldProperty): bool |
45 | { |
46 | $currentPropertyType = $property->getOnePropertyValue( |
47 | $property->getProperty(WidgetRdf::PROPERTY_WIDGET) |
48 | ); |
49 | $oldPropertyType = $oldProperty->getPropertyType(); |
50 | |
51 | if ($currentPropertyType === null && $oldPropertyType === null) { |
52 | return false; |
53 | } |
54 | |
55 | if ( |
56 | ($currentPropertyType !== null && $oldPropertyType === null) |
57 | || ($currentPropertyType === null && $oldPropertyType !== null) |
58 | ) { |
59 | return true; |
60 | } |
61 | |
62 | return $currentPropertyType->getUri() !== $oldPropertyType->getUri(); |
63 | } |
64 | |
65 | public function isRangeChanged(core_kernel_classes_Property $property, OldProperty $oldProperty): bool |
66 | { |
67 | $propertyRange = $property->getRange(); |
68 | $propertyRangeUri = $propertyRange ? $propertyRange->getUri() : null; |
69 | |
70 | return $propertyRangeUri !== $oldProperty->getRangeUri(); |
71 | } |
72 | |
73 | public function isValidationRulesChanged(core_kernel_classes_Property $property, OldProperty $oldProperty): bool |
74 | { |
75 | $propertyValidationRules = $property->getPropertyValues( |
76 | $property->getProperty(ValidationRuleRegistry::PROPERTY_VALIDATION_RULE) |
77 | ); |
78 | $oldPropertyValidationRules = $oldProperty->getValidationRules(); |
79 | |
80 | return !$this->areArraysEqual($propertyValidationRules, $oldPropertyValidationRules); |
81 | } |
82 | |
83 | public function isAliasChanged(core_kernel_classes_Property $property, OldProperty $oldProperty): bool |
84 | { |
85 | return $property->getAlias() !== $oldProperty->getAlias(); |
86 | } |
87 | |
88 | public function isDependsOnPropertyCollectionChanged( |
89 | core_kernel_classes_Property $property, |
90 | OldProperty $oldProperty |
91 | ): bool { |
92 | return !$property->getDependsOnPropertyCollection()->isEqual( |
93 | $oldProperty->getDependsOnPropertyCollection() |
94 | ); |
95 | } |
96 | |
97 | private function areArraysEqual(array $array1, array $array2): bool |
98 | { |
99 | return empty(array_diff($array1, $array2)) && empty(array_diff($array2, $array1)); |
100 | } |
101 | } |