[7576] | 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 Debug result from the data |
---|
| 41 | * |
---|
| 42 | * @package Solarium |
---|
| 43 | * @subpackage Client |
---|
| 44 | */ |
---|
| 45 | class Solarium_Client_ResponseParser_Select_Component_Debug |
---|
| 46 | { |
---|
| 47 | |
---|
| 48 | /** |
---|
| 49 | * Parse result data into result objects |
---|
| 50 | * |
---|
| 51 | * @param Solarium_Query_Select $query |
---|
| 52 | * @param Solarium_Query_Select_Component_Debug $component |
---|
| 53 | * @param array $data |
---|
| 54 | * @return Solarium_Result_Select_Debug|null |
---|
| 55 | */ |
---|
| 56 | public function parse($query, $component, $data) |
---|
| 57 | { |
---|
| 58 | $result = null; |
---|
| 59 | |
---|
| 60 | if (isset($data['debug'])) { |
---|
| 61 | $debug = $data['debug']; |
---|
| 62 | |
---|
| 63 | // get basic values from data |
---|
| 64 | $queryString = (isset($debug['querystring'])) ? $debug['querystring'] : ''; |
---|
| 65 | $parsedQuery = (isset($debug['parsedquery'])) ? $debug['parsedquery'] : ''; |
---|
| 66 | $queryParser = (isset($debug['QParser'])) ? $debug['QParser'] : ''; |
---|
| 67 | $otherQuery = (isset($debug['otherQuery'])) ? $debug['otherQuery'] : ''; |
---|
| 68 | |
---|
| 69 | // parse explain data |
---|
| 70 | if (isset($debug['explain']) && is_array($debug['explain'])) { |
---|
| 71 | $explain = $this->_parseDocumentSet($debug['explain']); |
---|
| 72 | } else { |
---|
| 73 | $explain = new Solarium_Result_Select_Debug_DocumentSet(array()); |
---|
| 74 | } |
---|
| 75 | |
---|
| 76 | // parse explainOther data |
---|
| 77 | if (isset($debug['explainOther']) && is_array($debug['explainOther'])) { |
---|
| 78 | $explainOther = $this->_parseDocumentSet($debug['explainOther']); |
---|
| 79 | } else { |
---|
| 80 | $explainOther = new Solarium_Result_Select_Debug_DocumentSet(array()); |
---|
| 81 | } |
---|
| 82 | |
---|
| 83 | // parse timing data |
---|
| 84 | $timing = null; |
---|
| 85 | if (isset($debug['timing']) && is_array($debug['timing'])) { |
---|
| 86 | $time = null; |
---|
| 87 | $timingPhases = array(); |
---|
| 88 | foreach ($debug['timing'] as $key => $timingData) { |
---|
| 89 | switch($key) { |
---|
| 90 | case 'time': |
---|
| 91 | $time = $timingData; |
---|
| 92 | break; |
---|
| 93 | default: |
---|
| 94 | $timingPhases[$key] = $this->_parseTimingPhase($key, $timingData); |
---|
| 95 | } |
---|
| 96 | } |
---|
| 97 | $timing = new Solarium_Result_Select_Debug_Timing($time, $timingPhases); |
---|
| 98 | } |
---|
| 99 | |
---|
| 100 | // create result object |
---|
| 101 | $result = new Solarium_Result_Select_Debug( |
---|
| 102 | $queryString, |
---|
| 103 | $parsedQuery, |
---|
| 104 | $queryParser, |
---|
| 105 | $otherQuery, |
---|
| 106 | $explain, |
---|
| 107 | $explainOther, |
---|
| 108 | $timing |
---|
| 109 | ); |
---|
| 110 | } |
---|
| 111 | |
---|
| 112 | return $result; |
---|
| 113 | } |
---|
| 114 | |
---|
| 115 | /** |
---|
| 116 | * Parse data into a documentset |
---|
| 117 | * |
---|
| 118 | * Used for explain and explainOther |
---|
| 119 | * |
---|
| 120 | * @param array $data |
---|
| 121 | * @return Solarium_Result_Select_Debug_DocumentSet |
---|
| 122 | */ |
---|
| 123 | protected function _parseDocumentSet($data) |
---|
| 124 | { |
---|
| 125 | $docs = array(); |
---|
| 126 | foreach ($data as $key => $documentData) { |
---|
| 127 | |
---|
| 128 | $details = array(); |
---|
| 129 | if (isset($documentData['details']) && is_array($documentData['details'])) { |
---|
| 130 | foreach ($documentData['details'] as $detailData) { |
---|
| 131 | $details[] = new Solarium_Result_Select_Debug_Detail( |
---|
| 132 | $detailData['match'], |
---|
| 133 | $detailData['value'], |
---|
| 134 | $detailData['description'] |
---|
| 135 | ); |
---|
| 136 | } |
---|
| 137 | } |
---|
| 138 | |
---|
| 139 | $docs[$key] = new Solarium_Result_Select_Debug_Document( |
---|
| 140 | $key, |
---|
| 141 | $documentData['match'], |
---|
| 142 | $documentData['value'], |
---|
| 143 | $documentData['description'], |
---|
| 144 | $details |
---|
| 145 | ); |
---|
| 146 | } |
---|
| 147 | |
---|
| 148 | return new Solarium_Result_Select_Debug_DocumentSet($docs); |
---|
| 149 | } |
---|
| 150 | |
---|
| 151 | /** |
---|
| 152 | * Parse raw timing phase data into a result class |
---|
| 153 | * |
---|
| 154 | * @param string $name |
---|
| 155 | * @param array $data |
---|
| 156 | * @return Solarium_Result_Select_Debug_TimingPhase |
---|
| 157 | */ |
---|
| 158 | protected function _parseTimingPhase($name, $data) |
---|
| 159 | { |
---|
| 160 | $time = 0.0; |
---|
| 161 | $classes = array(); |
---|
| 162 | foreach ($data as $key => $timingData) { |
---|
| 163 | switch($key) { |
---|
| 164 | case 'time': |
---|
| 165 | $time = $timingData; |
---|
| 166 | break; |
---|
| 167 | default: |
---|
| 168 | $classes[$key] = $timingData['time']; |
---|
| 169 | } |
---|
| 170 | } |
---|
| 171 | |
---|
| 172 | return new Solarium_Result_Select_Debug_TimingPhase($name, $time, $classes); |
---|
| 173 | } |
---|
| 174 | |
---|
| 175 | } |
---|