Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
Total | |
92.00% |
46 / 50 |
|
60.00% |
3 / 5 |
CRAP | |
0.00% |
0 / 1 |
JsonLdListTripleEncoder | |
92.00% |
46 / 50 |
|
60.00% |
3 / 5 |
13.09 | |
0.00% |
0 / 1 |
__construct | |
100.00% |
3 / 3 |
|
100.00% |
1 / 1 |
1 | |||
encode | |
92.31% |
24 / 26 |
|
0.00% |
0 / 1 |
8.03 | |||
prepareSearch | |
71.43% |
5 / 7 |
|
0.00% |
0 / 1 |
2.09 | |||
getMetadataKey | |
100.00% |
2 / 2 |
|
100.00% |
1 / 1 |
1 | |||
isWidgetSupported | |
100.00% |
12 / 12 |
|
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) 2021 (original work) Open Assessment Technologies SA; |
19 | */ |
20 | |
21 | declare(strict_types=1); |
22 | |
23 | namespace oat\tao\model\export\Metadata\JsonLd; |
24 | |
25 | use core_kernel_classes_Class; |
26 | use core_kernel_classes_Property; |
27 | use core_kernel_classes_Resource; |
28 | use core_kernel_classes_Triple; |
29 | use InvalidArgumentException; |
30 | use oat\tao\helpers\form\elements\xhtml\SearchDropdown; |
31 | use oat\tao\helpers\form\elements\xhtml\SearchTextBox; |
32 | use oat\tao\model\Lists\Business\Domain\ValueCollectionSearchRequest; |
33 | use oat\tao\model\Lists\Business\Input\ValueCollectionSearchInput; |
34 | use oat\tao\model\Lists\Business\Service\ValueCollectionService; |
35 | use oat\tao\model\Lists\Business\Specification\LocalListClassSpecification; |
36 | use oat\tao\model\Lists\Business\Specification\RemoteListClassSpecification; |
37 | use tao_helpers_form_elements_Checkbox; |
38 | use tao_helpers_form_elements_Combobox; |
39 | use tao_helpers_form_elements_Radiobox; |
40 | use tao_helpers_form_elements_Treebox; |
41 | |
42 | class JsonLdListTripleEncoder implements JsonLdTripleEncoderInterface |
43 | { |
44 | /** @var ValueCollectionService */ |
45 | private $valueCollectionService; |
46 | |
47 | /** @var RemoteListClassSpecification */ |
48 | private $remoteListClassSpecification; |
49 | |
50 | /** @var LocalListClassSpecification */ |
51 | private $localListClassSpecification; |
52 | |
53 | public function __construct( |
54 | ValueCollectionService $valueCollectionService, |
55 | RemoteListClassSpecification $remoteListClassSpecification, |
56 | LocalListClassSpecification $localListClassSpecification |
57 | ) { |
58 | $this->valueCollectionService = $valueCollectionService; |
59 | $this->remoteListClassSpecification = $remoteListClassSpecification; |
60 | $this->localListClassSpecification = $localListClassSpecification; |
61 | } |
62 | |
63 | public function encode( |
64 | array $dataToEncode, |
65 | core_kernel_classes_Triple $triple, |
66 | core_kernel_classes_Property $property = null, |
67 | core_kernel_classes_Resource $widget = null |
68 | ): array { |
69 | if ($property === null || $widget === null) { |
70 | throw new InvalidArgumentException('The parameters $property and $widget are required'); |
71 | } |
72 | |
73 | $propertyRange = $property->getRange(); |
74 | |
75 | if (!$propertyRange instanceof core_kernel_classes_Class) { |
76 | return $dataToEncode; |
77 | } |
78 | |
79 | $isRemoteList = $this->remoteListClassSpecification->isSatisfiedBy($propertyRange); |
80 | $isLocalList = $this->localListClassSpecification->isSatisfiedBy($propertyRange); |
81 | |
82 | if (!$isRemoteList && !$isLocalList) { |
83 | return $dataToEncode; |
84 | } |
85 | |
86 | $key = $this->getMetadataKey($triple, $dataToEncode); |
87 | |
88 | if (empty($dataToEncode[$key][self::CONTEXT_TYPE])) { |
89 | $dataToEncode[$key] = [ |
90 | self::CONTEXT_TYPE => $widget->getUri(), |
91 | self::CONTEXT_ALIAS => $property->getAlias(), |
92 | self::CONTEXT_LABEL => $property->getLabel(), |
93 | self::CONTEXT_VALUE => [], |
94 | ]; |
95 | } |
96 | |
97 | $request = $this->prepareSearch($propertyRange, $property, $triple, $isRemoteList); |
98 | |
99 | $value = $this->valueCollectionService |
100 | ->findAll(new ValueCollectionSearchInput($request)) |
101 | ->extractValueByUri($triple->object); |
102 | |
103 | $dataToEncode[$key][self::CONTEXT_VALUE][] = [ |
104 | self::CONTEXT_VALUE => $triple->object, |
105 | self::CONTEXT_LABEL => $value ? $value->getLabel() : null, |
106 | ]; |
107 | |
108 | return $dataToEncode; |
109 | } |
110 | |
111 | private function prepareSearch( |
112 | core_kernel_classes_Class $propertyRange, |
113 | core_kernel_classes_Property $property, |
114 | core_kernel_classes_Triple $triple, |
115 | bool $isRemoteList |
116 | ): ValueCollectionSearchRequest { |
117 | $request = new ValueCollectionSearchRequest(); |
118 | |
119 | if ($isRemoteList) { |
120 | $request->setValueCollectionUri($propertyRange->getUri()); |
121 | $request->setUris($triple->object); |
122 | |
123 | return $request; |
124 | } |
125 | |
126 | $request->setPropertyUri($property->getUri()); |
127 | |
128 | return $request; |
129 | } |
130 | |
131 | private function getMetadataKey(core_kernel_classes_Triple $triple, array $dataToEncode): ?string |
132 | { |
133 | $context = (array)($dataToEncode['@context'] ?? []); |
134 | |
135 | return array_flip($context)[$triple->predicate] ?? null; |
136 | } |
137 | |
138 | public function isWidgetSupported(string $widgetUri): bool |
139 | { |
140 | return in_array( |
141 | $widgetUri, |
142 | [ |
143 | tao_helpers_form_elements_Radiobox::WIDGET_ID, |
144 | tao_helpers_form_elements_Treebox::WIDGET_ID, |
145 | tao_helpers_form_elements_Combobox::WIDGET_ID, |
146 | tao_helpers_form_elements_Checkbox::WIDGET_ID, |
147 | SearchTextBox::WIDGET_ID, |
148 | SearchDropdown::WIDGET_ID, |
149 | ], |
150 | true |
151 | ); |
152 | } |
153 | } |