Passed
Branch master (4b538d)
by Osma
10:57
created

ConceptSearchParameters::getAdditionalFields()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 2
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 0
dl 0
loc 2
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()
69
    {
70
        $term = $this->request->getQueryParamRaw('q') ? $this->request->getQueryParamRaw('q') : $this->request->getQueryParamRaw('query');
71
        if (!$term && $this->rest)
72
            $term = $this->request->getQueryParamRaw('label');
73
        $term = trim($term); // surrounding whitespace is not considered significant
74
        if ($this->rest) {
75
            return $term;
76
        }
77
        return strpos($term, "*") === false ? $term . "*" : $term; // default to prefix search
78
    }
79
80
    public function getContentLang()
81
    {
82
        return $this->request->getContentLang();
83
    }
84
85
    public function getSearchLang()
86
    {
87
        if ($this->rest) {
88
            return $this->request->getQueryParam('lang');
89
        }
90
        return $this->request->getQueryParam('anylang') ? '' : $this->getContentLang();
91
    }
92
93
    private function getDefaultTypeLimit()
94
    {
95
        $type = array('skos:Concept');
96
        if ($this->request->getVocab()) {
97
            $conf = $this->request->getVocab()->getConfig();
98
            $type[] = $conf->getArrayClassURI();
99
            $type[] = $conf->getGroupClassURI();
100
        }
101
        return array_filter($type, 'strlen');
102
    }
103
104
    public function getTypeLimit()
105
    {
106
        $type = $this->request->getQueryParam('type') !== '' ? $this->request->getQueryParam('type') : null;
107
        if ($type && strpos($type, ' ')) {
108
            $type = explode(' ', $type);
109
        } else if ($type && strpos($type, '+')) {
110
            $type = explode('+', $type);
111
        } else if ($type && !is_array($type)) {
112
            // if only one type param given place it into an array regardless
113
            $type = array($type);
114
        }
115
        if ($type === null) {
116
            return $this->getDefaultTypeLimit();
117
        }
118
        return $type;
119
    }
120
121
    private function getQueryParam($name) {
122
        return $this->request->getQueryParam($name) !== '' ? $this->request->getQueryParam($name) : null;
123
    }
124
125
    private function getQueryParamArray($name) {
126
        return $this->request->getQueryParam($name) ? explode(' ', urldecode($this->request->getQueryParam($name))) : [];
127
    }
128
129
    public function getGroupLimit()
130
    {
131
        return $this->getQueryParam('group');
132
    }
133
134
    public function getParentLimit()
135
    {
136
        return $this->getQueryParam('parent');
137
    }
138
139
    public function getSchemeLimit()
140
    {
141
        return $this->getQueryParamArray('scheme');
142
    }
143
144
    public function getOffset()
145
    {
146
        return ($this->request->getQueryParam('offset') && is_numeric($this->request->getQueryParam('offset')) && $this->request->getQueryParam('offset') >= 0) ? $this->request->getQueryParam('offset') : 0;
147
    }
148
149
    public function getSearchLimit()
150
    {
151
        if ($this->rest) {
152
            return ($this->request->getQueryParam('maxhits')) ? $this->request->getQueryParam('maxhits') : 0;
153
        }
154
        return $this->config->getSearchResultsSize();
155
    }
156
157
    public function getUnique() {
158
        return $this->unique;
159
    }
160
161
    public function setUnique($unique) {
162
        $this->unique = $unique;
163
    }
164
165
    public function getAdditionalFields() {
166
        return $this->getQueryParamArray('fields');
167
    }
168
169
    public function getHidden() {
170
        return $this->hidden;
171
    }
172
173
    public function setHidden($hidden) {
174
        $this->hidden = $hidden;
175
    }
176
177
    public function getRest() {
178
        return $this->rest;
179
    }
180
}
181