Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
Total | |
100.00% |
29 / 29 |
|
100.00% |
3 / 3 |
CRAP | |
100.00% |
1 / 1 |
JsonLdBasicTripleEncoder | |
100.00% |
29 / 29 |
|
100.00% |
3 / 3 |
5 | |
100.00% |
1 / 1 |
encode | |
100.00% |
15 / 15 |
|
100.00% |
1 / 1 |
3 | |||
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_Property; |
26 | use core_kernel_classes_Resource; |
27 | use core_kernel_classes_Triple; |
28 | use InvalidArgumentException; |
29 | use tao_helpers_form_elements_Calendar; |
30 | use tao_helpers_form_elements_Hiddenbox; |
31 | use tao_helpers_form_elements_Htmlarea; |
32 | use tao_helpers_form_elements_Readonly; |
33 | use tao_helpers_form_elements_Textarea; |
34 | use tao_helpers_form_elements_Textbox; |
35 | |
36 | class JsonLdBasicTripleEncoder implements JsonLdTripleEncoderInterface |
37 | { |
38 | public function encode( |
39 | array $dataToEncode, |
40 | core_kernel_classes_Triple $triple, |
41 | core_kernel_classes_Property $property = null, |
42 | core_kernel_classes_Resource $widget = null |
43 | ): array { |
44 | if ($property === null || $widget === null) { |
45 | throw new InvalidArgumentException('The parameters $property and $widget are required'); |
46 | } |
47 | |
48 | $key = $this->getMetadataKey($triple, $dataToEncode); |
49 | |
50 | $dataToEncode[$key] = [ |
51 | self::CONTEXT_TYPE => $widget->getUri(), |
52 | self::CONTEXT_ALIAS => $property->getAlias(), |
53 | self::CONTEXT_LABEL => $property->getLabel(), |
54 | self::CONTEXT_VALUE => [ |
55 | [ |
56 | self::CONTEXT_LABEL => null, |
57 | self::CONTEXT_VALUE => $triple->object, |
58 | ] |
59 | ] |
60 | ]; |
61 | |
62 | return $dataToEncode; |
63 | } |
64 | |
65 | private function getMetadataKey(core_kernel_classes_Triple $triple, array $dataToEncode): ?string |
66 | { |
67 | $context = (array)($dataToEncode['@context'] ?? []); |
68 | |
69 | return array_flip($context)[$triple->predicate] ?? null; |
70 | } |
71 | |
72 | public function isWidgetSupported(string $widgetUri): bool |
73 | { |
74 | return in_array( |
75 | $widgetUri, |
76 | [ |
77 | tao_helpers_form_elements_Textbox::WIDGET_ID, |
78 | tao_helpers_form_elements_Textarea::WIDGET_ID, |
79 | tao_helpers_form_elements_Htmlarea::WIDGET_ID, |
80 | tao_helpers_form_elements_Calendar::WIDGET_ID, |
81 | tao_helpers_form_elements_Hiddenbox::WIDGET_ID, |
82 | tao_helpers_form_elements_Readonly::WIDGET_ID, |
83 | ], |
84 | true |
85 | ); |
86 | } |
87 | } |