added composer.json and needed libs

This commit is contained in:
Tobias Diekershoff 2015-07-11 20:21:09 +02:00
parent 4f1fb007c5
commit 1f74d409a2
42 changed files with 4413 additions and 0 deletions

View file

@ -0,0 +1,598 @@
<?php
/**
* OpenWeatherMap-PHP-API A php api to parse weather data from http://www.OpenWeatherMap.org .
*
* @license MIT
*
* Please see the LICENSE file distributed with this source code for further
* information regarding copyright and licensing.
*
* Please visit the following links to read about the usage policies and the license of
* OpenWeatherMap before using this class:
*
* @see http://www.OpenWeatherMap.org
* @see http://www.OpenWeatherMap.org/terms
* @see http://openweathermap.org/appid
*/
namespace Cmfcmf;
use Cmfcmf\OpenWeatherMap\AbstractCache;
use Cmfcmf\OpenWeatherMap\CurrentWeather;
use Cmfcmf\OpenWeatherMap\Exception as OWMException;
use Cmfcmf\OpenWeatherMap\Fetcher\CurlFetcher;
use Cmfcmf\OpenWeatherMap\Fetcher\FetcherInterface;
use Cmfcmf\OpenWeatherMap\Fetcher\FileGetContentsFetcher;
use Cmfcmf\OpenWeatherMap\WeatherForecast;
use Cmfcmf\OpenWeatherMap\WeatherHistory;
/**
* Main class for the OpenWeatherMap-PHP-API. Only use this class.
*
* @api
*/
class OpenWeatherMap
{
/**
* @var string $weatherUrl The basic api url to fetch weather data from.
*/
private $weatherUrl = "http://api.openweathermap.org/data/2.5/weather?";
/**
* @var string $url The basic api url to fetch weekly forecast data from.
*/
private $weatherHourlyForecastUrl = "http://api.openweathermap.org/data/2.5/forecast?";
/**
* @var string $url The basic api url to fetch daily forecast data from.
*/
private $weatherDailyForecastUrl = "http://api.openweathermap.org/data/2.5/forecast/daily?";
/**
* @var string $url The basic api url to fetch history weather data from.
*/
private $weatherHistoryUrl = "http://api.openweathermap.org/data/2.5/history/city?";
/**
* The copyright notice. This is no official text, this hint was created regarding to http://openweathermap.org/copyright.
*
* @var string $copyright
*/
const COPYRIGHT = "Weather data from <a href=\"http://www.openweathermap.org\">OpenWeatherMap.org</a>";
/**
* @var \Cmfcmf\OpenWeatherMap\AbstractCache|bool $cacheClass The cache class.
*/
private $cacheClass = false;
/**
* @var int
*/
private $seconds;
/**
* @var FetcherInterface The url fetcher.
*/
private $fetcher;
/**
* Constructs the OpenWeatherMap object.
*
* @param null|FetcherInterface $fetcher The interface to fetch the data from OpenWeatherMap. Defaults to
* CurlFetcher() if cURL is available. Otherwise defaults to
* FileGetContentsFetcher() using 'file_get_contents()'.
* @param bool|string $cacheClass If set to false, caching is disabled. Otherwise this must be a class
* extending AbstractCache. Defaults to false.
* @param int $seconds How long weather data shall be cached. Default 10 minutes.
*
* @throws \Exception If $cache is neither false nor a valid callable extending Cmfcmf\OpenWeatherMap\Util\Cache.
* @api
*/
public function __construct($fetcher = null, $cacheClass = false, $seconds = 600)
{
if ($cacheClass !== false && !($cacheClass instanceof AbstractCache)) {
throw new \Exception("The cache class must implement the FetcherInterface!");
}
if (!is_numeric($seconds)) {
throw new \Exception("\$seconds must be numeric.");
}
if (!isset($fetcher)) {
$fetcher = (function_exists('curl_version')) ? new CurlFetcher() : new FileGetContentsFetcher();
}
if ($seconds == 0) {
$cacheClass = false;
}
$this->cacheClass = $cacheClass;
$this->seconds = $seconds;
$this->fetcher = $fetcher;
}
/**
* Returns the current weather at the place you specified as an object.
*
* @param array|int|string $query The place to get weather information for. For possible values see below.
* @param string $units Can be either 'metric' or 'imperial' (default). This affects almost all units returned.
* @param string $lang The language to use for descriptions, default is 'en'. For possible values see below.
* @param string $appid Your app id, default ''. See http://openweathermap.org/appid for more details.
*
* @throws OpenWeatherMap\Exception If OpenWeatherMap returns an error.
* @throws \InvalidArgumentException If an argument error occurs.
*
* @return CurrentWeather The weather object.
*
* There are three ways to specify the place to get weather information for:
* - Use the city name: $query must be a string containing the city name.
* - Use the city id: $query must be an integer containing the city id.
* - Use the coordinates: $query must be an associative array containing the 'lat' and 'lon' values.
*
* Available languages are (as of 17. July 2013):
* - English - en
* - Russian - ru
* - Italian - it
* - Spanish - sp
* - Ukrainian - ua
* - German - de
* - Portuguese - pt
* - Romanian - ro
* - Polish - pl
* - Finnish - fi
* - Dutch - nl
* - French - fr
* - Bulgarian - bg
* - Swedish - se
* - Chinese Traditional - zh_tw
* - Chinese Simplified - zh_cn
* - Turkish - tr
*
* @api
*/
public function getWeather($query, $units = 'imperial', $lang = 'en', $appid = '')
{
// Disable default error handling of SimpleXML (Do not throw E_WARNINGs).
libxml_use_internal_errors(true);
libxml_clear_errors();
$answer = $this->getRawWeatherData($query, $units, $lang, $appid, 'xml');
try {
$xml = new \SimpleXMLElement($answer);
} catch (\Exception $e) {
// Invalid xml format. This happens in case OpenWeatherMap returns an error.
// OpenWeatherMap always uses json for errors, even if one specifies xml as format.
$error = json_decode($answer, true);
if (isset($error['message'])) {
throw new OWMException($error['message'], $error['cod']);
} else {
throw new OWMException('Unknown fatal error: OpenWeatherMap returned the following json object: ' . $answer);
}
}
return new CurrentWeather($xml, $units);
}
/**
* Returns the current weather at the place you specified as an object.
*
* @param array|int|string $query The place to get weather information for. For possible values see below.
* @param string $units Can be either 'metric' or 'imperial' (default). This affects almost all units returned.
* @param string $lang The language to use for descriptions, default is 'en'. For possible values see below.
* @param string $appid Your app id, default ''. See http://openweathermap.org/appid for more details.
* @param int $days For how much days you want to get a forecast. Default 1, maximum: 16.
*
* @throws OpenWeatherMap\Exception If OpenWeatherMap returns an error.
* @throws \InvalidArgumentException If an argument error occurs.
*
* @return WeatherForecast The WeatherForecast object.
*
* There are three ways to specify the place to get weather information for:
* - Use the city name: $query must be a string containing the city name.
* - Use the city id: $query must be an integer containing the city id.
* - Use the coordinates: $query must be an associative array containing the 'lat' and 'lon' values.
*
* Available languages are (as of 17. July 2013):
* - English - en
* - Russian - ru
* - Italian - it
* - Spanish - sp
* - Ukrainian - ua
* - German - de
* - Portuguese - pt
* - Romanian - ro
* - Polish - pl
* - Finnish - fi
* - Dutch - nl
* - French - fr
* - Bulgarian - bg
* - Swedish - se
* - Chinese Traditional - zh_tw
* - Chinese Simplified - zh_cn
* - Turkish - tr
*
* @api
*/
public function getWeatherForecast($query, $units = 'imperial', $lang = 'en', $appid = '', $days = 1)
{
// Disable default error handling of SimpleXML (Do not throw E_WARNINGs).
libxml_use_internal_errors(true);
libxml_clear_errors();
if ($days <= 5) {
$answer = $this->getRawHourlyForecastData($query, $units, $lang, $appid, 'xml');
} else if ($days <= 16) {
$answer = $this->getRawDailyForecastData($query, $units, $lang, $appid, 'xml', $days);
} else {
throw new \InvalidArgumentException('Error: forecasts are only available for the next 16 days. $days must be lower than 17.');
}
try {
$xml = new \SimpleXMLElement($answer);
} catch (\Exception $e) {
// Invalid xml format. This happens in case OpenWeatherMap returns an error.
// OpenWeatherMap always uses json for errors, even if one specifies xml as format.
$error = json_decode($answer, true);
if (isset($error['message'])) {
throw new OWMException($error['message'], $error['cod']);
} else {
throw new OWMException('Unknown fatal error: OpenWeatherMap returned the following json object: ' . $answer);
}
}
return new WeatherForecast($xml, $units, $days);
}
/**
* Returns the weather history for the place you specified as an object.
*
* @param array|int|string $query The place to get weather information for. For possible values see below.
* @param \DateTime $start
* @param int $endOrCount
* @param string $type
* @param string $units Can be either 'metric' or 'imperial' (default). This affects almost all units returned.
* @param string $lang The language to use for descriptions, default is 'en'. For possible values see below.
* @param string $appid Your app id, default ''. See http://openweathermap.org/appid for more details.
*
* @throws OpenWeatherMap\Exception If OpenWeatherMap returns an error.
* @throws \InvalidArgumentException If an argument error occurs.
*
* @return WeatherHistory The WeatherHistory object.
*
* There are three ways to specify the place to get weather information for:
* - Use the city name: $query must be a string containing the city name.
* - Use the city id: $query must be an integer containing the city id.
* - Use the coordinates: $query must be an associative array containing the 'lat' and 'lon' values.
*
* Available languages are (as of 17. July 2013):
* - English - en
* - Russian - ru
* - Italian - it
* - Spanish - sp
* - Ukrainian - ua
* - German - de
* - Portuguese - pt
* - Romanian - ro
* - Polish - pl
* - Finnish - fi
* - Dutch - nl
* - French - fr
* - Bulgarian - bg
* - Swedish - se
* - Chinese Traditional - zh_tw
* - Chinese Simplified - zh_cn
* - Turkish - tr
*
* @api
*/
public function getWeatherHistory($query, \DateTime $start, $endOrCount = 1, $type = 'hour', $units = 'imperial', $lang = 'en', $appid = '')
{
if (!in_array($type, array('tick', 'hour', 'day'))) {
throw new \InvalidArgumentException('$type must be either "tick", "hour" or "day"');
}
$xml = json_decode($this->getRawWeatherHistory($query, $start, $endOrCount, $type, $units, $lang, $appid), true);
if ($xml['cod'] != 200) {
throw new OWMException($xml['message'], $xml['cod']);
}
return new WeatherHistory($xml, $query);
}
/**
* @deprecated Use {@link self::getRawWeatherData()} instead.
*/
public function getRawData($query, $units = 'imperial', $lang = 'en', $appid = '', $mode = 'xml')
{
return $this->getRawWeatherData($query, $units, $lang, $appid, $mode);
}
/**
* Directly returns the xml/json/html string returned by OpenWeatherMap for the current weather.
*
* @param array|int|string $query The place to get weather information for. For possible values see below.
* @param string $units Can be either 'metric' or 'imperial' (default). This affects almost all units returned.
* @param string $lang The language to use for descriptions, default is 'en'. For possible values see below.
* @param string $appid Your app id, default ''. See http://openweathermap.org/appid for more details.
* @param string $mode The format of the data fetched. Possible values are 'json', 'html' and 'xml' (default).
*
* @return string Returns false on failure and the fetched data in the format you specified on success.
*
* Warning If an error occurred, OpenWeatherMap returns data in json format ALWAYS
*
* There are three ways to specify the place to get weather information for:
* - Use the city name: $query must be a string containing the city name.
* - Use the city id: $query must be an integer containing the city id.
* - Use the coordinates: $query must be an associative array containing the 'lat' and 'lon' values.
*
* Available languages are (as of 17. July 2013):
* - English - en
* - Russian - ru
* - Italian - it
* - Spanish - sp
* - Ukrainian - ua
* - German - de
* - Portuguese - pt
* - Romanian - ro
* - Polish - pl
* - Finnish - fi
* - Dutch - nl
* - French - fr
* - Bulgarian - bg
* - Swedish - se
* - Chinese Traditional - zh_tw
* - Chinese Simplified - zh_cn
* - Turkish - tr
*
* @api
*/
public function getRawWeatherData($query, $units = 'imperial', $lang = 'en', $appid = '', $mode = 'xml')
{
$url = $this->buildUrl($query, $units, $lang, $appid, $mode, $this->weatherUrl);
return $this->cacheOrFetchResult($url);
}
/**
* Directly returns the xml/json/html string returned by OpenWeatherMap for the hourly forecast.
*
* @param array|int|string $query The place to get weather information for. For possible values see below.
* @param string $units Can be either 'metric' or 'imperial' (default). This affects almost all units returned.
* @param string $lang The language to use for descriptions, default is 'en'. For possible values see below.
* @param string $appid Your app id, default ''. See http://openweathermap.org/appid for more details.
* @param string $mode The format of the data fetched. Possible values are 'json', 'html' and 'xml' (default).
*
* @return string Returns false on failure and the fetched data in the format you specified on success.
*
* Warning If an error occurred, OpenWeatherMap returns data in json format ALWAYS
*
* There are three ways to specify the place to get weather information for:
* - Use the city name: $query must be a string containing the city name.
* - Use the city id: $query must be an integer containing the city id.
* - Use the coordinates: $query must be an associative array containing the 'lat' and 'lon' values.
*
* Available languages are (as of 17. July 2013):
* - English - en
* - Russian - ru
* - Italian - it
* - Spanish - sp
* - Ukrainian - ua
* - German - de
* - Portuguese - pt
* - Romanian - ro
* - Polish - pl
* - Finnish - fi
* - Dutch - nl
* - French - fr
* - Bulgarian - bg
* - Swedish - se
* - Chinese Traditional - zh_tw
* - Chinese Simplified - zh_cn
* - Turkish - tr
*
* @api
*/
public function getRawHourlyForecastData($query, $units = 'imperial', $lang = 'en', $appid = '', $mode = 'xml')
{
$url = $this->buildUrl($query, $units, $lang, $appid, $mode, $this->weatherHourlyForecastUrl);
return $this->cacheOrFetchResult($url);
}
/**
* Directly returns the xml/json/html string returned by OpenWeatherMap for the daily forecast.
*
* @param array|int|string $query The place to get weather information for. For possible values see below.
* @param string $units Can be either 'metric' or 'imperial' (default). This affects almost all units returned.
* @param string $lang The language to use for descriptions, default is 'en'. For possible values see below.
* @param string $appid Your app id, default ''. See http://openweathermap.org/appid for more details.
* @param string $mode The format of the data fetched. Possible values are 'json', 'html' and 'xml' (default)
* @param int $cnt How many days of forecast shall be returned? Maximum (and default): 16
*
* @throws \InvalidArgumentException If $cnt is higher than 16.
* @return string Returns false on failure and the fetched data in the format you specified on success.
*
* Warning If an error occurred, OpenWeatherMap returns data in json format ALWAYS
*
* There are three ways to specify the place to get weather information for:
* - Use the city name: $query must be a string containing the city name.
* - Use the city id: $query must be an integer containing the city id.
* - Use the coordinates: $query must be an associative array containing the 'lat' and 'lon' values.
*
* Available languages are (as of 17. July 2013):
* - English - en
* - Russian - ru
* - Italian - it
* - Spanish - sp
* - Ukrainian - ua
* - German - de
* - Portuguese - pt
* - Romanian - ro
* - Polish - pl
* - Finnish - fi
* - Dutch - nl
* - French - fr
* - Bulgarian - bg
* - Swedish - se
* - Chinese Traditional - zh_tw
* - Chinese Simplified - zh_cn
* - Turkish - tr
*
* @api
*/
public function getRawDailyForecastData($query, $units = 'imperial', $lang = 'en', $appid = '', $mode = 'xml', $cnt = 16)
{
if ($cnt > 16) {
throw new \InvalidArgumentException('$cnt must be 16 or below!');
}
$url = $this->buildUrl($query, $units, $lang, $appid, $mode, $this->weatherDailyForecastUrl) . "&cnt=$cnt";
return $this->cacheOrFetchResult($url);
}
/**
* Directly returns the xml/json/html string returned by OpenWeatherMap for the daily forecast.
*
* @param array|int|string $query The place to get weather information for. For possible values see below.
* @param \DateTime $start The \DateTime object of the date to get the first weather information from.
* @param \DateTime|int $endOrCount Can be either a \DateTime object representing the end of the period to
* receive weather history data for or an integer counting the number of
* reports requested.
* @param string $type The period of the weather history requested. Can be either be either "tick",
* "hour" or "day".
* @param string $units Can be either 'metric' or 'imperial' (default). This affects almost all units returned.
* @param string $lang The language to use for descriptions, default is 'en'. For possible values see below.
* @param string $appid Your app id, default ''. See http://openweathermap.org/appid for more details.
*
* @throws \InvalidArgumentException
*
* @return string Returns false on failure and the fetched data in the format you specified on success.
*
* Warning If an error occurred, OpenWeatherMap returns data in json format ALWAYS
*
* There are three ways to specify the place to get weather information for:
* - Use the city name: $query must be a string containing the city name.
* - Use the city id: $query must be an integer containing the city id.
* - Use the coordinates: $query must be an associative array containing the 'lat' and 'lon' values.
*
* Available languages are (as of 17. July 2013):
* - English - en
* - Russian - ru
* - Italian - it
* - Spanish - sp
* - Ukrainian - ua
* - German - de
* - Portuguese - pt
* - Romanian - ro
* - Polish - pl
* - Finnish - fi
* - Dutch - nl
* - French - fr
* - Bulgarian - bg
* - Swedish - se
* - Chinese Traditional - zh_tw
* - Chinese Simplified - zh_cn
* - Turkish - tr
*
* @api
*/
public function getRawWeatherHistory($query, \DateTime $start, $endOrCount = 1, $type = 'hour', $units = 'imperial', $lang = 'en', $appid = '')
{
if (!in_array($type, array('tick', 'hour', 'day'))) {
throw new \InvalidArgumentException('$type must be either "tick", "hour" or "day"');
}
$queryUrl = $this->weatherHistoryUrl . $this->buildQueryUrlParameter($query) . "&start={$start->format('U')}";
if ($endOrCount instanceof \DateTime) {
$queryUrl .= "&end={$endOrCount->format('U')}";
} else if (is_numeric($endOrCount) && $endOrCount > 0) {
$queryUrl .= "&cnt=$endOrCount";
} else {
throw new \InvalidArgumentException('$endOrCount must be either a \DateTime or a positive integer.');
}
$queryUrl .= "&type=$type&units=$units&lang=$lang";
if (!empty($appid)) {
$queryUrl .= "&APPID=$appid";
}
return $this->cacheOrFetchResult($queryUrl);
}
/**
* Fetches the result or delivers a cached version of the result.
*
* @param $url
*
* @return string
*
* @internal
*/
private function cacheOrFetchResult($url)
{
if ($this->cacheClass !== false) {
/** @var \Cmfcmf\OpenWeatherMap\AbstractCache $cache */
$cache = $this->cacheClass;
$cache->setSeconds($this->seconds);
if ($cache->isCached($url)) {
return $cache->getCached($url);
}
$result = $this->fetcher->fetch($url);
$cache->setCached($url, $result);
} else {
$result = $this->fetcher->fetch($url);
}
return $result;
}
/**
* Build the url to fetch weather data from.
*
* @param $query
* @param $units
* @param $lang
* @param $appid
* @param $mode
* @param string $url The url to prepend.
*
* @return bool|string The fetched url, false on failure.
*
* @internal
*/
private function buildUrl($query, $units, $lang, $appid, $mode, $url)
{
$queryUrl = $this->buildQueryUrlParameter($query);
$url = $url . "$queryUrl&units=$units&lang=$lang&mode=$mode";
if (!empty($appid)) {
$url .= "&APPID=$appid";
}
return $url;
}
/**
* Builds the query string for the url.
*
* @param $query
*
* @return string The built query string for the url.
* @throws \InvalidArgumentException If the query parameter is invalid.
*
* @internal
*/
private function buildQueryUrlParameter($query)
{
switch ($query) {
case (is_array($query) && isset($query['lat']) && isset($query['lon']) && is_numeric($query['lat']) && is_numeric($query['lon'])):
return "lat={$query['lat']}&lon={$query['lon']}";
case (is_numeric($query)):
return "id=$query";
case (is_string($query)):
return "q=" . urlencode($query);
default:
throw new \InvalidArgumentException('Error: $query has the wrong format. See the documentation of OpenWeatherMap::getRawData() to read about valid formats.');
}
}
}

