public function Parser::parseResponse in Google Search Appliance 8
Parses response from GSA.
Parameters
string $xml: Response body.
bool $useCached: FALSE to re-parse and bypass static-cache.
Return value
\Drupal\google_appliance\SearchResults\ResultSet Search response.
Overrides ParserInterface::parseResponse
File
- src/
Service/ Parser.php, line 47
Class
- Parser
- Defines a class for parsing GSA responses.
Namespace
Drupal\google_appliance\ServiceCode
public function parseResponse($xml, $useCached = TRUE) {
if (!$this->response || FALSE === $useCached) {
$response = new ResultSet();
// Look for xml parse errors.
libxml_use_internal_errors(TRUE);
/** @var \SimpleXMLElement $payload */
$payload = simplexml_load_string($xml);
if ($payload === FALSE) {
// XML parse error(s)
$errors = [];
foreach (libxml_get_errors() as $error) {
$response
->addError($error->message);
}
}
else {
// Store metrics for stat reporting.
$response
->setLastResult((int) $payload->RES['EN']);
$this
->parseResultCount($payload, $response);
// Search returned zero results.
if (!$response
->getTotal()) {
$response
->addError('No results', ResultSet::ERROR_NO_RESULTS);
$this->response = $response;
return $this->response;
}
// Spelling suggestions.
if (isset($payload->Spelling->Suggestion)) {
$spelling_suggestion = (string) $payload->Spelling->Suggestion;
$response
->addSpellingSuggestion(Xss::filter($spelling_suggestion, [
'b',
'i',
]));
}
$this
->parseOneboxResults($payload, $response);
foreach ($payload
->xpath('//GM') as $km) {
$keymatch = new KeyMatch((string) $km->GD, (string) $km->GL);
$response
->addKeyMatch($keymatch);
}
// If there are any synonyms returned by the appliance,
// put them in the results as a new array.
// @see http://code.google.com/apis/searchappliance/documentation/50/xml_reference.html#tag_onesynonym
foreach ($payload
->xpath('//OneSynonym') as $syn_element) {
$synonym = new Synonym((string) $syn_element, (string) $syn_element['q']);
$response
->addSynonym($synonym);
}
$this
->parseResultEntries($payload, $response);
$this->moduleHandler
->alter('google_appliance_results', $results, $payload);
$this->response = $response;
}
}
return $this->response;
}