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 Highlighting result from the data |
---|
41 | * |
---|
42 | * @package Solarium |
---|
43 | * @subpackage Client |
---|
44 | */ |
---|
45 | class Solarium_Client_ResponseParser_Select_Component_Spellcheck |
---|
46 | { |
---|
47 | |
---|
48 | /** |
---|
49 | * Parse result data into result objects |
---|
50 | * |
---|
51 | * @param Solarium_Query_Select $query |
---|
52 | * @param Solarium_Query_Select_Component_Spellcheck $spellcheck |
---|
53 | * @param array $data |
---|
54 | * @return Solarium_Result_Select_Spellcheck|null |
---|
55 | */ |
---|
56 | public function parse($query, $spellcheck, $data) |
---|
57 | { |
---|
58 | $results = array(); |
---|
59 | if ( |
---|
60 | isset($data['spellcheck']['suggestions']) && |
---|
61 | is_array($data['spellcheck']['suggestions']) && |
---|
62 | count($data['spellcheck']['suggestions']) > 0 |
---|
63 | ) { |
---|
64 | |
---|
65 | $spellcheckResults = $data['spellcheck']['suggestions']; |
---|
66 | |
---|
67 | $suggestions = array(); |
---|
68 | $correctlySpelled = null; |
---|
69 | $collation = null; |
---|
70 | |
---|
71 | $index = 0; |
---|
72 | while (isset($spellcheckResults[$index]) && isset($spellcheckResults[$index+1])) { |
---|
73 | $key = $spellcheckResults[$index]; |
---|
74 | $value = $spellcheckResults[$index+1]; |
---|
75 | |
---|
76 | switch ($key) { |
---|
77 | case 'correctlySpelled': |
---|
78 | $correctlySpelled = $value; |
---|
79 | break; |
---|
80 | case 'collation': |
---|
81 | $collation = $this->_parseCollation($value); |
---|
82 | break; |
---|
83 | default: |
---|
84 | $suggestions[] = $this->_parseSuggestion($key, $value); |
---|
85 | } |
---|
86 | |
---|
87 | $index +=2; |
---|
88 | } |
---|
89 | |
---|
90 | return new Solarium_Result_Select_Spellcheck($suggestions, $collation, $correctlySpelled); |
---|
91 | } else { |
---|
92 | return null; |
---|
93 | } |
---|
94 | } |
---|
95 | |
---|
96 | /** |
---|
97 | * Parse collation data into a result object |
---|
98 | * |
---|
99 | * @param array $values |
---|
100 | * @return Solarium_Result_Select_Spellcheck_Collation |
---|
101 | */ |
---|
102 | protected function _parseCollation($values) |
---|
103 | { |
---|
104 | if (is_string($values)) { |
---|
105 | |
---|
106 | return new Solarium_Result_Select_Spellcheck_Collation($values, null, array()); |
---|
107 | |
---|
108 | } else { |
---|
109 | |
---|
110 | $query = null; |
---|
111 | $hits = null; |
---|
112 | $correctionResult = null; |
---|
113 | |
---|
114 | $index = 0; |
---|
115 | while (isset($values[$index]) && isset($values[$index+1])) { |
---|
116 | $key = $values[$index]; |
---|
117 | $value = $values[$index+1]; |
---|
118 | |
---|
119 | switch ($key) { |
---|
120 | case 'collationQuery': |
---|
121 | $query = $value; |
---|
122 | break; |
---|
123 | case 'hits': |
---|
124 | $hits = $value; |
---|
125 | break; |
---|
126 | case 'misspellingsAndCorrections': |
---|
127 | $correctionResult = $value; |
---|
128 | break; |
---|
129 | } |
---|
130 | |
---|
131 | $index +=2; |
---|
132 | } |
---|
133 | |
---|
134 | $corrections = array(); |
---|
135 | if ($correctionResult !== null) { |
---|
136 | $index = 0; |
---|
137 | while (isset($correctionResult[$index]) && isset($correctionResult[$index+1])) { |
---|
138 | $input = $correctionResult[$index]; |
---|
139 | $correction = $correctionResult[$index+1]; |
---|
140 | |
---|
141 | $corrections[$input] = $correction; |
---|
142 | $index += 2; |
---|
143 | } |
---|
144 | } |
---|
145 | |
---|
146 | return new Solarium_Result_Select_Spellcheck_Collation($query, $hits, $corrections); |
---|
147 | } |
---|
148 | } |
---|
149 | |
---|
150 | /** |
---|
151 | * Parse suggestion data into a result object |
---|
152 | * |
---|
153 | * @param string $key |
---|
154 | * @param array $value |
---|
155 | * @return Solarium_Result_Select_Spellcheck_Suggestion |
---|
156 | */ |
---|
157 | protected function _parseSuggestion($key, $value) |
---|
158 | { |
---|
159 | $numFound = (isset($value['numFound'])) ? $value['numFound'] : null; |
---|
160 | $startOffset = (isset($value['startOffset'])) ? $value['startOffset'] : null; |
---|
161 | $endOffset = (isset($value['endOffset'])) ? $value['endOffset'] : null; |
---|
162 | $originalFrequency = (isset($value['origFreq'])) ? $value['origFreq'] : null; |
---|
163 | |
---|
164 | if (is_string($value['suggestion'][0])) { |
---|
165 | $word = $value['suggestion'][0]; |
---|
166 | $frequency = null; |
---|
167 | } else { |
---|
168 | $word = $value['suggestion'][0]['word']; |
---|
169 | $frequency = $value['suggestion'][0]['freq']; |
---|
170 | } |
---|
171 | |
---|
172 | return new Solarium_Result_Select_Spellcheck_Suggestion( |
---|
173 | $numFound, $startOffset, $endOffset, $originalFrequency, $word, $frequency |
---|
174 | ); |
---|
175 | } |
---|
176 | } |
---|