View file

@ -0,0 +1,69 @@
<?php
/**
* OpenWeatherMap-PHP-API A php api to parse weather data from http://www.OpenWeatherMap.org .
*
* @license MIT
*
* Please see the LICENSE file distributed with this source code for further
* information regarding copyright and licensing.
*
* Please visit the following links to read about the usage policies and the license of
* OpenWeatherMap before using this class:
*
* @see http://www.OpenWeatherMap.org
* @see http://www.OpenWeatherMap.org/terms
* @see http://openweathermap.org/appid
*/
namespace Cmfcmf\OpenWeatherMap;
/**
* Abstract cache class to be overwritten by custom cache implementations.
*/
abstract class AbstractCache
{
/**
* @var int $seconds Cache time in seconds.
*/
protected $seconds;
/**
* Checks whether a cached weather data is available.
*
* @param string $url The unique url of the cached content.
*
* @return bool False if no cached information is available, otherwise true.
*
* You need to check if a cached result is outdated here. Return false in that case.
*/
abstract public function isCached($url);
/**
* Returns cached weather data.
*
* @param string $url The unique url of the cached content.
*
* @return string|bool The cached data if it exists, false otherwise.
*/
abstract public function getCached($url);
/**
* Saves cached weather data.
*
* @param string $url The unique url of the cached content.
* @param string $content The weather data to cache.
*
* @return bool True on success, false on failure.
*/
abstract public function setCached($url, $content);
/**
* Set after how much seconds the cache shall expire.
*
* @param int $seconds
*/
public function setSeconds($seconds)
{
$this->seconds = $seconds;
}
}

