1 | <?php |
---|
2 | /** |
---|
3 | * Copyright 2011 Bas de Nooijer. All rights reserved. |
---|
4 | * |
---|
5 | * Redistribution and use in source and binary forms, with or without |
---|
6 | * modification, are permitted provided that the following conditions are met: |
---|
7 | * |
---|
8 | * 1. Redistributions of source code must retain the above copyright notice, |
---|
9 | * this list of conditions and the following disclaimer. |
---|
10 | * |
---|
11 | * 2. Redistributions in binary form must reproduce the above copyright notice, |
---|
12 | * this listof conditions and the following disclaimer in the documentation |
---|
13 | * and/or other materials provided with the distribution. |
---|
14 | * |
---|
15 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDER AND CONTRIBUTORS "AS IS" |
---|
16 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
---|
17 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
---|
18 | * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE |
---|
19 | * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR |
---|
20 | * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF |
---|
21 | * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS |
---|
22 | * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN |
---|
23 | * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) |
---|
24 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE |
---|
25 | * POSSIBILITY OF SUCH DAMAGE. |
---|
26 | * |
---|
27 | * The views and conclusions contained in the software and documentation are |
---|
28 | * those of the authors and should not be interpreted as representing official |
---|
29 | * policies, either expressed or implied, of the copyright holder. |
---|
30 | * |
---|
31 | * @copyright Copyright 2011 Bas de Nooijer <solarium@raspberry.nl> |
---|
32 | * @license http://github.com/basdenooijer/solarium/raw/master/COPYING |
---|
33 | * @link http://www.solarium-project.org/ |
---|
34 | * |
---|
35 | * @package Solarium |
---|
36 | * @subpackage Query |
---|
37 | */ |
---|
38 | |
---|
39 | /** |
---|
40 | * Filterquery |
---|
41 | * |
---|
42 | * @link http://wiki.apache.org/solr/CommonQueryParameters#fq |
---|
43 | * |
---|
44 | * @package Solarium |
---|
45 | * @subpackage Query |
---|
46 | */ |
---|
47 | class Solarium_Query_Select_FilterQuery extends Solarium_Configurable |
---|
48 | { |
---|
49 | |
---|
50 | /** |
---|
51 | * Tags for this filterquery |
---|
52 | * |
---|
53 | * @var array |
---|
54 | */ |
---|
55 | protected $_tags = array(); |
---|
56 | |
---|
57 | /** |
---|
58 | * Query |
---|
59 | * |
---|
60 | * @var string |
---|
61 | */ |
---|
62 | protected $_query; |
---|
63 | |
---|
64 | /** |
---|
65 | * Initialize options |
---|
66 | * |
---|
67 | * @return void |
---|
68 | */ |
---|
69 | protected function _init() |
---|
70 | { |
---|
71 | foreach ($this->_options AS $name => $value) { |
---|
72 | switch ($name) { |
---|
73 | case 'tag': |
---|
74 | if(!is_array($value)) $value = array($value); |
---|
75 | $this->addTags($value); |
---|
76 | break; |
---|
77 | case 'key': |
---|
78 | $this->setKey($value); |
---|
79 | break; |
---|
80 | case 'query': |
---|
81 | $this->setQuery($value); |
---|
82 | break; |
---|
83 | } |
---|
84 | } |
---|
85 | } |
---|
86 | |
---|
87 | /** |
---|
88 | * Get key value |
---|
89 | * |
---|
90 | * @return string |
---|
91 | */ |
---|
92 | public function getKey() |
---|
93 | { |
---|
94 | return $this->getOption('key'); |
---|
95 | } |
---|
96 | |
---|
97 | /** |
---|
98 | * Set key value |
---|
99 | * |
---|
100 | * @param string $value |
---|
101 | * @return Solarium_Query_Select_FilterQuery Provides fluent interface |
---|
102 | */ |
---|
103 | public function setKey($value) |
---|
104 | { |
---|
105 | return $this->_setOption('key', $value); |
---|
106 | } |
---|
107 | |
---|
108 | /** |
---|
109 | * Set the query string |
---|
110 | * |
---|
111 | * This overwrites the current value |
---|
112 | * |
---|
113 | * @param string $query |
---|
114 | * @param array $bind Bind values for placeholders in the query string |
---|
115 | * @return Solarium_Query Provides fluent interface |
---|
116 | */ |
---|
117 | public function setQuery($query, $bind = null) |
---|
118 | { |
---|
119 | if (!is_null($bind)) { |
---|
120 | $helper = new Solarium_Query_Helper; |
---|
121 | $query = $helper->assemble($query, $bind); |
---|
122 | } |
---|
123 | |
---|
124 | $this->_query = trim($query); |
---|
125 | return $this; |
---|
126 | } |
---|
127 | |
---|
128 | /** |
---|
129 | * Get the query string |
---|
130 | * |
---|
131 | * @return string |
---|
132 | */ |
---|
133 | public function getQuery() |
---|
134 | { |
---|
135 | return $this->_query; |
---|
136 | } |
---|
137 | |
---|
138 | /** |
---|
139 | * Add a tag |
---|
140 | * |
---|
141 | * @param string $tag |
---|
142 | * @return Solarium_Query_Select_FilterQuery Provides fluent interface |
---|
143 | */ |
---|
144 | public function addTag($tag) |
---|
145 | { |
---|
146 | $this->_tags[$tag] = true; |
---|
147 | return $this; |
---|
148 | } |
---|
149 | |
---|
150 | /** |
---|
151 | * Add tags |
---|
152 | * |
---|
153 | * @param array $tags |
---|
154 | * @return Solarium_Query_Select_FilterQuery Provides fluent interface |
---|
155 | */ |
---|
156 | public function addTags($tags) |
---|
157 | { |
---|
158 | foreach ($tags AS $tag) { |
---|
159 | $this->addTag($tag); |
---|
160 | } |
---|
161 | return $this; |
---|
162 | } |
---|
163 | |
---|
164 | /** |
---|
165 | * Get all tagss |
---|
166 | * |
---|
167 | * @return array |
---|
168 | */ |
---|
169 | public function getTags() |
---|
170 | { |
---|
171 | return array_keys($this->_tags); |
---|
172 | } |
---|
173 | |
---|
174 | /** |
---|
175 | * Remove a tag |
---|
176 | * |
---|
177 | * @param string $tag |
---|
178 | * @return Solarium_Query_Select_FilterQuery Provides fluent interface |
---|
179 | */ |
---|
180 | public function removeTag($tag) |
---|
181 | { |
---|
182 | if (isset($this->_tags[$tag])) { |
---|
183 | unset($this->_tags[$tag]); |
---|
184 | } |
---|
185 | |
---|
186 | return $this; |
---|
187 | } |
---|
188 | |
---|
189 | /** |
---|
190 | * Remove all tags |
---|
191 | * |
---|
192 | * @return Solarium_Query_Select_FilterQuery Provides fluent interface |
---|
193 | */ |
---|
194 | public function clearTags() |
---|
195 | { |
---|
196 | $this->_tags = array(); |
---|
197 | return $this; |
---|
198 | } |
---|
199 | |
---|
200 | /** |
---|
201 | * Set multiple tags |
---|
202 | * |
---|
203 | * This overwrites any existing tags |
---|
204 | * |
---|
205 | * @param array $filterQueries |
---|
206 | * @return Solarium_Query_Select_FilterQuery Provides fluent interface |
---|
207 | */ |
---|
208 | public function setTags($filterQueries) |
---|
209 | { |
---|
210 | $this->clearTags(); |
---|
211 | return $this->addTags($filterQueries); |
---|
212 | } |
---|
213 | |
---|
214 | } |
---|