public function ParserService::getForecastsFromDatabase in Weather 8
Same name and namespace in other branches
- 2.0.x src/Service/ParserService.php \Drupal\weather\Service\ParserService::getForecastsFromDatabase()
Try to fetch forecasts from the database.
Parameters
string $geoid: GeoID of the place for which the weather is desired.
string $utc_offset: UTC offset of place in minutes.
int $days: Return weather for specified number of days (0 = all available days).
bool $detailed: Return detailed forecasts or just one forecast per day.
int $time: Timestamp for which the weather should be returned. This is only needed to enable proper testing of the module.
Return value
array Weather array with forecast information.
1 call to ParserService::getForecastsFromDatabase()
- ParserService::getWeather in src/
Service/ ParserService.php - Returns a weather object for the specified GeoID.
File
- src/
Service/ ParserService.php, line 308
Class
- ParserService
- ParserService service.
Namespace
Drupal\weather\ServiceCode
public function getForecastsFromDatabase($geoid, $utc_offset, $days, $detailed, $time) {
// Fetch the first forecast. This must be done separately, because
// sometimes the first forecast is already on the next day (this can
// happen e.g. late in the evenings). Otherwise, the calculation of
// 'tomorrow' would fail.
$current_local_time = gmdate('Y-m-d H:i:s', $time + $utc_offset * 60);
$first_forecast = $this->weatherForecastStorage
->getQuery()
->condition('geoid', $geoid)
->condition('time_to', $current_local_time, '>=')
->sort('time_from', 'ASC')
->range(0, 1)
->execute();
// If there are no forecasts available, return an empty array.
if (!$first_forecast) {
return [];
}
$first_forecast = $this->weatherForecastStorage
->load(reset($first_forecast));
$weather = $this
->createWeatherArray([
$first_forecast,
]);
// Calculate tomorrow based on result.
$first_forecast_day = explode('-', key($weather));
$tomorrow_local_time = gmdate('Y-m-d H:i:s', gmmktime(0, 0, 0, $first_forecast_day[1], $first_forecast_day[2] + 1, $first_forecast_day[0]));
$forecasts_until_local_time = gmdate('Y-m-d 23:59:59', gmmktime(23, 59, 59, $first_forecast_day[1], $first_forecast_day[2] + $days - 1, $first_forecast_day[0]));
$query = $this->weatherForecastStorage
->getQuery()
->condition('geoid', $geoid);
if ($detailed) {
$query
->condition('time_to', $current_local_time, '>=');
// Fetch all available forecasts.
if ($days > 0) {
$query
->condition('time_from', $forecasts_until_local_time, '<=');
}
$query
->sort('time_from', 'ASC');
$forecasts = $query
->execute();
$forecasts = $this->weatherForecastStorage
->loadMultiple($forecasts);
$weather = $this
->createWeatherArray($forecasts);
}
elseif ($days > 1 || $days == 0) {
$query
->condition('time_from', $tomorrow_local_time, '>=');
$query
->condition('period', '2');
if ($days > 1) {
$query
->condition('time_from', $forecasts_until_local_time, '<=');
}
$query
->sort('time_from', 'ASC');
$forecasts = $query
->execute();
$forecasts = $this->weatherForecastStorage
->loadMultiple($forecasts);
$weather = array_merge($weather, $this
->createWeatherArray($forecasts));
}
return $weather;
}