View file

@ -0,0 +1,117 @@
<?php
/**
* OpenWeatherMap-PHP-API A php api to parse weather data from http://www.OpenWeatherMap.org .
*
* @license MIT
*
* Please see the LICENSE file distributed with this source code for further
* information regarding copyright and licensing.
*
* Please visit the following links to read about the usage policies and the license of
* OpenWeatherMap before using this class:
*
* @see http://www.OpenWeatherMap.org
* @see http://www.OpenWeatherMap.org/terms
* @see http://openweathermap.org/appid
*/
namespace Cmfcmf\OpenWeatherMap;
use Cmfcmf\OpenWeatherMap;
use Cmfcmf\OpenWeatherMap\Util\City;
use Cmfcmf\OpenWeatherMap\Util\Sun;
use Cmfcmf\OpenWeatherMap\Util\Temperature;
use Cmfcmf\OpenWeatherMap\Util\Unit;
use Cmfcmf\OpenWeatherMap\Util\Weather as WeatherObj;
use Cmfcmf\OpenWeatherMap\Util\Wind;
/**
* Weather class used to hold the current weather data.
*/
class CurrentWeather
{
/**
* The city object.
*
* @var Util\City
*/
public $city;
/**
* The temperature object.
*
* @var Util\Temperature
*/
public $temperature;
/**
* @var Util\Unit
*/
public $humidity;
/**
* @var Util\Unit
*/
public $pressure;
/**
* @var Util\Wind
*/
public $wind;
/**
* @var Util\Unit
*/
public $clouds;
/**
* @var Util\Unit
*/
public $precipitation;
/**
* @var Util\Sun
*/
public $sun;
/**
* @var Util\Weather
*/
public $weather;
/**
* @var \DateTime
*/
public $lastUpdate;
/**
* Create a new weather object.
*
* @param \SimpleXMLElement $xml
* @param string $units
*
* @internal
*/
public function __construct(\SimpleXMLElement $xml, $units)
{
$this->city = new City($xml->city['id'], $xml->city['name'], $xml->city->coord['lon'], $xml->city->coord['lat'], $xml->city->country);
$this->temperature = new Temperature(new Unit($xml->temperature['value'], $xml->temperature['unit']), new Unit($xml->temperature['min'], $xml->temperature['unit']), new Unit($xml->temperature['max'], $xml->temperature['unit']));
$this->humidity = new Unit($xml->humidity['value'], $xml->humidity['unit']);
$this->pressure = new Unit($xml->pressure['value'], $xml->pressure['unit']);
// This is kind of a hack, because the units are missing in the xml document.
if ($units == 'metric') {
$windSpeedUnit = 'm/s';
} else {
$windSpeedUnit = 'mph';
}
$this->wind = new Wind(new Unit($xml->wind->speed['value'], $windSpeedUnit, $xml->wind->speed['name']), new Unit($xml->wind->direction['value'], $xml->wind->direction['code'], $xml->wind->direction['name']));
$this->clouds = new Unit($xml->clouds['value'], null, $xml->clouds['name']);
$this->precipitation = new Unit($xml->precipitation['value'], $xml->precipitation['unit'], $xml->precipitation['mode']);
$utctz = new \DateTimeZone('UTC');
$this->sun = new Sun(new \DateTime($xml->city->sun['rise'], $utctz), new \DateTime($xml->city->sun['set'], $utctz));
$this->weather = new WeatherObj($xml->weather['number'], $xml->weather['value'], $xml->weather['icon']);
$this->lastUpdate = new \DateTime($xml->lastupdate['value'], $utctz);
}
}

