ConceptSearchParameters::getParentLimit()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 0
dl 0
loc 3
rs 10
c 0
b 0
f 0
1
<?php
2
3
/**
4
 * ConceptSearchParameters is used for search parameters.
5
 */
6
class ConceptSearchParameters
7
{
8
9
    private $config;
10
    private $request;
11
    private $vocabs;
12
    private $rest;
13
    private $hidden;
14
    private $unique;
15
16
    public function __construct($request, $config, $rest = false)
17
    {
18
        $this->request = $request;
19
        $this->config = $config;
20
        $this->rest = $rest;
21
        $this->hidden = true;
22
        $this->unique = $request->getQueryParamBoolean('unique', false);
23
    }
24
25
    public function getLang()
26
    {
27
        if ($this->rest && $this->request->getQueryParam('labellang')) {
28
            return $this->request->getQueryParam('labellang');
29
        }
30
        return $this->request->getLang();
31
    }
32
33
    public function getVocabs()
34
    {
35
        if ($this->vocabs) {
36
            return $this->vocabs;
37
        }
38
        if ($this->request->getVocab()) {
39
            return array($this->request->getVocab());
40
        }
41
        return array();
42
    }
43
44
    public function getVocabIds()
45
    {
46
        if ($this->rest || $this->request->getQueryParam('vocabs')) {
47
            $vocabs = $this->rest ? $this->request->getQueryParam('vocab') : $this->request->getQueryParam('vocabs');
48
            return ($vocabs !== null && $vocabs !== '') ? explode(' ', $vocabs) : null;
49
        }
50
        $vocabs = $this->getVocabs();
51
        return isset($vocabs[0]) ? array($vocabs[0]->getId()) : null;
52
    }
53
54
    public function setVocabularies($vocabs)
55
    {
56
        $this->vocabs = $vocabs;
57
    }
58
59
    public function getArrayClass()
60
    {
61
        if (sizeof($this->getVocabs()) == 1) { // search within vocabulary
62
            $vocabs = $this->getVocabs();
63
            return $vocabs[0]->getConfig()->getArrayClassURI();
64
        }
65
        return null;
66
    }
67
68
    public function getSearchTerm() : string
69
    {
70
        $term = $this->request->getQueryParamRaw('q') !== null ? $this->request->getQueryParamRaw('q') : $this->request->getQueryParamRaw('query');
71
        if ((!isset($term) || strlen(trim($term)) === 0) && $this->rest)
72
            $term = $this->request->getQueryParamRaw('label');
73
        $term = trim(strval($term)); // surrounding whitespace is not considered significant
74
        $term = Normalizer::normalize( $term, Normalizer::FORM_C ); //Normalize decomposed unicode characters #1184
75
        if ($this->rest) {
76
            return $term;
77
        }
78
        return strpos($term, "*") === false ? $term . "*" : $term; // default to prefix search
79
    }
80
81
    public function getContentLang()
82
    {
83
        return $this->request->getContentLang();
84
    }
85
86
    public function getSearchLang()
87
    {
88
        if ($this->rest) {
89
            return $this->request->getQueryParam('lang');
90
        }
91
        return $this->request->getQueryParam('anylang') ? '' : $this->getContentLang();
92
    }
93
94
    private function getDefaultTypeLimit()
95
    {
96
        $type = array('skos:Concept');
97
        if ($this->request->getVocab()) {
98
            $conf = $this->request->getVocab()->getConfig();
99
            $type[] = strval($conf->getArrayClassURI());
100
            $type[] = strval($conf->getGroupClassURI());
101
        }
102
        return array_filter($type, 'strlen');
103
    }
104
105
    public function getTypeLimit()
106
    {
107
        $type = $this->request->getQueryParam('type') !== '' ? $this->request->getQueryParam('type') : null;
108
        if ($type && strpos($type, ' ')) {
109
            $type = explode(' ', $type);
110
        } else if ($type && strpos($type, '+')) {
111
            $type = explode('+', $type);
112
        } else if ($type && !is_array($type)) {
113
            // if only one type param given place it into an array regardless
114
            $type = array($type);
115
        }
116
        if ($type === null) {
117
            return $this->getDefaultTypeLimit();
118
        }
119
        return $type;
120
    }
121
122
    private function getQueryParam($name) {
123
        return $this->request->getQueryParam($name) !== '' ? $this->request->getQueryParam($name) : null;
124
    }
125
126
    private function getQueryParamArray($name) {
127
        return $this->request->getQueryParam($name) ? explode(' ', urldecode($this->request->getQueryParam($name))) : [];
128
    }
129
130
    public function getGroupLimit()
131
    {
132
        return $this->getQueryParam('group');
133
    }
134
135
    public function getParentLimit()
136
    {
137
        return $this->getQueryParam('parent');
138
    }
139
140
    public function getSchemeLimit()
141
    {
142
        return $this->getQueryParamArray('scheme');
143
    }
144
145
    public function getOffset()
146
    {
147
        return ($this->request->getQueryParam('offset') && is_numeric($this->request->getQueryParam('offset')) && $this->request->getQueryParam('offset') >= 0) ? $this->request->getQueryParam('offset') : 0;
148
    }
149
150
    public function getSearchLimit()
151
    {
152
        if ($this->rest) {
153
            return ($this->request->getQueryParam('maxhits')) ? $this->request->getQueryParam('maxhits') : 0;
154
        }
155
        return $this->config->getSearchResultsSize();
156
    }
157
158
    public function getUnique() {
159
        return $this->unique;
160
    }
161
162
    public function setUnique($unique) {
163
        $this->unique = $unique;
164
    }
165
166
    public function getAdditionalFields() {
167
        return $this->getQueryParamArray('fields');
168
    }
169
170
    public function getHidden() {
171
        return $this->hidden;
172
    }
173
174
    public function setHidden($hidden) {
175
        $this->hidden = $hidden;
176
    }
177
178
    public function getRest() {
179
        return $this->rest;
180
    }
181
}
182