View source
<?php
namespace Drupal\wunderground_weather;
use Drupal\Core\Config\ConfigFactory;
use GuzzleHttp\Client;
class WundergroundWeatherManager {
private $configFactory;
private $httpClient;
public function __construct(ConfigFactory $config_factory, Client $http_client) {
$this->configFactory = $config_factory;
$this->httpClient = $http_client;
}
public function getSettings() {
return $this->configFactory
->get('wunderground_weather.settings');
}
public function requestData($options) {
$url = 'http://api.wunderground.com';
foreach ($options as $argument) {
$url .= '/' . $argument;
}
$url .= '.json';
$data = $this->httpClient
->request('GET', $url);
return json_decode($data
->getBody());
}
public static function getIconUrl($set, $icon) {
$path = drupal_get_path('module', 'wunderground_weather');
return $path . '/icons/' . $set . '/' . $icon . '.gif';
}
public function getIconSetSample($set) {
$all_icons = $this
->getIconNames();
$sample = [
$all_icons[8],
$all_icons[9],
$all_icons[15],
$all_icons[18],
$all_icons[20],
];
$sample_icons = '';
foreach ($sample as $name) {
$image_variables = [
'#theme' => 'image',
'#uri' => $this
->getIconUrl($set, $name),
];
$sample_icons .= render($image_variables);
}
return $sample_icons;
}
public function getIconNames() {
return [
'chanceflurries',
'chancerain',
'chancerain',
'chancesleet',
'chancesleet',
'chancesnow',
'chancetstorms',
'chancetstorms',
'clear',
'cloudy',
'flurries',
'fog',
'hazy',
'mostlycloudy',
'mostlysunny',
'partlycloudy',
'partlysunny',
'sleet',
'rain',
'sleet',
'snow',
'sunny',
'tstorms',
'tstorms',
'unknown',
'cloudy',
'partlycloudy',
];
}
public function windSpeedToBeaufort($speed, $unit) {
$speed = $unit == 'kph' ? $speed : $speed * 1.6;
switch (TRUE) {
case $speed < 1:
$bft = 0;
break;
case $speed < 5.6:
$bft = 1;
break;
case $speed < 12:
$bft = 2;
break;
case $speed < 20:
$bft = 3;
break;
case $speed < 29:
$bft = 4;
break;
case $speed < 39:
$bft = 5;
break;
case $speed < 50:
$bft = 6;
break;
case $speed < 62:
$bft = 7;
break;
case $speed < 75:
$bft = 8;
break;
case $speed < 89:
$bft = 9;
break;
case $speed < 103:
$bft = 10;
break;
case $speed < 118:
$bft = 11;
break;
case $speed >= 118:
$bft = 12;
break;
default:
$bft = 100;
break;
}
return $bft;
}
public function getLanguages() {
return [
'AF' => 'Afrikaans',
'AL' => 'Albanian',
'AR' => 'Arabic',
'HY' => 'Armenian',
'AZ' => 'Azerbaijani',
'EU' => 'Basque',
'BY' => 'Belarusian',
'BU' => 'Bulgarian',
'LI' => 'British English',
'MY' => 'Burmese',
'CA' => 'Catalan',
'CN' => 'Chinese - Simplified',
'TW' => 'Chinese - Traditional',
'CR' => 'Croatian',
'CZ' => 'Czech',
'DK' => 'Danish',
'DV' => 'Dhivehi',
'NL' => 'Dutch',
'EN' => 'English',
'EO' => 'Esperanto',
'ET' => 'Estonian',
'FA' => 'Farsi',
'FI' => 'Finnish',
'FR' => 'French',
'FC' => 'French Canadian',
'GZ' => 'Galician',
'DL' => 'German',
'KA' => 'Georgian',
'GR' => 'Greek',
'GU' => 'Gujarati',
'HT' => 'Haitian Creole',
'IL' => 'Hebrew',
'HI' => 'Hindi',
'HU' => 'Hungarian',
'IS' => 'Icelandic',
'IO' => 'Ido',
'ID' => 'Indonesian',
'IR' => 'Irish Gaelic',
'IT' => 'Italian',
'JP' => 'Japanese',
'JW' => 'Javanese',
'KM' => 'Khmer',
'KR' => 'Korean',
'KU' => 'Kurdish',
'LA' => 'Latin',
'LV' => 'Latvian',
'LT' => 'Lithuanian',
'ND' => 'Low German',
'MK' => 'Macedonian',
'MT' => 'Maltese',
'GM' => 'Mandinka',
'MI' => 'Maori',
'MR' => 'Marathi',
'MN' => 'Mongolian',
'NO' => 'Norwegian',
'OC' => 'Occitan',
'PS' => 'Pashto',
'GN' => 'Plautdietsch',
'PL' => 'Polish',
'BR' => 'Portuguese',
'PA' => 'Punjabi',
'RO' => 'Romanian',
'RU' => 'Russian',
'SR' => 'Serbian',
'SK' => 'Slovak',
'SL' => 'Slovenian',
'SP' => 'Spanish',
'SI' => 'Swahili',
'SW' => 'Swedish',
'CH' => 'Swiss',
'TL' => 'Tagalog',
'TT' => 'Tatarish',
'TH' => 'Thai',
'TR' => 'Turkish',
'TK' => 'Turkmen',
'UA' => 'Ukrainian',
'UZ' => 'Uzbek',
'VU' => 'Vietnamese',
'CY' => 'Welsh',
'SN' => 'Wolof',
'JI' => 'Yiddish - transliterated',
'YI' => 'Yiddish - unicode',
];
}
}