View file

@ -0,0 +1,27 @@
<?php
/**
* OpenWeatherMap-PHP-API A php api to parse weather data from http://www.OpenWeatherMap.org .
*
* @license MIT
*
* Please see the LICENSE file distributed with this source code for further
* information regarding copyright and licensing.
*
* Please visit the following links to read about the usage policies and the license of
* OpenWeatherMap before using this class:
*
* @see http://www.OpenWeatherMap.org
* @see http://www.OpenWeatherMap.org/terms
* @see http://openweathermap.org/appid
*/
namespace Cmfcmf\OpenWeatherMap;
/**
* Dummy class extending \Exception to allow checking if it is an OpenWeatherMap error
* or an argument error.
*/
class Exception extends \Exception
{
}

View file

@ -0,0 +1,42 @@
<?php
/**
* OpenWeatherMap-PHP-API A php api to parse weather data from http://www.OpenWeatherMap.org .
*
* @license MIT
*
* Please see the LICENSE file distributed with this source code for further
* information regarding copyright and licensing.
*
* Please visit the following links to read about the usage policies and the license of
* OpenWeatherMap before using this class:
*
* @see http://www.OpenWeatherMap.org
* @see http://www.OpenWeatherMap.org/terms
* @see http://openweathermap.org/appid
*/
namespace Cmfcmf\OpenWeatherMap\Fetcher;
/**
* Class CurlFetcher.
*
* @internal
*/
class CurlFetcher implements FetcherInterface
{
/**
* {@inheritdoc}
*/
public function fetch($url)
{
$ch = curl_init($url);
$timeout = 5;
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $timeout);
$content = curl_exec($ch);
curl_close($ch);
return $content;
}
}

