View source
<?php
namespace Drupal\Tests\weather\Functional;
use Drupal\weather\Entity\WeatherPlaceInterface;
use ReflectionClass;
trait WeatherCommonTestTrait {
public function weatherFillWeatherSchema($source = 'geonames_2911298.xml') {
$xml_source = dirname((new ReflectionClass(static::class))
->getFileName()) . '/data/' . $source;
$xml_source_stream_content = stream_get_contents(fopen($xml_source, 'rb'));
$element_data = new \SimpleXMLElement($xml_source_stream_content);
$geobase = $element_data->location->location
->attributes()->{'geobase'};
$geobase_id = $element_data->location->location
->attributes()->{'geobaseid'};
$latitude = $element_data->location->location
->attributes()->{'latitude'};
$longitude = $element_data->location->location
->attributes()->{'longitude'};
$name = $element_data->location->name;
$country = $element_data->location->country;
$last_update = $element_data->meta->lastupdate;
$next_update = $element_data->meta->nextupdate;
if ($source !== 'geonames_2911298.xml') {
$link = 'Kiev/Kyiv';
}
else {
$link = 'Hamburg/Hamburg';
}
$forecasts = $element_data->forecast->tabular;
$forecats_list = [];
foreach ($forecasts->time as $key => $forecast) {
$from = $forecast
->attributes()->{'from'};
$to = $forecast
->attributes()->{'to'};
$symbol = (string) $forecast->symbol
->attributes()->{'var'};
if (strlen($symbol) > 3) {
$symbol = substr($symbol, 3, 3);
}
$period = $forecast
->attributes()->{'period'};
$precipitation = $forecast->precipitation
->attributes()->{'value'};
$wind_direction = $forecast->windDirection
->attributes()->{'deg'};
$wind_speed = $forecast->windSpeed
->attributes()->{'mps'};
$temperature = $forecast->temperature
->attributes()->{'value'};
$pressure = $forecast->pressure
->attributes()->{'value'};
$forecats_list[] = [
'time_from' => (string) $from,
'time_to' => (string) $to,
'period' => (string) $period,
'symbol' => $symbol,
'precipitation' => (string) $precipitation,
'wind_direction' => (string) $wind_direction,
'wind_speed' => (string) $wind_speed,
'temperature' => (string) $temperature,
'pressure' => (string) $pressure,
];
}
$connection = \Drupal::database();
$query = $connection
->insert('weather_place');
$query
->fields([
'geoid' => (string) $geobase . '_' . (string) $geobase_id,
'latitude' => (string) $latitude,
'longitude' => (string) $longitude,
'name' => (string) $name,
'country' => (string) $country,
'link' => $link,
'status' => WeatherPlaceInterface::STATUS_ORIGINAL,
]);
$query
->execute();
foreach ($forecats_list as $forecast_item) {
$query = $connection
->insert('weather_forecast');
$query
->fields([
'id' => 0,
'geoid' => (string) $geobase . '_' . (string) $geobase_id,
'time_from' => $forecast_item['time_from'],
'time_to' => $forecast_item['time_to'],
'period' => $forecast_item['period'],
'symbol' => $forecast_item['symbol'],
'precipitation' => $forecast_item['precipitation'],
'wind_direction' => $forecast_item['wind_direction'],
'wind_speed' => $forecast_item['wind_speed'],
'temperature' => $forecast_item['temperature'],
'pressure' => $forecast_item['pressure'],
]);
$query
->execute();
}
$query = $connection
->insert('weather_forecast_information');
$query
->fields([
'geoid' => (string) $geobase . '_' . (string) $geobase_id,
'last_update' => (string) $last_update,
'next_update' => (string) $next_update,
'next_download_attempt' => (string) $next_update,
'utc_offset' => '180',
]);
$query
->execute();
}
public function weatherCreateWeatherArray(array $forecasts) {
$weather = [];
if (count($forecasts) === 1) {
$forecasts = reset($forecasts);
}
foreach ($forecasts as $forecast) {
list($day_from, $time_from) = explode(' ', $forecast->time_from);
$time_range = substr($time_from, 0, 5);
list($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;
}
public function weatherGetInformationAboutGeoid($wanted_geoid) {
$connection = \Drupal::database();
$query = $connection
->select('weather_place', 'wfi');
$query
->condition('wfi.geoid', $wanted_geoid, '=');
$query
->fields('wfi', [
'geoid',
'link',
'country',
]);
$query
->range(0, 50);
$result = $query
->execute();
return $result
->fetchAssoc();
}
public function weatherGetLinkForGeoId($geoid, $destination, $number = 1) {
$info = $this
->weatherGetInformationAboutGeoid($geoid);
$country = str_replace(' ', '_', $info['country']);
if (substr($country, -1) == '.') {
$country[strlen($country) - 1] = '_';
}
$link = $country . '/' . $info['link'];
switch ($destination) {
case 'system-wide':
$link = 'weather/' . $link . '/' . $number;
break;
case 'default':
$link = 'weather/' . $link;
break;
case 'user':
$link = 'weather/' . $link . '/u';
break;
case 'yr':
$link = rawurlencode($link);
$link = str_replace('%2F', '/', $link);
$link = 'http://www.yr.no/place/' . $link . '/forecast.xml';
break;
case 'yr.no':
$link = 'http://www.yr.no/place/' . $link . '/';
break;
case 'autocomplete':
break;
}
return $link;
}
}