source: sandbox/expresso-solr/expressoMail1_2/solrclient/library/Solarium/Query/Select/Component/Stats.php @ 7588

Revision 7588, 8.7 KB checked in by adir, 11 years ago (diff)

Ticket #000 - Adicionando a integracao de buscas com Solr na base a ser isnerida na comunidade

Line 
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 * Stats component
41 *
42 * @link http://wiki.apache.org/solr/StatsComponent
43 *
44 * @package Solarium
45 * @subpackage Query
46 */
47class Solarium_Query_Select_Component_Stats extends Solarium_Query_Select_Component
48{
49
50    /**
51     * Component type
52     *
53     * @var string
54     */
55    protected $_type = Solarium_Query_Select::COMPONENT_STATS;
56
57    /**
58     * Stats facets for all fields
59     *
60     * @var array
61     */
62    protected $_facets = array();
63
64    /**
65     * Fields
66     *
67     * @var array
68     */
69    protected $_fields = array();
70
71    /**
72     * Initialize options
73     *
74     * Several options need some extra checks or setup work, for these options
75     * the setters are called.
76     *
77     * @return void
78     */
79    protected function _init()
80    {
81        foreach ($this->_options AS $name => $value) {
82            switch ($name) {
83                case 'field':
84                    $this->setFields($value);
85                    break;
86                case 'facet':
87                    $this->setFacets($value);
88                    break;
89            }
90        }
91    }
92
93    /**
94     * Create a field instance
95     *
96     * If you supply a string as the first arguments ($options) it will be used as the key for the field
97     * and it will be added to this query component.
98     * If you supply an options array/object that contains a key the field will also be added to the component.
99     *
100     * When no key is supplied the field cannot be added, in that case you will need to add it manually
101     * after setting the key, by using the addField method.
102     *
103     * @param mixed $options
104     * @return Solarium_Query_Select_Component_Stats_Field
105     */
106    public function createField($options = null)
107    {
108        if (is_string($options)) {
109            $fq = new Solarium_Query_Select_Component_Stats_Field;
110            $fq->setKey($options);
111        } else {
112            $fq = new Solarium_Query_Select_Component_Stats_Field($options);
113        }
114
115        if ($fq->getKey() !== null) {
116            $this->addField($fq);
117        }
118
119        return $fq;
120    }
121
122    /**
123     * Add a field
124     *
125     * Supports a field instance or a config array, in that case a new
126     * field instance wil be created based on the options.
127     *
128     * @param Solarium_Query_Select_Component_Stats_Field|array $field
129     * @return Solarium_Query_Select_Component_Stats Provides fluent interface
130     */
131    public function addField($field)
132    {
133        if (is_array($field)) {
134            $field = new Solarium_Query_Select_Component_Stats_Field($field);
135        }
136
137        $key = $field->getKey();
138
139        if (0 === strlen($key)) {
140            throw new Solarium_Exception('A field must have a key value');
141        }
142
143        //double add calls for the same field are ignored, but non-unique keys cause an exception
144        //@todo add trigger_error with a notice for double add calls?
145        if (array_key_exists($key, $this->_fields) && $this->_fields[$key] !== $field) {
146            throw new Solarium_Exception('A field must have a unique key value');
147        } else {
148            $this->_fields[$key] = $field;
149        }
150
151        return $this;
152    }
153
154    /**
155     * Add multiple fields
156     *
157     * @param array $fields
158     * @return Solarium_Query_Select_Component_Stats Provides fluent interface
159     */
160    public function addFields(array $fields)
161    {
162        foreach ($fields AS $key => $field) {
163
164            // in case of a config array: add key to config
165            if (is_array($field) && !isset($field['key'])) {
166                $field['key'] = $key;
167            }
168
169            $this->addField($field);
170        }
171
172        return $this;
173    }
174
175    /**
176     * Get a field
177     *
178     * @param string $key
179     * @return string
180     */
181    public function getField($key)
182    {
183        if (isset($this->_fields[$key])) {
184            return $this->_fields[$key];
185        } else {
186            return null;
187        }
188    }
189
190    /**
191     * Get all fields
192     *
193     * @return array
194     */
195    public function getFields()
196    {
197        return $this->_fields;
198    }
199
200    /**
201     * Remove a single field
202     *
203     * You can remove a field by passing it's key, or by passing the field instance
204     *
205     * @param string|Solarium_Query_Select_Component_Stats_Field $field
206     * @return Solarium_Query_Select_Component_Stats Provides fluent interface
207     */
208    public function removeField($field)
209    {
210        if (is_object($field)) {
211            $field = $field->getKey();
212        }
213
214        if (isset($this->_fields[$field])) {
215            unset($this->_fields[$field]);
216        }
217
218        return $this;
219    }
220
221    /**
222     * Remove all fields
223     *
224     * @return Solarium_Query_Select_Component_Stats Provides fluent interface
225     */
226    public function clearFields()
227    {
228        $this->_fields = array();
229        return $this;
230    }
231
232    /**
233     * Set multiple fields
234     *
235     * This overwrites any existing fields
236     *
237     * @param array $fields
238     */
239    public function setFields($fields)
240    {
241        $this->clearFields();
242        $this->addFields($fields);
243    }
244
245    /**
246     * Specify a facet to return in the resultset
247     *
248     * @param string $facet
249     * @return Solarium_Query_Select_Component_Stats Provides fluent interface
250     */
251    public function addFacet($facet)
252    {
253       $this->_facets[$facet] = true;
254       return $this;
255    }
256
257    /**
258     * Specify multiple facets to return in the resultset
259     *
260     * @param string|array $facets can be an array or string with comma
261     * separated facetnames
262     *
263     * @return Solarium_Query_Select_Component_Stats Provides fluent interface
264     */
265    public function addFacets($facets)
266    {
267        if (is_string($facets)) {
268            $facets = explode(',', $facets);
269            $facets = array_map('trim', $facets);
270        }
271
272        foreach ($facets AS $facet) {
273            $this->addFacet($facet);
274        }
275
276        return $this;
277    }
278
279    /**
280     * Remove a facet from the facet list
281     *
282     * @param string $facet
283     * @return Solarium_Query_Select_Component_Stats Provides fluent interface
284     */
285    public function removeFacet($facet)
286    {
287        if (isset($this->_facets[$facet])) {
288           unset($this->_facets[$facet]);
289        }
290
291        return $this;
292    }
293
294    /**
295     * Remove all facets from the facet list.
296     *
297     * @return Solarium_Query_Select_Component_Stats Provides fluent interface
298     */
299    public function clearFacets()
300    {
301        $this->_facets = array();
302        return $this;
303    }
304
305    /**
306     * Get the list of facets
307     *
308     * @return array
309     */
310    public function getFacets()
311    {
312        return array_keys($this->_facets);
313    }
314
315    /**
316     * Set multiple facets
317     *
318     * This overwrites any existing facets
319     *
320     * @param array $facets
321     * @return Solarium_Query_Select_Component_Stats Provides fluent interface
322     */
323    public function setFacets($facets)
324    {
325        $this->clearFacets();
326        $this->addFacets($facets);
327
328        return $this;
329    }
330
331
332}
Note: See TracBrowser for help on using the repository browser.