Passed
Push — master ( 847fb8...bb8f79 )
by Timo
24:45
created

QueryStringContainer   A

Complexity

Total Complexity 11

Size/Duplication

Total Lines 131
Duplicated Lines 0 %

Test Coverage

Coverage 92.86%

Importance

Changes 0
Metric Value
wmc 11
dl 0
loc 131
ccs 26
cts 28
cp 0.9286
rs 10
c 0
b 0
f 0

10 Methods

Rating   Name   Duplication   Size   Complexity  
A getKeywords() 0 3 1
A useRawQueryString() 0 3 1
A getQueryString() 0 7 2
A setQueryString() 0 3 1
A setKeywords() 0 4 1
A buildQueryString() 0 4 1
A __construct() 0 3 1
A getKeywordsRaw() 0 3 1
A __toString() 0 3 1
A getKeywordsCleaned() 0 3 1
1
<?php
2
namespace ApacheSolrForTypo3\Solr\Domain\Search\Query\Helper;
3
4
/***************************************************************
5
 *  Copyright notice
6
 *
7
 *  (c) 2010-2017 dkd Internet Service GmbH <[email protected]>
8
 *  All rights reserved
9
 *
10
 *  This script is part of the TYPO3 project. The TYPO3 project is
11
 *  free software; you can redistribute it and/or modify
12
 *  it under the terms of the GNU General Public License as published by
13
 *  the Free Software Foundation; either version 3 of the License, or
14
 *  (at your option) any later version.
15
 *
16
 *  The GNU General Public License can be found at
17
 *  http://www.gnu.org/copyleft/gpl.html.
18
 *
19
 *  This script is distributed in the hope that it will be useful,
20
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
21
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
22
 *  GNU General Public License for more details.
23
 *
24
 *  This copyright notice MUST APPEAR in all copies of the script!
25
 ***************************************************************/
26
27
class QueryStringContainer {
28
29
    /**
30
     * @var string
31
     */
32
    protected $keywords;
33
34
    /**
35
     * @var string
36
     */
37
    protected $keywordsRaw;
38
39
    /**
40
     * @var
41
     */
42
    protected $queryString;
43
44
    /**
45
     * @var bool
46
     */
47
    private $rawQueryString = false;
48
49
    /**
50
     * QueryStrings constructor.
51
     * @param string $keywords
52
     */
53 179
    public function __construct($keywords)
54
    {
55 179
        $this->setKeywords($keywords);
56 179
    }
57
58
    /**
59
     * Builds the query string which is then used for Solr's q parameters
60
     *
61
     * @return string Solr query string
62
     */
63 53
    public function getQueryString()
64
    {
65 53
        if (!$this->rawQueryString) {
66 44
            $this->buildQueryString();
67
        }
68
69 53
        return $this->queryString;
70
    }
71
72
    /**
73
     * Sets the query string without any escaping.
74
     *
75
     * Be cautious with this function!
76
     * TODO remove this method as it basically just sets the q parameter / keywords
77
     *
78
     * @param string $queryString The raw query string.
79
     */
80 10
    public function setQueryString($queryString)
81
    {
82 10
        $this->queryString = $queryString;
83 10
    }
84
85
    /**
86
     * Creates the string that is later used as the q parameter in the solr query
87
     *
88
     * @return void
89
     */
90 44
    protected function buildQueryString()
91
    {
92
        // very simple for now
93 44
        $this->queryString = $this->keywords;
94 44
    }
95
96
    /**
97
     * Sets whether a raw query sting should be used, that is, whether the query
98
     * string should be escaped or not.
99
     *
100
     * @param bool $useRawQueryString TRUE to use raw queries (like Lucene Query Language) or FALSE for regular, escaped queries
101
     */
102 10
    public function useRawQueryString($useRawQueryString)
103
    {
104 10
        $this->rawQueryString = (boolean)$useRawQueryString;
105 10
    }
106
107
    /**
108
     * Get the query keywords, keywords are escaped.
109
     *
110
     * @return string query keywords
111
     */
112 39
    public function getKeywords()
113
    {
114 39
        return $this->keywords;
115
    }
116
117
    /**
118
     * Sets the query keywords, escapes them as needed for Solr/Lucene.
119
     *
120
     * @param string $keywords user search terms/keywords
121
     */
122 179
    public function setKeywords($keywords)
123
    {
124 179
        $this->keywords = EscapeService::escape($keywords);
125 179
        $this->keywordsRaw = $keywords;
126 179
    }
127
128
    /**
129
     * Gets the cleaned keywords so that it can be used in templates f.e.
130
     *
131
     * @return string The cleaned keywords.
132
     */
133 28
    public function getKeywordsCleaned()
134
    {
135 28
        return EscapeService::clean($this->keywordsRaw);
136
    }
137
138
    /**
139
     * Gets the raw, unescaped, unencoded keywords.
140
     *
141
     * USE WITH CAUTION!
142
     *
143
     * @return string raw keywords
144
     */
145
    public function getKeywordsRaw()
146
    {
147
        return $this->keywordsRaw;
148
    }
149
150
    /**
151
     * returns a string representation of the query
152
     *
153
     * @return string the string representation of the query
154
     */
155 2
    public function __toString()
156
    {
157 2
        return $this->getQueryString();
158
    }
159
}