public function ParserTest::weatherParseForecast in Weather 8
Same name and namespace in other branches
- 2.0.x tests/src/Functional/ParserTest.php \Drupal\Tests\weather\Functional\ParserTest::weatherParseForecast()
Parses an XML forecast supplied by yr.no.
Parameters
string $xml: XML to be parsed.
string $geoid: The GeoID for which the forecasts should be parsed.
Return value
bool TRUE on success, FALSE on failure.
Throws
\Exception
1 call to ParserTest::weatherParseForecast()
- ParserTest::weatherDownloadForecast in tests/
src/ Functional/ ParserTest.php - Downloads a new forecast from yr.no.
File
- tests/
src/ Functional/ ParserTest.php, line 261
Class
- ParserTest
- Tests parsing of XML weather forecasts.
Namespace
Drupal\Tests\weather\FunctionalCode
public function weatherParseForecast($xml, $geoid = '') {
// In case the parsing fails, do not output all error messages.
$use_errors = libxml_use_internal_errors(TRUE);
$fc = simplexml_load_string($xml);
// Restore previous setting of error handling.
libxml_use_internal_errors($use_errors);
if ($fc === FALSE) {
return FALSE;
}
// Update weather_places table with downloaded information, if necessary.
$this
->weatherUpdatePlaces($fc);
// Extract meta information.
// @TODO: Extract GeoID of returned XML data.
// This might differ from the data we have in the database. An example
// was Heraklion (ID 261745), which got the forecast for
// Nomós Irakleíou (ID 261741).
if ($geoid == '') {
$geoid = $fc->location->location['geobase'] . "_" . $fc->location->location['geobaseid'];
}
$meta['geoid'] = $geoid;
$meta['utc_offset'] = (int) $fc->location->timezone['utcoffsetMinutes'];
// Calculate the UTC time.
$utctime = strtotime((string) $fc->meta->lastupdate . ' UTC') - 60 * $meta['utc_offset'];
$meta['last_update'] = gmdate('Y-m-d H:i:s', $utctime);
// Calculate the UTC time.
$utctime = strtotime((string) $fc->meta->nextupdate . ' UTC') - 60 * $meta['utc_offset'];
$meta['next_update'] = gmdate('Y-m-d H:i:s', $utctime);
$meta['next_download_attempt'] = $meta['next_update'];
// Merge meta information for this location.
// This prevents an integrity constraint violation, if multiple
// calls to this function occur at the same time. See bug #1412352.
//
\Drupal::database()
->merge('weather_forecast_information')
->key([
'geoid' => $meta['geoid'],
])
->insertFields($meta)
->updateFields($meta)
->execute();
// Remove all forecasts for this location.
\Drupal::database()
->delete('weather_forecast')
->condition('geoid', $meta['geoid'])
->execute();
// Cycle through all forecasts and write them to the table.
foreach ($fc->forecast->tabular->time as $time) {
$forecast = [];
$forecast['geoid'] = $meta['geoid'];
$forecast['time_from'] = str_replace('T', ' ', (string) $time['from']);
$forecast['time_to'] = str_replace('T', ' ', (string) $time['to']);
$forecast['period'] = (string) $time['period'];
$forecast['symbol'] = (string) $time->symbol['var'];
// Remove moon phases, which are not supported.
// This is in the format "mf/03n.56", where 56 would be the
// percentage of the moon phase.
if (strlen($forecast['symbol']) > 3) {
$forecast['symbol'] = substr($forecast['symbol'], 3, 3);
}
$forecast['precipitation'] = (double) $time->precipitation['value'];
$forecast['wind_direction'] = (int) $time->windDirection['deg'];
$forecast['wind_speed'] = (double) $time->windSpeed['mps'];
$forecast['temperature'] = (int) $time->temperature['value'];
$forecast['pressure'] = (int) $time->pressure['value'];
// Use db_merge to prevent integrity constraint violation, see above.
\Drupal::database()
->merge('weather_forecast')
->key([
'geoid' => $meta['geoid'],
'time_from' => $forecast['time_from'],
])
->insertFields($forecast)
->updateFields($forecast)
->execute();
}
return TRUE;
}