View file

@ -0,0 +1,37 @@
<?php
/**
* OpenWeatherMap-PHP-API A php api to parse weather data from http://www.OpenWeatherMap.org .
*
* @license MIT
*
* Please see the LICENSE file distributed with this source code for further
* information regarding copyright and licensing.
*
* Please visit the following links to read about the usage policies and the license of
* OpenWeatherMap before using this class:
*
* @see http://www.OpenWeatherMap.org
* @see http://www.OpenWeatherMap.org/terms
* @see http://openweathermap.org/appid
*/
namespace Cmfcmf\OpenWeatherMap\Fetcher;
/**
* Interface FetcherInterface.
*
* @api
*/
interface FetcherInterface
{
/**
* Fetch contents from the specified url.
*
* @param string $url The url to be fetched.
*
* @return string The fetched content.
*
* @api
*/
public function fetch($url);
}

View file

@ -0,0 +1,34 @@
<?php
/**
* OpenWeatherMap-PHP-API A php api to parse weather data from http://www.OpenWeatherMap.org .
*
* @license MIT
*
* Please see the LICENSE file distributed with this source code for further
* information regarding copyright and licensing.
*
* Please visit the following links to read about the usage policies and the license of
* OpenWeatherMap before using this class:
*
* @see http://www.OpenWeatherMap.org
* @see http://www.OpenWeatherMap.org/terms
* @see http://openweathermap.org/appid
*/
namespace Cmfcmf\OpenWeatherMap\Fetcher;
/**
* Class FileGetContentsFetcher.
*
* @internal
*/
class FileGetContentsFetcher implements FetcherInterface
{
/**
* {@inheritdoc}
*/
public function fetch($url)
{
return file_get_contents($url);
}
}

View file

@ -0,0 +1,83 @@
<?php
/**
* OpenWeatherMap-PHP-API A php api to parse weather data from http://www.OpenWeatherMap.org .
*
* @license MIT
*
* Please see the LICENSE file distributed with this source code for further
* information regarding copyright and licensing.
*
* Please visit the following links to read about the usage policies and the license of
* OpenWeatherMap before using this class:
*
* @see http://www.OpenWeatherMap.org
* @see http://www.OpenWeatherMap.org/terms
* @see http://openweathermap.org/appid
*/
namespace Cmfcmf\OpenWeatherMap;
use Cmfcmf\OpenWeatherMap;
use Cmfcmf\OpenWeatherMap\Util\City;
use Cmfcmf\OpenWeatherMap\Util\Sun;
use Cmfcmf\OpenWeatherMap\Util\Temperature;
use Cmfcmf\OpenWeatherMap\Util\Time;
use Cmfcmf\OpenWeatherMap\Util\Unit;
use Cmfcmf\OpenWeatherMap\Util\Weather as WeatherObj;
use Cmfcmf\OpenWeatherMap\Util\Wind;
/**
* Class Forecast.
*/
class Forecast extends CurrentWeather
{
/**
* @var Time The time of the forecast.
*/
public $time;
/**
* Create a new weather object for forecasts.
*
* @param \SimpleXMLElement $xml The forecasts xml.
* @param string $units Ths units used.
*
* @internal
*/
public function __construct(\SimpleXMLElement $xml, $units)
{
$this->city = new City($xml->city['id'], $xml->city['name'], $xml->city->coord['lon'], $xml->city->coord['lat'], $xml->city->country);
if ($units == 'metric') {
$temperatureUnit = "&deg;C";
} else {
$temperatureUnit = 'F';
}
$xml->temperature['value'] = ($xml->temperature['max'] + $xml->temperature['min']) / 2;
$this->temperature = new Temperature(new Unit($xml->temperature['value'], $temperatureUnit), new Unit($xml->temperature['min'], $temperatureUnit), new Unit($xml->temperature['max'], $temperatureUnit));
$this->humidity = new Unit($xml->humidity['value'], $xml->humidity['unit']);
$this->pressure = new Unit($xml->pressure['value'], $xml->pressure['unit']);
// This is kind of a hack, because the units are missing in the xml document.
if ($units == 'metric') {
$windSpeedUnit = 'm/s';
} else {
$windSpeedUnit = 'mps';
}
$this->wind = new Wind(new Unit($xml->windSpeed['mps'], $windSpeedUnit, $xml->windSpeed['name']), new Unit($xml->windDirection['value'], $xml->windDirection['code'], $xml->windDirection['name']));
$this->clouds = new Unit($xml->clouds['all'], $xml->clouds['unit'], $xml->clouds['value']);
$this->precipitation = new Unit($xml->precipitation['value'], null, $xml->precipitation['type']);
$this->sun = new Sun(new \DateTime($xml->city->sun['rise']), new \DateTime($xml->city->sun['set']));
$this->weather = new WeatherObj($xml->symbol['number'], $xml->symbol['name'], $xml->symbol['var']);
$this->lastUpdate = new \DateTime($xml->lastupdate['value']);
if (isset($xml['from'])) {
$this->time = new Time($xml['from'], $xml['to']);
} else {
$this->time = new Time($xml['day']);
}
}
}

