Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
90.32% |
56 / 62 |
|
50.00% |
5 / 10 |
CRAP | |
0.00% |
0 / 1 |
| ElementDecorator | |
90.32% |
56 / 62 |
|
50.00% |
5 / 10 |
29.76 | |
0.00% |
0 / 1 |
| __construct | |
100.00% |
3 / 3 |
|
100.00% |
1 / 1 |
1 | |||
| getFormData | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| getIndex | |
100.00% |
3 / 3 |
|
100.00% |
1 / 1 |
2 | |||
| getProperty | |
87.50% |
7 / 8 |
|
0.00% |
0 / 1 |
3.02 | |||
| getRangeClass | |
100.00% |
7 / 7 |
|
100.00% |
1 / 1 |
3 | |||
| getCurrentWidgetUri | |
83.33% |
5 / 6 |
|
0.00% |
0 / 1 |
3.04 | |||
| getNewWidgetUri | |
100.00% |
5 / 5 |
|
100.00% |
1 / 1 |
2 | |||
| getListValues | |
87.50% |
7 / 8 |
|
0.00% |
0 / 1 |
4.03 | |||
| getPrimaryElementsDecorators | |
90.00% |
9 / 10 |
|
0.00% |
0 / 1 |
5.03 | |||
| transformListValuesToUris | |
81.82% |
9 / 11 |
|
0.00% |
0 / 1 |
5.15 | |||
| 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\helpers\form\Decorator; |
| 24 | |
| 25 | use tao_helpers_Uri; |
| 26 | use tao_helpers_form_Form; |
| 27 | use core_kernel_classes_Class; |
| 28 | use core_kernel_classes_Property; |
| 29 | use core_kernel_classes_Resource; |
| 30 | use tao_helpers_form_FormElement; |
| 31 | use oat\generis\model\data\Ontology; |
| 32 | use tao_helpers_form_GenerisFormFactory; |
| 33 | use oat\tao\helpers\form\elements\ElementValue; |
| 34 | use oat\tao\helpers\form\elements\AbstractSearchElement; |
| 35 | |
| 36 | class ElementDecorator |
| 37 | { |
| 38 | /** @var Ontology */ |
| 39 | private $ontology; |
| 40 | |
| 41 | /** @var tao_helpers_form_FormElement */ |
| 42 | private $element; |
| 43 | |
| 44 | /** @var tao_helpers_form_Form */ |
| 45 | private $form; |
| 46 | |
| 47 | /** @var array */ |
| 48 | private $cache = []; |
| 49 | |
| 50 | public function __construct( |
| 51 | Ontology $ontology, |
| 52 | tao_helpers_form_Form $form, |
| 53 | tao_helpers_form_FormElement $element |
| 54 | ) { |
| 55 | $this->ontology = $ontology; |
| 56 | $this->form = $form; |
| 57 | $this->element = $element; |
| 58 | } |
| 59 | |
| 60 | public function getFormData(): array |
| 61 | { |
| 62 | return $this->form->getValues(); |
| 63 | } |
| 64 | |
| 65 | public function getIndex(): int |
| 66 | { |
| 67 | if (!array_key_exists(__METHOD__, $this->cache)) { |
| 68 | $this->cache[__METHOD__] = (int) (explode('_', $this->element->getName())[0] ?? 0); |
| 69 | } |
| 70 | |
| 71 | return $this->cache[__METHOD__]; |
| 72 | } |
| 73 | |
| 74 | public function getProperty(): ?core_kernel_classes_Property |
| 75 | { |
| 76 | if (!array_key_exists(__METHOD__, $this->cache)) { |
| 77 | $propertyUri = $this->getFormData()[$this->getIndex() . '_uri'] |
| 78 | ?? tao_helpers_Uri::decode($this->element->getName()); |
| 79 | |
| 80 | $property = $this->ontology->getProperty($propertyUri); |
| 81 | |
| 82 | $this->cache[__METHOD__] = $property->exists() |
| 83 | ? $property |
| 84 | : null; |
| 85 | } |
| 86 | |
| 87 | return $this->cache[__METHOD__]; |
| 88 | } |
| 89 | |
| 90 | public function getRangeClass(): ?core_kernel_classes_Class |
| 91 | { |
| 92 | if (!array_key_exists(__METHOD__, $this->cache)) { |
| 93 | $uri = $this->getFormData()[$this->getIndex() . '_range'] ?? null; |
| 94 | $range = empty($uri) |
| 95 | ? null |
| 96 | : $this->ontology->getClass(tao_helpers_Uri::decode($uri)); |
| 97 | |
| 98 | $this->cache[__METHOD__] = $range ?? $this->getProperty()->getRange(); |
| 99 | } |
| 100 | |
| 101 | return $this->cache[__METHOD__]; |
| 102 | } |
| 103 | |
| 104 | public function getCurrentWidgetUri(): ?string |
| 105 | { |
| 106 | if (!array_key_exists(__METHOD__, $this->cache)) { |
| 107 | $property = $this->getProperty(); |
| 108 | |
| 109 | $this->cache[__METHOD__] = $property->getWidget() instanceof core_kernel_classes_Resource |
| 110 | ? $property->getWidget()->getUri() |
| 111 | : null; |
| 112 | } |
| 113 | |
| 114 | return $this->cache[__METHOD__]; |
| 115 | } |
| 116 | |
| 117 | public function getNewWidgetUri(): ?string |
| 118 | { |
| 119 | if (!array_key_exists(__METHOD__, $this->cache)) { |
| 120 | $this->cache[__METHOD__] = tao_helpers_form_GenerisFormFactory::getWidgetUriById( |
| 121 | (string)$this->element->getRawValue() |
| 122 | ); |
| 123 | } |
| 124 | |
| 125 | return $this->cache[__METHOD__]; |
| 126 | } |
| 127 | |
| 128 | public function getListValues(): array |
| 129 | { |
| 130 | if (!array_key_exists(__METHOD__, $this->cache)) { |
| 131 | $listValues = array_filter(explode(',', $this->element->getInputValue() ?? '')); |
| 132 | |
| 133 | if (empty($listValues)) { |
| 134 | $listValues = $this->element instanceof AbstractSearchElement |
| 135 | ? $this->element->getValues() |
| 136 | : [$this->element->getRawValue()]; |
| 137 | } |
| 138 | |
| 139 | $this->cache[__METHOD__] = $this->transformListValuesToUris($listValues); |
| 140 | } |
| 141 | |
| 142 | return $this->cache[__METHOD__]; |
| 143 | } |
| 144 | |
| 145 | /** |
| 146 | * @return ElementDecorator[] |
| 147 | */ |
| 148 | public function getPrimaryElementsDecorators(): array |
| 149 | { |
| 150 | if (!array_key_exists(__METHOD__, $this->cache)) { |
| 151 | if ($this->getProperty() === null) { |
| 152 | return []; |
| 153 | } |
| 154 | |
| 155 | $primaryDecorators = []; |
| 156 | |
| 157 | foreach ($this->getProperty()->getDependsOnPropertyCollection() as $primaryProperty) { |
| 158 | $primaryElement = $this->form->getElement(tao_helpers_Uri::encode($primaryProperty->getUri())); |
| 159 | |
| 160 | if ($primaryElement !== null) { |
| 161 | $primaryDecorators[] = new self($this->ontology, $this->form, $primaryElement); |
| 162 | } |
| 163 | } |
| 164 | |
| 165 | $this->cache[__METHOD__] = $primaryDecorators; |
| 166 | } |
| 167 | |
| 168 | return $this->cache[__METHOD__]; |
| 169 | } |
| 170 | |
| 171 | /** |
| 172 | * @param string|array $values |
| 173 | * |
| 174 | * @return string[] |
| 175 | */ |
| 176 | private function transformListValuesToUris($values): array |
| 177 | { |
| 178 | if (is_string($values)) { |
| 179 | $values = [trim($values)]; |
| 180 | } |
| 181 | |
| 182 | if (!is_array($values)) { |
| 183 | return []; |
| 184 | } |
| 185 | |
| 186 | $uris = []; |
| 187 | |
| 188 | foreach ($values as $value) { |
| 189 | $uri = $value instanceof ElementValue |
| 190 | ? $value->getUri() |
| 191 | : trim($value ?? ''); |
| 192 | |
| 193 | $uris[tao_helpers_Uri::encode($uri)] = tao_helpers_Uri::decode($uri); |
| 194 | } |
| 195 | |
| 196 | return array_filter($uris); |
| 197 | } |
| 198 | } |