You are here

protected function ParserService::createWeatherArray in Weather 8

Same name and namespace in other branches
  1. 2.0.x src/Service/ParserService.php \Drupal\weather\Service\ParserService::createWeatherArray()

Create a weather array with the forecast data from database.

Parameters

array $forecasts: Raw forecast data from database.

Return value

array Weather array with forecast information.

1 call to ParserService::createWeatherArray()
ParserService::getForecastsFromDatabase in src/Service/ParserService.php
Try to fetch forecasts from the database.

File

src/Service/ParserService.php, line 381

Class

ParserService
ParserService service.

Namespace

Drupal\weather\Service

Code

protected function createWeatherArray(array $forecasts) {
  $weather = [];

  // Cycle through all forecasts and set up a hierarchical array structure.
  foreach ($forecasts as $forecast) {
    list($day_from, $time_from) = explode(' ', $forecast->time_from->value);
    $time_range = substr($time_from, 0, 5);
    list($day_to, $time_to) = explode(' ', $forecast->time_to->value);
    $time_range .= '-' . substr($time_to, 0, 5);

    // @TODO: Refactor condition below and $day_to var if it will not needed.
    if ($day_to === $day_from) {
      unset($day_to);
    }
    $weather[$day_from][$time_range] = [
      'period' => $forecast->period->value,
      'symbol' => $forecast->symbol->value,
      'precipitation' => $forecast->precipitation->value,
      'wind_direction' => $forecast->wind_direction->value,
      'wind_speed' => $forecast->wind_speed->value,
      'temperature' => $forecast->temperature->value,
      'pressure' => $forecast->pressure->value,
    ];
  }
  return $weather;
}