View file

@ -0,0 +1,104 @@
<?php
/**
* OpenWeatherMap-PHP-API A php api to parse weather data from http://www.OpenWeatherMap.org .
*
* @license MIT
*
* Please see the LICENSE file distributed with this source code for further
* information regarding copyright and licensing.
*
* Please visit the following links to read about the usage policies and the license of
* OpenWeatherMap before using this class:
*
* @see http://www.OpenWeatherMap.org
* @see http://www.OpenWeatherMap.org/terms
* @see http://openweathermap.org/appid
*/
namespace Cmfcmf\OpenWeatherMap;
use Cmfcmf\OpenWeatherMap\Util\Temperature;
use Cmfcmf\OpenWeatherMap\Util\Unit;
use Cmfcmf\OpenWeatherMap\Util\Weather;
use Cmfcmf\OpenWeatherMap\Util\Wind;
/**
* Class WeatherHistory.
*/
class History
{
/**
* The city object.
*
* @var Util\City
*/
public $city;
/**
* The temperature object.
*
* @var Util\Temperature
*/
public $temperature;
/**
* @var Util\Unit
*/
public $humidity;
/**
* @var Util\Unit
*/
public $pressure;
/**
* @var Util\Wind
*/
public $wind;
/**
* @var Util\Unit
*/
public $clouds;
/**
* @var Util\Unit
*/
public $precipitation;
/**
* @var Util\Weather
*/
public $weather;
/**
* @var \DateTime The time of the history.
*/
public $time;
/**
* @param $city
* @param $weather
* @param $temperature
* @param $pressure
* @param $humidity
* @param $clouds
* @param $rain
* @param $wind
* @param $time
*
* @internal
*/
public function __construct($city, $weather, $temperature, $pressure, $humidity, $clouds, $rain, $wind, $time)
{
$this->city = $city;
$this->weather = new Weather($weather['id'], $weather['description'], $weather['icon']);
$this->temperature = new Temperature(new Unit($temperature['now'] - 273.15, "\xB0C"), new Unit($temperature['min'] - 273.15, "\xB0C"), new Unit($temperature['max'] - 273.15, "\xB0C"));
$this->pressure = new Unit($pressure, 'kPa');
$this->humidity = new Unit($humidity, '%');
$this->clouds = new Unit($clouds, '%');
$this->precipitation = new Unit($rain['val'], $rain['unit']);
$this->wind = new Wind(new Unit($wind['speed']), new Unit($wind['deg']));
$this->time = $time;
}
}

View file

@ -0,0 +1,76 @@
<?php
/**
* OpenWeatherMap-PHP-API A php api to parse weather data from http://www.OpenWeatherMap.org .
*
* @license MIT
*
* Please see the LICENSE file distributed with this source code for further
* information regarding copyright and licensing.
*
* Please visit the following links to read about the usage policies and the license of
* OpenWeatherMap before using this class:
*
* @see http://www.OpenWeatherMap.org
* @see http://www.OpenWeatherMap.org/terms
* @see http://openweathermap.org/appid
*/
namespace Cmfcmf\OpenWeatherMap\Util;
/**
* The city class representing a city object.
*/
class City
{
/**
* @var int The city id.
*/
public $id;
/**
* @var string The name of the city.
*/
public $name;
/**
* @var float The longitude of the city.
*/
public $lon;
/**
* @var float The latitude of the city.
*/
public $lat;
/**
* @var string The abbreviation of the country the city is located in.
*/
public $country;
/**
* @var int The city's population
*/
public $population;
/**
* Create a new city object.
*
* @param int $id The city id.
* @param string $name The name of the city.
* @param float $lon The longitude of the city.
* @param float $lat The latitude of the city.
* @param string $country The abbreviation of the country the city is located in
* @param int $population The city's population.
*
* @internal
*/
public function __construct($id, $name = null, $lon = null, $lat = null, $country = null, $population = null)
{
$this->id = (int)$id;
$this->name = isset($name) ? (string)$name : null;
$this->lon = isset($lon) ? (float)$lon : null;
$this->lat = isset($lat) ? (float)$lat : null;
$this->country = isset($country) ? (string)$country : null;
$this->population = isset($population) ? (int)$population : null;
}
}

View file

@ -0,0 +1,52 @@
<?php
/**
* OpenWeatherMap-PHP-API A php api to parse weather data from http://www.OpenWeatherMap.org .
*
* @license MIT
*
* Please see the LICENSE file distributed with this source code for further
* information regarding copyright and licensing.
*
* Please visit the following links to read about the usage policies and the license of
* OpenWeatherMap before using this class:
*
* @see http://www.OpenWeatherMap.org
* @see http://www.OpenWeatherMap.org/terms
* @see http://openweathermap.org/appid
*/
namespace Cmfcmf\OpenWeatherMap\Util;
/**
* The sun class representing a sun object.
*/
class Sun
{
/**
* @var \DateTime The time of the sun rise.
*/
public $rise;
/**
* @var \DateTime The time of the sun set.
*/
public $set;
/**
* Create a new sun object.
*
* @param \DateTime $rise The time of the sun rise
* @param \DateTime $set The time of the sun set.
*
* @throws \LogicException If sunset is before sunrise.
* @internal
*/
public function __construct(\DateTime $rise, \DateTime $set)
{
if ($set < $rise) {
throw new \LogicException('Sunset cannot be before sunrise!');
}
$this->rise = $rise;
$this->set = $set;
}
}

View file

