You are here

public function WeatherCommonTestTrait::weatherCreateWeatherArray in Weather 2.0.x

Same name and namespace in other branches
  1. 8 tests/src/Functional/WeatherCommonTestTrait.php \Drupal\Tests\weather\Functional\WeatherCommonTestTrait::weatherCreateWeatherArray()

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 WeatherCommonTestTrait::weatherCreateWeatherArray()
ParserTest::weatherGetForecastsFromDatabase in tests/src/Functional/ParserTest.php
Try to fetch forecasts from the database.

File

tests/src/Functional/WeatherCommonTestTrait.php, line 127

Class

WeatherCommonTestTrait
Provides a helper method for testing Weather module.

Namespace

Drupal\Tests\weather\Functional

Code

public function weatherCreateWeatherArray(array $forecasts) {
  $weather = [];

  // Cycle through all forecasts and set up a hierarchical array structure.
  if (count($forecasts) === 1) {
    $forecasts = reset($forecasts);
  }
  foreach ($forecasts as $forecast) {
    [
      $day_from,
      $time_from,
    ] = explode(' ', $forecast->time_from);
    $time_range = substr($time_from, 0, 5);
    [
      $day_to,
      $time_to,
    ] = explode(' ', $forecast->time_to);
    $time_range .= '-' . substr($time_to, 0, 5);
    $weather[$day_from][$time_range] = [
      'period' => $forecast->period,
      'symbol' => $forecast->symbol,
      'precipitation' => $forecast->precipitation,
      'wind_direction' => $forecast->wind_direction,
      'wind_speed' => $forecast->wind_speed,
      'temperature' => $forecast->temperature,
      'pressure' => $forecast->pressure,
    ];
  }
  return $weather;
}