source: sandbox/expresso-solr/expressoMail1_2/inc/solrclient/library/Solarium/Client/ResponseParser/Select/Component/FacetSet.php @ 7576

Revision 7576, 6.4 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 Client
37 */
38
39/**
40 * Parse select component FacetSet result from the data
41 *
42 * @package Solarium
43 * @subpackage Client
44 */
45class Solarium_Client_ResponseParser_Select_Component_FacetSet
46{
47
48    /**
49     * Parse result data into result objects
50     *
51     * @param Solarium_Query_Select $query
52     * @param Solarium_Query_Select_Component_FacetSet $facetSet
53     * @param array $data
54     * @return Solarium_Result_Select_FacetSet
55     */
56    public function parse($query, $facetSet, $data)
57    {
58        $facets = array();
59        foreach ($facetSet->getFacets() AS $key => $facet) {
60            switch ($facet->getType()) {
61                case Solarium_Query_Select_Component_FacetSet::FACET_FIELD:
62                    $result = $this->_facetField($facet, $data);
63                    break;
64                case Solarium_Query_Select_Component_FacetSet::FACET_QUERY:
65                    $result = $this->_facetQuery($facet, $data);
66                    break;
67                case Solarium_Query_Select_Component_FacetSet::FACET_MULTIQUERY:
68                    $result = $this->_facetMultiQuery($facet, $data);
69                    break;
70                case Solarium_Query_Select_Component_FacetSet::FACET_RANGE:
71                    $result = $this->_facetRange($facet, $data);
72                    break;
73                default:
74                    throw new Solarium_Exception('Unknown facet type');
75            }
76
77            if($result !== null) $facets[$key] = $result;
78        }
79
80        return $this->_createFacetSet($facets);
81    }
82
83    /**
84     * Create a facetset result object
85     *
86     * @param array $facets
87     * @return Solarium_Result_Select_FacetSet
88     */
89    protected function _createFacetSet($facets)
90    {
91        return new Solarium_Result_Select_FacetSet($facets);
92    }
93
94    /**
95     * Add a facet result for a field facet
96     *
97     * @param Solarium_Query_Select_Component_Facet_Field $facet
98     * @param array $data
99     * @return void
100     */
101    protected function _facetField($facet, $data)
102    {
103        $key = $facet->getKey();
104        if (isset($data['facet_counts']['facet_fields'][$key])) {
105
106            $values = array_chunk(
107                $data['facet_counts']['facet_fields'][$key],
108                2
109            );
110
111            $facetValues = array();
112            foreach ($values AS $value) {
113                $facetValues[$value[0]] = $value[1];
114            }
115
116            return new Solarium_Result_Select_Facet_Field($facetValues);
117        }
118    }
119
120    /**
121     * Add a facet result for a facet query
122     *
123     * @param Solarium_Query_Select_Component_Facet_Query $facet
124     * @param array $data
125     * @return void
126     */
127    protected function _facetQuery($facet, $data)
128    {
129        $key = $facet->getKey();
130        if (isset($data['facet_counts']['facet_queries'][$key])) {
131
132            $value = $data['facet_counts']['facet_queries'][$key];
133            return new Solarium_Result_Select_Facet_Query($value);
134        }
135    }
136
137    /**
138     * Add a facet result for a multiquery facet
139     *
140     * @param Solarium_Query_Select_Component_Facet_MultiQuery $facet
141     * @param array $data
142     * @return void
143     */
144    protected function _facetMultiQuery($facet, $data)
145    {
146        $values = array();
147        foreach ($facet->getQueries() AS $query) {
148            $key = $query->getKey();
149            if (isset($data['facet_counts']['facet_queries'][$key])) {
150                $count = $data['facet_counts']['facet_queries'][$key];
151                $values[$key] = $count;
152            }
153        }
154
155        if (count($values) > 0) {
156            return new Solarium_Result_Select_Facet_MultiQuery($values);
157        }
158    }
159
160
161    /**
162     * Add a facet result for a range facet
163     *
164     * @param Solarium_Query_Select_Component_Facet_Range $facet
165     * @param array $data
166     * @return void
167     */
168    protected function _facetRange($facet, $data)
169    {
170        $key = $facet->getKey();
171        if (isset($data['facet_counts']['facet_ranges'][$key])) {
172
173            $data = $data['facet_counts']['facet_ranges'][$key];
174            $before = (isset($data['before'])) ? $data['before'] : null;
175            $after = (isset($data['after'])) ? $data['after'] : null;
176            $between = (isset($data['between'])) ? $data['between'] : null;
177
178            $offset = 0;
179            $counts = array();
180            while (isset($data['counts'][$offset]) && isset($data['counts'][$offset+1])) {
181                $counts[$data['counts'][$offset]] = $data['counts'][$offset+1];
182                $offset += 2;
183            }
184
185            return new Solarium_Result_Select_Facet_Range($counts, $before, $after, $between);
186        }
187    }
188
189}
Note: See TracBrowser for help on using the repository browser.