@ -0,0 +1,105 @@
<?php
/**
* OpenWeatherMap-PHP-API A php api to parse weather data from http://www.OpenWeatherMap.org .
*
* @license MIT
*
* Please see the LICENSE file distributed with this source code for further
* information regarding copyright and licensing.
*
* Please visit the following links to read about the usage policies and the license of
* OpenWeatherMap before using this class:
*
* @see http://www.OpenWeatherMap.org
* @see http://www.OpenWeatherMap.org/terms
* @see http://openweathermap.org/appid
*/
namespace Cmfcmf\OpenWeatherMap\Util;
/**
* The temperature class representing a temperature object.
*/
class Temperature
{
/**
* @var Unit The current temperature.
*/
public $now;
/**
* @var Unit The minimal temperature.
*/
public $min;
/**
* @var Unit The maximal temperature.
*/
public $max;
/**
* Returns the current temperature as formatted string.
*
* @return string The current temperature as a formatted string.
*/
public function __toString()
{
return $this->now->__toString();
}
/**
* Returns the current temperature's unit.
*
* @return string The current temperature's unit.
*/
public function getUnit()
{
return $this->now->getUnit();
}
/**
* Returns the current temperature.
*
* @return string The current temperature.
*/
public function getValue()
{
return $this->now->getValue();
}
/**
* Returns the current temperature's description.
*
* @return string The current temperature's description.
*/
public function getDescription()
{
return $this->now->getDescription();
}
/**
* Returns the current temperature as formatted string.
*
* @return string The current temperature as formatted string.
*/
public function getFormatted()
{
return $this->now->getFormatted();
}
/**
* Create a new temperature object.
*
* @param Unit $now The current temperature.
* @param Unit $min The minimal temperature.
* @param Unit $max The maximal temperature.
*
* @internal
*/
public function __construct(Unit $now, Unit $min, Unit $max)
{
$this->now = $now;
$this->min = $min;
$this->max = $max;
}
}

View file

@ -0,0 +1,65 @@
<?php
/**
* OpenWeatherMap-PHP-API A php api to parse weather data from http://www.OpenWeatherMap.org .
*
* @license MIT
*
* Please see the LICENSE file distributed with this source code for further
* information regarding copyright and licensing.
*
* Please visit the following links to read about the usage policies and the license of
* OpenWeatherMap before using this class:
*
* @see http://www.OpenWeatherMap.org
* @see http://www.OpenWeatherMap.org/terms
* @see http://openweathermap.org/appid
*/
namespace Cmfcmf\OpenWeatherMap\Util;
/**
* The time class representing a time object.
*/
class Time
{
/**
* @var \DateTime The start time for the forecast.
*/
public $from;
/**
* @var \DateTime The end time for the forecast.
*/
public $to;
/**
* @var \DateTime The day of the forecast.
*/
public $day;
/**
* Create a new time object.
*
* @param string|\DateTime $from The start time for the forecast.
* @param string|\DateTime $to The end time for the forecast.
*
* @internal
*/
public function __construct($from, $to = null)
{
if (isset($to)) {
$from = ($from instanceof \DateTime) ? $from : new \DateTime((string)$from);
$to = ($to instanceof \DateTime) ? $to : new \DateTime((string)$to);
$day = new \DateTime($from->format('Y-m-d'));
} else {
$from = ($from instanceof \DateTime) ? $from : new \DateTime((string)$from);
$day = clone $from;
$to = clone $from;
$to = $to->add(new \DateInterval('PT23H59M59S'));
}
$this->from = $from;
$this->to = $to;
$this->day = $day;
}
}

View file

@ -0,0 +1,128 @@
<?php
/**
* OpenWeatherMap-PHP-API A php api to parse weather data from http://www.OpenWeatherMap.org .
*
* @license MIT
*
* Please see the LICENSE file distributed with this source code for further
* information regarding copyright and licensing.
*
* Please visit the following links to read about the usage policies and the license of
* OpenWeatherMap before using this class:
*
* @see http://www.OpenWeatherMap.org
* @see http://www.OpenWeatherMap.org/terms
* @see http://openweathermap.org/appid
*/
namespace Cmfcmf\OpenWeatherMap\Util;
/**
* The unit class representing a unit object.
*/
class Unit
{
/**
* @var float The value.
*
* @internal
*/
private $value;
/**
* @var string The value's unit.
*
* @internal
*/
private $unit;
/**
* @var string The value's description.
*
* @internal
*/
private $description;
/**
* Create a new unit object.
*
* @param float $value The value.
* @param string $unit The unit of the value.
* @param string $description The description of the value.
*
* @internal
*/
public function __construct($value = 0.0, $unit = "", $description = "")
{
$this->value = (float)$value;
$this->unit = (string)$unit;
$this->description = (string)$description;
}
/**
* Get the value as formatted string with unit.
*
* @return string The value as formatted string with unit.
*
* The unit is not included if it is empty.
*/
public function __toString()
{
return $this->getFormatted();
}
/**
* Get the value's unit.
*
* @return string The value's unit.
*
* This also converts 'celsius' to '°C' and 'fahrenheit' to 'F'.
*/
public function getUnit()
{
// Units are inconsistent. Only celsius and fahrenheit are not abbreviated. This check fixes that.
if ($this->unit == 'celsius') {
return "&deg;C";
} else if ($this->unit == 'fahrenheit') {
return 'F';
} else {
return $this->unit;
}
}
/**
* Get the value.
*
* @return float The value.
*/
public function getValue()
{
return $this->value;
}
/**
* Get the value's description.
*
* @return string The value's description.
*/
public function getDescription()
{
return $this->description;
}
/**
* Get the value as formatted string with unit.
*
* @return string The value as formatted string with unit.
*
* The unit is not included if it is empty.
*/
public function getFormatted()
{
if ($this->getUnit() != "") {
return "{$this->getValue()} {$this->getUnit()}";
} else {
return "{$this->getValue()}";
}
}
}

View file

@ -0,0 +1,82 @@
<?php
/**
* OpenWeatherMap-PHP-API A php api to parse weather data from http://www.OpenWeatherMap.org .
*
* @license MIT
*
* Please see the LICENSE file distributed with this source code for further
* information regarding copyright and licensing.
*
* Please visit the following links to read about the usage policies and the license of
* OpenWeatherMap before using this class:
*
* @see http://www.OpenWeatherMap.org
* @see http://www.OpenWeatherMap.org/terms
* @see http://openweathermap.org/appid
*/
namespace Cmfcmf\OpenWeatherMap\Util;
/**
* The weather class representing a weather object.
*/
class Weather
{
/**
* @var int The weather id.
*/
public $id;
/**
* @var string The weather description.
*/
public $description;
/**
* @var string the icon name.
*/
public $icon;
/**
* @var string The url for icons.
*
* @see self::getIconUrl() to see how it is used.
*/
private $iconUrl = "http://openweathermap.org/img/w/%s.png";
/**
* Create a new weather object.
*
* @param int $id The icon id.
* @param string $description The weather description.
* @param string $icon The icon name.
*
* @internal
*/
public function __construct($id, $description, $icon)
{
$this->id = (int)$id;
$this->description = (string)$description;
$this->icon = (string)$icon;
}
/**
* Get the weather description.
*
* @return string
*/
public function __toString()
{
return $this->description;
}
/**
* Get the icon url.
*
* @return string The icon url.
*/
public function getIconUrl()
{
return str_replace("%s", $this->icon, $this->iconUrl);
}
}

