[7576] | 1 | <?php |
---|
| 2 | /** |
---|
| 3 | * Copyright 2011 Gasol Wu. PIXNET Digital Media Corporation. |
---|
| 4 | * All rights reserved. |
---|
| 5 | * |
---|
| 6 | * Redistribution and use in source and binary forms, with or without |
---|
| 7 | * modification, are permitted provided that the following conditions are met: |
---|
| 8 | * |
---|
| 9 | * 1. Redistributions of source code must retain the above copyright notice, |
---|
| 10 | * this list of conditions and the following disclaimer. |
---|
| 11 | * |
---|
| 12 | * 2. Redistributions in binary form must reproduce the above copyright notice, |
---|
| 13 | * this listof conditions and the following disclaimer in the documentation |
---|
| 14 | * and/or other materials provided with the distribution. |
---|
| 15 | * |
---|
| 16 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDER AND CONTRIBUTORS "AS IS" |
---|
| 17 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
---|
| 18 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
---|
| 19 | * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE |
---|
| 20 | * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR |
---|
| 21 | * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF |
---|
| 22 | * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS |
---|
| 23 | * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN |
---|
| 24 | * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) |
---|
| 25 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE |
---|
| 26 | * POSSIBILITY OF SUCH DAMAGE. |
---|
| 27 | * |
---|
| 28 | * The views and conclusions contained in the software and documentation are |
---|
| 29 | * those of the authors and should not be interpreted as representing official |
---|
| 30 | * policies, either expressed or implied, of the copyright holder. |
---|
| 31 | * |
---|
| 32 | * @copyright Copyright 2011 Gasol Wu <gasol.wu@gmail.com> |
---|
| 33 | * @license http://github.com/basdenooijer/solarium/raw/master/COPYING |
---|
| 34 | * @link http://www.solarium-project.org/ |
---|
| 35 | * |
---|
| 36 | * @package Solarium |
---|
| 37 | * @subpackage Client |
---|
| 38 | */ |
---|
| 39 | |
---|
| 40 | /** |
---|
| 41 | * Parse Suggester response data |
---|
| 42 | * |
---|
| 43 | * @package Solarium |
---|
| 44 | * @subpackage Client |
---|
| 45 | */ |
---|
| 46 | class Solarium_Client_ResponseParser_Suggester extends Solarium_Client_ResponseParser |
---|
| 47 | { |
---|
| 48 | |
---|
| 49 | /** |
---|
| 50 | * Get result data for the response |
---|
| 51 | * |
---|
| 52 | * @param Solarium_Result_Terms $result |
---|
| 53 | * @return array |
---|
| 54 | */ |
---|
| 55 | public function parse($result) |
---|
| 56 | { |
---|
| 57 | $data = $result->getData(); |
---|
| 58 | $query = $result->getQuery(); |
---|
| 59 | |
---|
| 60 | $status = null; |
---|
| 61 | $queryTime = null; |
---|
| 62 | if (isset($data['responseHeader'])) { |
---|
| 63 | $status = $data['responseHeader']['status']; |
---|
| 64 | $queryTime = $data['responseHeader']['QTime']; |
---|
| 65 | } |
---|
| 66 | |
---|
| 67 | $suggestions = array(); |
---|
| 68 | $collation = null; |
---|
| 69 | |
---|
| 70 | if (isset($data['spellcheck']['suggestions']) && is_array($data['spellcheck']['suggestions'])) { |
---|
| 71 | $suggestResults = $data['spellcheck']['suggestions']; |
---|
| 72 | $termClass = $query->getOption('termclass'); |
---|
| 73 | for ($i = 0; $i < count($suggestResults); $i += 2) { |
---|
| 74 | $term = $suggestResults[$i]; |
---|
| 75 | $termData = $suggestResults[$i+1]; |
---|
| 76 | |
---|
| 77 | if ($term == 'collation'&& $i == count($suggestResults)-2) { |
---|
| 78 | $collation = $termData; |
---|
| 79 | } else { |
---|
| 80 | $suggestions[$term] = new $termClass( |
---|
| 81 | $termData['numFound'], |
---|
| 82 | $termData['startOffset'], |
---|
| 83 | $termData['endOffset'], |
---|
| 84 | $termData['suggestion'] |
---|
| 85 | ); |
---|
| 86 | } |
---|
| 87 | } |
---|
| 88 | } |
---|
| 89 | |
---|
| 90 | return array( |
---|
| 91 | 'status' => $status, |
---|
| 92 | 'queryTime' => $queryTime, |
---|
| 93 | 'results' => $suggestions, |
---|
| 94 | 'collation' => $collation, |
---|
| 95 | ); |
---|
| 96 | } |
---|
| 97 | |
---|
| 98 | } |
---|