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 Document |
---|
37 | */ |
---|
38 | |
---|
39 | /** |
---|
40 | * Read/Write Solr document |
---|
41 | * |
---|
42 | * This document type is used for update queries. It has all of the features of |
---|
43 | * the readonly document and it also allows for updating or adding fields and |
---|
44 | * boosts. |
---|
45 | * |
---|
46 | * While it is possible to use this document type for a select, alter it and use |
---|
47 | * it in an update query (effectively the 'edit' that Solr doesn't have) this |
---|
48 | * is not recommended. Most Solr indexes have fields that are indexed and not |
---|
49 | * stored. You will loose that data because it is impossible to retrieve it from |
---|
50 | * Solr. Always update from the original data source. |
---|
51 | * |
---|
52 | * @package Solarium |
---|
53 | * @subpackage Document |
---|
54 | */ |
---|
55 | class Solarium_Document_ReadWrite extends Solarium_Document_ReadOnly |
---|
56 | { |
---|
57 | |
---|
58 | /** |
---|
59 | * Document boost value |
---|
60 | * |
---|
61 | * @var float |
---|
62 | */ |
---|
63 | protected $_boost = null; |
---|
64 | |
---|
65 | /** |
---|
66 | * Field boosts |
---|
67 | * |
---|
68 | * Using fieldname as the key and the boost as the value |
---|
69 | * |
---|
70 | * @var array |
---|
71 | */ |
---|
72 | protected $_fieldBoosts; |
---|
73 | |
---|
74 | /** |
---|
75 | * Constructor |
---|
76 | * |
---|
77 | * @param array $fields |
---|
78 | * @param array $boosts |
---|
79 | */ |
---|
80 | public function __construct($fields = array(), $boosts = array()) |
---|
81 | { |
---|
82 | $this->_fields = $fields; |
---|
83 | $this->_fieldBoosts = $boosts; |
---|
84 | } |
---|
85 | |
---|
86 | /** |
---|
87 | * Add a field value |
---|
88 | * |
---|
89 | * If a field already has a value it will be converted |
---|
90 | * to a multivalue field. |
---|
91 | * |
---|
92 | * @param string $key |
---|
93 | * @param mixed $value |
---|
94 | * @param float $boost |
---|
95 | * @return Solarium_Document_ReadWrite Provides fluent interface |
---|
96 | */ |
---|
97 | public function addField($key, $value, $boost = null) |
---|
98 | { |
---|
99 | if (!isset($this->_fields[$key])) { |
---|
100 | $this->setField($key, $value, $boost); |
---|
101 | } else { |
---|
102 | // convert single value to array if needed |
---|
103 | if (!is_array($this->_fields[$key])) { |
---|
104 | $this->_fields[$key] = array($this->_fields[$key]); |
---|
105 | } |
---|
106 | |
---|
107 | $this->_fields[$key][] = $value; |
---|
108 | $this->setFieldBoost($key, $boost); |
---|
109 | } |
---|
110 | |
---|
111 | return $this; |
---|
112 | } |
---|
113 | |
---|
114 | /** |
---|
115 | * Set a field value |
---|
116 | * |
---|
117 | * If a field already has a value it will be overwritten. You cannot use |
---|
118 | * this method for a multivalue field. |
---|
119 | * If you supply NULL as the value the field will be removed |
---|
120 | * |
---|
121 | * @param string $key |
---|
122 | * @param mixed $value |
---|
123 | * @param float $boost |
---|
124 | * @return Solarium_Document_ReadWrite Provides fluent interface |
---|
125 | */ |
---|
126 | public function setField($key, $value, $boost = null) |
---|
127 | { |
---|
128 | if ($value === null) { |
---|
129 | $this->removeField($key); |
---|
130 | } else { |
---|
131 | $this->_fields[$key] = $value; |
---|
132 | $this->setFieldBoost($key, $boost); |
---|
133 | } |
---|
134 | |
---|
135 | return $this; |
---|
136 | } |
---|
137 | |
---|
138 | /** |
---|
139 | * Remove a field |
---|
140 | * |
---|
141 | * @param string $key |
---|
142 | * @return Solarium_Document_ReadWrite Provides fluent interface |
---|
143 | */ |
---|
144 | public function removeField($key) |
---|
145 | { |
---|
146 | if (isset($this->_fields[$key])) { |
---|
147 | unset($this->_fields[$key]); |
---|
148 | } |
---|
149 | |
---|
150 | if (isset($this->_fieldBoosts[$key])) { |
---|
151 | unset($this->_fieldBoosts[$key]); |
---|
152 | } |
---|
153 | |
---|
154 | return $this; |
---|
155 | } |
---|
156 | |
---|
157 | /** |
---|
158 | * Get the boost value for a field |
---|
159 | * |
---|
160 | * @param string $key |
---|
161 | * @return float |
---|
162 | */ |
---|
163 | public function getFieldBoost($key) |
---|
164 | { |
---|
165 | if (isset($this->_fieldBoosts[$key])) { |
---|
166 | return $this->_fieldBoosts[$key]; |
---|
167 | } else { |
---|
168 | return null; |
---|
169 | } |
---|
170 | } |
---|
171 | |
---|
172 | /** |
---|
173 | * Set the boost value for a field |
---|
174 | * |
---|
175 | * @param string $key |
---|
176 | * @param float $boost |
---|
177 | * @return Solarium_Document_ReadWrite Provides fluent interface |
---|
178 | */ |
---|
179 | public function setFieldBoost($key, $boost) |
---|
180 | { |
---|
181 | $this->_fieldBoosts[$key] = $boost; |
---|
182 | return $this; |
---|
183 | } |
---|
184 | |
---|
185 | /** |
---|
186 | * Set the document boost value |
---|
187 | * |
---|
188 | * @param float $boost |
---|
189 | * @return Solarium_Document_ReadWrite Provides fluent interface |
---|
190 | */ |
---|
191 | public function setBoost($boost) |
---|
192 | { |
---|
193 | $this->_boost = $boost; |
---|
194 | return $this; |
---|
195 | } |
---|
196 | |
---|
197 | /** |
---|
198 | * Get the document boost value |
---|
199 | * |
---|
200 | * @return float |
---|
201 | */ |
---|
202 | public function getBoost() |
---|
203 | { |
---|
204 | return $this->_boost; |
---|
205 | } |
---|
206 | |
---|
207 | /** |
---|
208 | * Clear all fields |
---|
209 | * |
---|
210 | * @return Solarium_Document_ReadWrite Provides fluent interface |
---|
211 | **/ |
---|
212 | public function clear() |
---|
213 | { |
---|
214 | $this->_fields = array(); |
---|
215 | $this->_fieldBoosts = array(); |
---|
216 | |
---|
217 | return $this; |
---|
218 | } |
---|
219 | |
---|
220 | /** |
---|
221 | * Set field value |
---|
222 | * |
---|
223 | * Magic method for setting fields as properties of this document |
---|
224 | * object, by field name. |
---|
225 | * |
---|
226 | * If you supply NULL as the value the field will be removed |
---|
227 | * If you supply an array a multivalue field will be created. |
---|
228 | * In all cases any existing (multi)value will be overwritten. |
---|
229 | * |
---|
230 | * @param string $name |
---|
231 | * @param string|null $value |
---|
232 | * @return void |
---|
233 | */ |
---|
234 | public function __set($name, $value) |
---|
235 | { |
---|
236 | $this->setField($name, $value); |
---|
237 | } |
---|
238 | |
---|
239 | /** |
---|
240 | * Unset field value |
---|
241 | * |
---|
242 | * Magic method for removing fields by unsetting object properties |
---|
243 | * |
---|
244 | * @param string $name |
---|
245 | * @return void |
---|
246 | */ |
---|
247 | public function __unset($name) |
---|
248 | { |
---|
249 | $this->removeField($name); |
---|
250 | } |
---|
251 | |
---|
252 | } |
---|