Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
91.67% covered (success)
91.67%
22 / 24
66.67% covered (warning)
66.67%
4 / 6
CRAP
0.00% covered (danger)
0.00%
0 / 1
JsonLdTripleEncoderProxy
91.67% covered (success)
91.67%
22 / 24
66.67% covered (warning)
66.67%
4 / 6
12.08
0.00% covered (danger)
0.00%
0 / 1
 __construct
66.67% covered (warning)
66.67%
2 / 3
0.00% covered (danger)
0.00%
0 / 1
2.15
 addEncoder
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 encode
92.31% covered (success)
92.31%
12 / 13
0.00% covered (danger)
0.00%
0 / 1
4.01
 isWidgetSupported
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 getProperty
100.00% covered (success)
100.00%
3 / 3
100.00% covered (success)
100.00%
1 / 1
2
 getWidget
100.00% covered (success)
100.00%
3 / 3
100.00% covered (success)
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
21declare(strict_types=1);
22
23namespace oat\tao\model\export\Metadata\JsonLd;
24
25use core_kernel_classes_Property;
26use core_kernel_classes_Resource;
27use core_kernel_classes_Triple;
28use oat\generis\model\data\Ontology;
29use oat\tao\helpers\form\elements\xhtml\SearchDropdown;
30use oat\tao\helpers\form\elements\xhtml\SearchTextBox;
31use tao_helpers_form_elements_Calendar;
32use tao_helpers_form_elements_Checkbox;
33use tao_helpers_form_elements_Combobox;
34use tao_helpers_form_elements_Hiddenbox;
35use tao_helpers_form_elements_Htmlarea;
36use tao_helpers_form_elements_Radiobox;
37use tao_helpers_form_elements_Textarea;
38use tao_helpers_form_elements_Textbox;
39use tao_helpers_form_elements_Treebox;
40
41class JsonLdTripleEncoderProxy implements JsonLdTripleEncoderInterface
42{
43    private const ALLOWED_WIDGETS = [
44        tao_helpers_form_elements_Textbox::WIDGET_ID,
45        tao_helpers_form_elements_Textarea::WIDGET_ID,
46        tao_helpers_form_elements_Htmlarea::WIDGET_ID,
47        tao_helpers_form_elements_Radiobox::WIDGET_ID,
48        tao_helpers_form_elements_Treebox::WIDGET_ID,
49        tao_helpers_form_elements_Combobox::WIDGET_ID,
50        tao_helpers_form_elements_Checkbox::WIDGET_ID,
51        SearchTextBox::WIDGET_ID,
52        SearchDropdown::WIDGET_ID,
53        tao_helpers_form_elements_Calendar::WIDGET_ID,
54        tao_helpers_form_elements_Hiddenbox::WIDGET_ID
55    ];
56
57    /** @var Ontology */
58    private $ontology;
59
60    /** @var JsonLdTripleEncoderInterface[] */
61    private $encoders = [];
62
63    /** @var array */
64    private $allowedWidgets = self::ALLOWED_WIDGETS;
65
66    /** @var core_kernel_classes_Property[] */
67    private $propertyCache = [];
68
69    /** @var core_kernel_classes_Property[] */
70    private $widgetCache = [];
71
72    public function __construct(Ontology $ontology, array $allowedWidgets = [])
73    {
74        $this->ontology = $ontology;
75
76        if (!empty($allowedWidgets)) {
77            $this->allowedWidgets = $allowedWidgets;
78        }
79    }
80
81    public function addEncoder(JsonLdTripleEncoderInterface $encoder): void
82    {
83        $this->encoders[] = $encoder;
84    }
85
86    public function encode(
87        array $dataToEncode,
88        core_kernel_classes_Triple $triple,
89        core_kernel_classes_Property $property = null,
90        core_kernel_classes_Resource $widget = null
91    ): array {
92        $property = $this->getProperty($triple);
93        $widget = $this->getWidget($triple);
94
95        if ($widget === null) {
96            return $dataToEncode;
97        }
98
99        foreach ($this->encoders as $encoder) {
100            if ($encoder->isWidgetSupported($widget->getUri())) {
101                $dataToEncode = $encoder->encode(
102                    $dataToEncode,
103                    $triple,
104                    $property,
105                    $widget
106                );
107            }
108        }
109
110        return $dataToEncode;
111    }
112
113    public function isWidgetSupported(string $widgetUri): bool
114    {
115        return in_array($widgetUri, $this->allowedWidgets, true);
116    }
117
118    private function getProperty(core_kernel_classes_Triple $triple): core_kernel_classes_Property
119    {
120        if (!isset($this->propertyCache[$triple->predicate])) {
121            $this->propertyCache[$triple->predicate] = $this->ontology->getProperty($triple->predicate);
122        }
123
124        return $this->propertyCache[$triple->predicate];
125    }
126
127    private function getWidget(core_kernel_classes_Triple $triple): ?core_kernel_classes_Resource
128    {
129        if (!array_key_exists($triple->predicate, $this->widgetCache)) {
130            $this->widgetCache[$triple->predicate] = $this->getProperty($triple)->getWidget();
131        }
132
133        return $this->widgetCache[$triple->predicate];
134    }
135}