Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
85.00% covered (warning)
85.00%
17 / 20
50.00% covered (danger)
50.00%
1 / 2
CRAP
0.00% covered (danger)
0.00%
0 / 1
PropertyIndexReferenceFactory
85.00% covered (warning)
85.00%
17 / 20
50.00% covered (danger)
50.00%
1 / 2
12.49
0.00% covered (danger)
0.00%
0 / 1
 create
72.73% covered (warning)
72.73%
8 / 11
0.00% covered (danger)
0.00%
0 / 1
4.32
 createRaw
100.00% covered (success)
100.00%
9 / 9
100.00% covered (success)
100.00%
1 / 1
8
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) 2022 (original work) Open Assessment Technologies SA;
19 *
20 * @author Gabriel Felipe Soares <gabriel.felipe.soares@taotesting.com>
21 */
22
23declare(strict_types=1);
24
25namespace oat\tao\model\search\index\DocumentBuilder;
26
27use core_kernel_classes_Property;
28use oat\generis\model\WidgetRdf;
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_Htmlarea;
35use tao_helpers_form_elements_Radiobox;
36use tao_helpers_form_elements_Readonly;
37use tao_helpers_form_elements_Textarea;
38use tao_helpers_form_elements_Textbox;
39use tao_helpers_Uri;
40
41class PropertyIndexReferenceFactory
42{
43    public const RAW_SUFFIX = '_raw';
44    private const ALLOWED_DYNAMIC_TYPES = [
45        tao_helpers_form_elements_Textbox::WIDGET_ID,
46        tao_helpers_form_elements_Textarea::WIDGET_ID,
47        tao_helpers_form_elements_Htmlarea::WIDGET_ID,
48        tao_helpers_form_elements_Checkbox::WIDGET_ID,
49        tao_helpers_form_elements_Combobox::WIDGET_ID,
50        tao_helpers_form_elements_Radiobox::WIDGET_ID,
51        tao_helpers_form_elements_Calendar::WIDGET_ID,
52        tao_helpers_form_elements_Readonly::WIDGET_ID,
53        SearchTextBox::WIDGET_ID,
54        SearchDropdown::WIDGET_ID,
55    ];
56
57    public function create(core_kernel_classes_Property $property): ?string
58    {
59        $propertyType = $property->getOnePropertyValue($property->getProperty(WidgetRdf::PROPERTY_WIDGET));
60
61        if (null === $propertyType) {
62            return null;
63        }
64
65        $propertyTypeUri = $propertyType->getUri();
66
67        if (!in_array($propertyTypeUri, self::ALLOWED_DYNAMIC_TYPES, true)) {
68            return null;
69        }
70
71        $propertyTypeArray = explode('#', $propertyTypeUri, 2);
72        $propertyTypeId = end($propertyTypeArray);
73
74        if (false === $propertyTypeId) {
75            return null;
76        }
77
78        return $propertyTypeId . '_' . tao_helpers_Uri::encode($property->getUri());
79    }
80
81    public function createRaw(core_kernel_classes_Property $property): ?string
82    {
83        $reference = $this->create($property);
84
85        if (
86            strpos($reference, 'RadioBox') === 0 ||
87            strpos($reference, 'ComboBox') === 0 ||
88            strpos($reference, 'CheckBox') === 0 ||
89            strpos($reference, 'HTMLArea') === 0 ||
90            strpos($reference, 'SearchTextBox') === 0 ||
91            strpos($reference, 'SearchDropdown') === 0
92        ) {
93            return $reference ? ($reference . self::RAW_SUFFIX) : null;
94        }
95
96        return null;
97    }
98}