View file

@ -0,0 +1,48 @@
<?php
/**
* OpenWeatherMap-PHP-API A php api to parse weather data from http://www.OpenWeatherMap.org .
*
* @license MIT
*
* Please see the LICENSE file distributed with this source code for further
* information regarding copyright and licensing.
*
* Please visit the following links to read about the usage policies and the license of
* OpenWeatherMap before using this class:
*
* @see http://www.OpenWeatherMap.org
* @see http://www.OpenWeatherMap.org/terms
* @see http://openweathermap.org/appid
*/
namespace Cmfcmf\OpenWeatherMap\Util;
/**
* The wind class representing a wind object.
*/
class Wind
{
/**
* @var Unit The wind speed.
*/
public $speed;
/**
* @var Unit The wind direction.
*/
public $direction;
/**
* Create a new wind object.
*
* @param Unit $speed The wind speed.
* @param Unit $direction The wind direction.
*
* @internal
*/
public function __construct(Unit $speed, Unit $direction)
{
$this->speed = $speed;
$this->direction = $direction;
}
}

View file

@ -0,0 +1,136 @@
<?php
/**
* OpenWeatherMap-PHP-API A php api to parse weather data from http://www.OpenWeatherMap.org .
*
* @license MIT
*
* Please see the LICENSE file distributed with this source code for further
* information regarding copyright and licensing.
*
* Please visit the following links to read about the usage policies and the license of
* OpenWeatherMap before using this class:
*
* @see http://www.OpenWeatherMap.org
* @see http://www.OpenWeatherMap.org/terms
* @see http://openweathermap.org/appid
*/
namespace Cmfcmf\OpenWeatherMap;
use Cmfcmf\OpenWeatherMap;
use Cmfcmf\OpenWeatherMap\Util\City;
use Cmfcmf\OpenWeatherMap\Util\Sun;
/**
* Weather class returned by Cmfcmf\OpenWeatherMap->getWeather().
*
* @see Cmfcmf\OpenWeatherMap::getWeather() The function using it.
*/
class WeatherForecast implements \Iterator
{
/**
* A city object.
*
* @var Util\City
*/
public $city;
/**
* A sun object
*
* @var Util\Sun
*/
public $sun;
/**
* The time of the last update of this weather data.
*
* @var \DateTime
*/
public $lastUpdate;
/**
* An array of {@link WeatherForecast} objects.
*
* @var array
*
* @see WeatherForecast The WeatherForecast class.
*/
private $forecasts;
/**
* @internal
*/
private $position = 0;
/**
* Create a new Forecast object.
*
* @param $xml
* @param string $units
* @param int $days How many days of forecast to receive.
*
* @internal
*/
public function __construct($xml, $units, $days)
{
$this->city = new City(-1, $xml->location->name, $xml->location->location['longitude'], $xml->location->location['latitude'], $xml->location->country);
$this->sun = new Sun(new \DateTime($xml->sun['rise']), new \DateTime($xml->sun['set']));
$this->lastUpdate = new \DateTime($xml->meta->lastupdate);
$counter = 0;
foreach ($xml->forecast->time as $time) {
$forecast = new Forecast($time, $units);
$forecast->city = $this->city;
$this->forecasts[] = $forecast;
$counter++;
// Make sure to only return the requested number of days.
if ($days <= 5 && $counter == $days * 8) {
break;
} else if ($days > 5 && $counter == $days) {
break;
}
}
}
/**
* @internal
*/
public function rewind()
{
$this->position = 0;
}
/**
* @internal
*/
public function current()
{
return $this->forecasts[$this->position];
}
/**
* @internal
*/
public function key()
{
return $this->position;
}
/**
* @internal
*/
public function next()
{
++$this->position;
}
/**
* @internal
*/
public function valid()
{
return isset($this->forecasts[$this->position]);
}
}

View file

@ -0,0 +1,118 @@
<?php
/**
* OpenWeatherMap-PHP-API A php api to parse weather data from http://www.OpenWeatherMap.org .
*
* @license MIT
*
* Please see the LICENSE file distributed with this source code for further
* information regarding copyright and licensing.
*
* Please visit the following links to read about the usage policies and the license of
* OpenWeatherMap before using this class:
*
* @see http://www.OpenWeatherMap.org
* @see http://www.OpenWeatherMap.org/terms
* @see http://openweathermap.org/appid
*/
namespace Cmfcmf\OpenWeatherMap;
use Cmfcmf\OpenWeatherMap;
/**
* Class WeatherHistory.
*/
class WeatherHistory implements \Iterator
{
/**
* The city object. IMPORTANT: Not all values will be set
*
* @var Util\City
*/
public $city;
/**
* The time needed to calculate the request data.
*
* @var float
*/
public $calctime;
/**
* An array of {@link WeatherHistory} objects.
*
* @var array
*
* @see WeatherForecast The WeatherForecast class.
*/
private $histories;
/**
* @internal
*/
private $position = 0;
public function __construct($weatherHistory, $query)
{
if (isset($weatherHistory['list'][0]['city'])) {
$country = $weatherHistory['list'][0]['city']['country'];
$population = $weatherHistory['list'][0]['city']['population'];
} else {
$country = null;
$population = null;
}
$this->city = new OpenWeatherMap\Util\City($weatherHistory['city_id'], (is_string($query)) ? $query : null, (isset($query['lon'])) ? $query['lon'] : null, (isset($query['lat'])) ? $query['lat'] : null, $country, $population);
$this->calctime = $weatherHistory['calctime'];
foreach ($weatherHistory['list'] as $history) {
if (isset($history['rain'])) {
$units = array_keys($history['rain']);
} else {
$units = array(0 => null);
}
$this->histories[] = new History($this->city, $history['weather'][0], array('now' => $history['main']['temp'], 'min' => $history['main']['temp_min'], 'max' => $history['main']['temp_max']), $history['main']['pressure'], $history['main']['humidity'], $history['clouds']['all'], isset($history['rain']) ? array('val' => $history['rain'][($units[0])], 'unit' => $units[0]) : null, $history['wind'], \DateTime::createFromFormat('U', $history['dt']));
}
}
/**
* @internal
*/
public function rewind()
{
$this->position = 0;
}
/**
* @internal
*/
public function current()
{
return $this->histories[$this->position];
}
/**
* @internal
*/
public function key()
{
return $this->position;
}
/**
* @internal
*/
public function next()
{
++$this->position;
}
/**
* @internal
*/
public function valid()
{
return isset($this->histories[$this->position]);
}
}