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

Revision 7588, 5.0 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 * TODO
39 * Voorbeeld request:
40 * http://localhost:8983/solr/select?q=*:*&stats=true&stats.field=price&stats.field=popularity
41 *      &stats.twopass=true&rows=0&indent=true&stats.facet=inStock&f.price.stats.facet=price
42 *      &f.price.stats.facet=popularity
43 */
44
45/**
46 * Stats component field class
47 *
48 * @package Solarium
49 * @subpackage Query
50 */
51class Solarium_Query_Select_Component_Stats_Field extends Solarium_Configurable
52{
53
54    /**
55     * Field facets (for stats)
56     *
57     * @var array
58     */
59    protected $_facets = array();
60
61    /**
62     * Initialize options
63     *
64     * Several options need some extra checks or setup work, for these options
65     * the setters are called.
66     *
67     * @return void
68     */
69    protected function _init()
70    {
71        foreach ($this->_options AS $name => $value) {
72            switch ($name) {
73                case 'facet':
74                    $this->setFacets($value);
75                    break;
76            }
77        }
78    }
79
80    /**
81     * Get key value
82     *
83     * @return string
84     */
85    public function getKey()
86    {
87        return $this->getOption('key');
88    }
89
90    /**
91     * Set key value
92     *
93     * @param string $value
94     * @return Solarium_Query_Select_Component_Stats Provides fluent interface
95     */
96    public function setKey($value)
97    {
98        return $this->_setOption('key', $value);
99    }
100
101    /**
102     * Specify a facet to return in the resultset
103     *
104     * @param string $facet
105     * @return Solarium_Query_Select_Component_Stats Provides fluent interface
106     */
107    public function addFacet($facet)
108    {
109       $this->_facets[$facet] = true;
110       return $this;
111    }
112
113    /**
114     * Specify multiple facets to return in the resultset
115     *
116     * @param string|array $facets can be an array or string with comma
117     * separated facetnames
118     *
119     * @return Solarium_Query_Select_Component_Stats Provides fluent interface
120     */
121    public function addFacets($facets)
122    {
123        if (is_string($facets)) {
124            $facets = explode(',', $facets);
125            $facets = array_map('trim', $facets);
126        }
127
128        foreach ($facets AS $facet) {
129            $this->addFacet($facet);
130        }
131
132        return $this;
133    }
134
135    /**
136     * Remove a facet from the facet list
137     *
138     * @param string $facet
139     * @return Solarium_Query_Select_Component_Stats Provides fluent interface
140     */
141    public function removeFacet($facet)
142    {
143        if (isset($this->_facets[$facet])) {
144           unset($this->_facets[$facet]);
145        }
146
147        return $this;
148    }
149
150    /**
151     * Remove all facets from the facet list.
152     *
153     * @return Solarium_Query_Select_Component_Stats Provides fluent interface
154     */
155    public function clearFacets()
156    {
157        $this->_facets = array();
158        return $this;
159    }
160
161    /**
162     * Get the list of facets
163     *
164     * @return array
165     */
166    public function getFacets()
167    {
168        return array_keys($this->_facets);
169    }
170
171    /**
172     * Set multiple facets
173     *
174     * This overwrites any existing facets
175     *
176     * @param array $facets
177     * @return Solarium_Query_Select_Component_Stats Provides fluent interface
178     */
179    public function setFacets($facets)
180    {
181        $this->clearFacets();
182        $this->addFacets($facets);
183
184        return $this;
185    }
186
187}
Note: See TracBrowser for help on using the repository browser.