Skip to content

Commit 0647cf0

Browse files
committed
Merge pull request #7 from cmfcmf/refactor
[BC breaks] Refactoring, added weather history, removed autoloader.
2 parents 7403a0e + caec40d commit 0647cf0

27 files changed

+1220
-712
lines changed

.gitignore

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
/vendor
2+
/composer.lock

Cmfcmf/OpenWeatherMap.php

+596
Large diffs are not rendered by default.

cmfcmf/OpenWeatherMap/AbstractCache.php renamed to Cmfcmf/OpenWeatherMap/AbstractCache.php

+4-4
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
* @see http://openweathermap.org/appid
1515
*/
1616

17-
namespace cmfcmf\OpenWeatherMap;
17+
namespace Cmfcmf\OpenWeatherMap;
1818

1919
/**
2020
* Abstract cache class to be overwritten by custom cache implementations.
@@ -41,7 +41,7 @@ abstract class AbstractCache
4141
* @note This is not the time when the weather was cached, but the {@link Weather::$lastUpdate} value of the cached weather.
4242
* @note You need to check here if a cached result is outdated. Return false in that case.
4343
*/
44-
abstract function isCached($type, $query, $units, $lang, $mode);
44+
public abstract function isCached($type, $query, $units, $lang, $mode);
4545

4646
/**
4747
* Returns cached weather data.
@@ -54,7 +54,7 @@ abstract function isCached($type, $query, $units, $lang, $mode);
5454
*
5555
* @return string|bool The cached data if it exists, false otherwise.
5656
*/
57-
abstract function getCached($type, $query, $units, $lang, $mode);
57+
public abstract function getCached($type, $query, $units, $lang, $mode);
5858

5959
/**
6060
* Saves cached weather data.
@@ -68,7 +68,7 @@ abstract function getCached($type, $query, $units, $lang, $mode);
6868
*
6969
* @return bool True on success, false on failure.
7070
*/
71-
abstract function setCached($type, $content, $query, $units, $lang, $mode);
71+
public abstract function setCached($type, $content, $query, $units, $lang, $mode);
7272

7373
/**
7474
* Set after how much seconds the cache shall expire.

cmfcmf/OpenWeatherMap/Weather.php renamed to Cmfcmf/OpenWeatherMap/CurrentWeather.php

+14-57
Original file line numberDiff line numberDiff line change
@@ -14,21 +14,20 @@
1414
* @see http://openweathermap.org/appid
1515
*/
1616

17-
namespace cmfcmf\OpenWeatherMap;
17+
namespace Cmfcmf\OpenWeatherMap;
1818

19-
use cmfcmf\OpenWeatherMap,
20-
cmfcmf\OpenWeatherMap\Exception as OWMException,
21-
cmfcmf\OpenWeatherMap\Util\City,
22-
cmfcmf\OpenWeatherMap\Util\Sun,
23-
cmfcmf\OpenWeatherMap\Util\Temperature,
24-
cmfcmf\OpenWeatherMap\Util\Unit,
25-
cmfcmf\OpenWeatherMap\Util\Weather as WeatherObj,
26-
cmfcmf\OpenWeatherMap\Util\Wind;
19+
use Cmfcmf\OpenWeatherMap;
20+
use Cmfcmf\OpenWeatherMap\Util\City;
21+
use Cmfcmf\OpenWeatherMap\Util\Sun;
22+
use Cmfcmf\OpenWeatherMap\Util\Temperature;
23+
use Cmfcmf\OpenWeatherMap\Util\Unit;
24+
use Cmfcmf\OpenWeatherMap\Util\Weather as WeatherObj;
25+
use Cmfcmf\OpenWeatherMap\Util\Wind;
2726

2827
/**
29-
* Weather class returned by {@link OpenWeatherMap::getWeather()}.
28+
* Weather class used to hold the current weather data.
3029
*/
31-
class Weather
30+
class CurrentWeather
3231
{
3332
/**
3433
* The city object.
@@ -84,62 +83,21 @@ class Weather
8483
*/
8584
public $lastUpdate;
8685

87-
/**
88-
* The copyright notice. This is no offical text, this hint was made regarding to http://www.http://openweathermap.org/copyright.
89-
*
90-
* @var $copyright
91-
*
92-
* @see http://www.http://openweathermap.org/copyright http://www.http://openweathermap.org/copyright
93-
*/
94-
public $copyright = "Weather data from <a href=\"http://www.openweathermap.org\">OpenWeatherMap.org</a>";
95-
9686
/**
9787
* Create a new weather object.
98-
* @param $query
99-
* @param string $units
100-
* @param string $lang
101-
* @param string $appid
102-
* @param bool $cacheClass
103-
* @param int $seconds
10488
*
105-
* @throws OWMException If OpenWeatherMap returns an error.
106-
* @throws \Exception If the parameters are invalid.
89+
* @param $xml
90+
* @param string $units
10791
*
10892
* @internal
10993
*/
110-
public function __construct($query, $units = 'imperial', $lang = 'en', $appid = '', $cacheClass = false, $seconds = 600)
94+
public function __construct($xml, $units)
11195
{
112-
// Disable default error handling of SimpleXML (Do not throw E_WARNINGs).
113-
libxml_use_internal_errors(true);
114-
libxml_clear_errors();
115-
116-
$owm = new OpenWeatherMap($cacheClass, $seconds);
117-
118-
$answer = $owm->getRawWeatherData($query, $units, $lang, $appid, 'xml');
119-
if ($answer === false) {
120-
// $query has the wrong format, throw error.
121-
throw new \Exception('Error: $query has the wrong format. See the documentation of OpenWeatherMap::getRawData() to read about valid formats.');
122-
}
123-
124-
try {
125-
$xml = new \SimpleXMLElement($answer);
126-
} catch(\Exception $e) {
127-
// Invalid xml format. This happens in case OpenWeatherMap returns an error.
128-
// OpenWeatherMap always uses json for errors, even if one specifies xml as format.
129-
$error = json_decode($answer, true);
130-
if (isset($error['message'])) {
131-
throw new OWMException($error['message'], $error['cod']);
132-
} else {
133-
throw new OWMException('Unknown fatal error: OpenWeatherMap returned the following json object: ' . print_r($error));
134-
}
135-
}
136-
13796
$this->city = new City($xml->city['id'], $xml->city['name'], $xml->city->coord['lon'], $xml->city->coord['lat'], $xml->city->country);
13897
$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']));
13998
$this->humidity = new Unit($xml->humidity['value'], $xml->humidity['unit']);
14099
$this->pressure = new Unit($xml->pressure['value'], $xml->pressure['unit']);
141-
142-
100+
143101
// This is kind of a hack, because the units are missing in the xml document.
144102
if ($units == 'metric') {
145103
$windSpeedUnit = 'm/s';
@@ -148,7 +106,6 @@ public function __construct($query, $units = 'imperial', $lang = 'en', $appid =
148106
}
149107
$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']));
150108

151-
152109
$this->clouds = new Unit($xml->clouds['value'], null, $xml->clouds['name']);
153110
$this->precipitation = new Unit($xml->precipitation['value'], $xml->precipitation['unit'], $xml->precipitation['mode']);
154111
$this->sun = new Sun(new \DateTime($xml->city->sun['rise']), new \DateTime($xml->city->sun['set']));

cmfcmf/OpenWeatherMap/Exception.php renamed to Cmfcmf/OpenWeatherMap/Exception.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
* @see http://openweathermap.org/appid
1515
*/
1616

17-
namespace cmfcmf\OpenWeatherMap;
17+
namespace Cmfcmf\OpenWeatherMap;
1818

1919
/**
2020
* Dummy class extending \Exception to allow checking if it is an OpenWeatherMap error
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
<?php
2+
/**
3+
* OpenWeatherMap-PHP-API — An php api to parse weather data from http://www.OpenWeatherMap.org .
4+
*
5+
* @license MIT
6+
*
7+
* Please see the LICENSE file distributed with this source code for further
8+
* information regarding copyright and licensing.
9+
*
10+
* Please visit the following links to read about the usage policies and the license of OpenWeatherMap before using this class.
11+
* @see http://www.OpenWeatherMap.org
12+
* @see http://www.OpenWeatherMap.org/about
13+
* @see http://www.OpenWeatherMap.org/copyright
14+
* @see http://openweathermap.org/appid
15+
*/
16+
17+
namespace Cmfcmf\OpenWeatherMap\Fetcher;
18+
19+
/**
20+
* Class CurlFetcher.
21+
*
22+
* @internal
23+
*/
24+
class CurlFetcher implements FetcherInterface
25+
{
26+
/**
27+
* {@inheritdoc}
28+
*/
29+
public function fetch($url)
30+
{
31+
$ch = curl_init($url);
32+
$timeout = 5;
33+
curl_setopt($ch, CURLOPT_URL, $url);
34+
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
35+
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $timeout);
36+
$content = curl_exec($ch);
37+
curl_close( $ch );
38+
39+
return $content;
40+
}
41+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
<?php
2+
/**
3+
* OpenWeatherMap-PHP-API — An php api to parse weather data from http://www.OpenWeatherMap.org .
4+
*
5+
* @license MIT
6+
*
7+
* Please see the LICENSE file distributed with this source code for further
8+
* information regarding copyright and licensing.
9+
*
10+
* Please visit the following links to read about the usage policies and the license of OpenWeatherMap before using this class.
11+
* @see http://www.OpenWeatherMap.org
12+
* @see http://www.OpenWeatherMap.org/about
13+
* @see http://www.OpenWeatherMap.org/copyright
14+
* @see http://openweathermap.org/appid
15+
*/
16+
17+
namespace Cmfcmf\OpenWeatherMap\Fetcher;
18+
19+
/**
20+
* Interface FetcherInterface.
21+
*
22+
* @api
23+
*/
24+
interface FetcherInterface
25+
{
26+
/**
27+
* Fetch contents from the specified url.
28+
*
29+
* @param string $url The url to be fetched.
30+
*
31+
* @return string The fetched content.
32+
*
33+
* @api
34+
*/
35+
public function fetch ($url);
36+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
<?php
2+
/**
3+
* OpenWeatherMap-PHP-API — An php api to parse weather data from http://www.OpenWeatherMap.org .
4+
*
5+
* @license MIT
6+
*
7+
* Please see the LICENSE file distributed with this source code for further
8+
* information regarding copyright and licensing.
9+
*
10+
* Please visit the following links to read about the usage policies and the license of OpenWeatherMap before using this class.
11+
* @see http://www.OpenWeatherMap.org
12+
* @see http://www.OpenWeatherMap.org/about
13+
* @see http://www.OpenWeatherMap.org/copyright
14+
* @see http://openweathermap.org/appid
15+
*/
16+
17+
namespace Cmfcmf\OpenWeatherMap\Fetcher;
18+
19+
/**
20+
* Class FileGetContentsFetcher.
21+
*
22+
* @internal
23+
*/
24+
class FileGetContentsFetcher implements FetcherInterface
25+
{
26+
/**
27+
* {@inheritdoc}
28+
*/
29+
public function fetch($url)
30+
{
31+
return file_get_contents($url);
32+
}
33+
}

cmfcmf/OpenWeatherMap/WeatherForecast.php renamed to Cmfcmf/OpenWeatherMap/Forecast.php

+15-15
Original file line numberDiff line numberDiff line change
@@ -14,40 +14,40 @@
1414
* @see http://openweathermap.org/appid
1515
*/
1616

17-
namespace cmfcmf\OpenWeatherMap;
17+
namespace Cmfcmf\OpenWeatherMap;
1818

19-
use cmfcmf\OpenWeatherMap,
20-
cmfcmf\OpenWeatherMap\Util\City,
21-
cmfcmf\OpenWeatherMap\Util\Sun,
22-
cmfcmf\OpenWeatherMap\Util\Temperature,
23-
cmfcmf\OpenWeatherMap\Util\Unit,
24-
cmfcmf\OpenWeatherMap\Util\Weather as WeatherObj,
25-
cmfcmf\OpenWeatherMap\Util\Wind,
26-
cmfcmf\OpenWeatherMap\Util\Time;
19+
use Cmfcmf\OpenWeatherMap;
20+
use Cmfcmf\OpenWeatherMap\Util\City;
21+
use Cmfcmf\OpenWeatherMap\Util\Sun;
22+
use Cmfcmf\OpenWeatherMap\Util\Temperature;
23+
use Cmfcmf\OpenWeatherMap\Util\Unit;
24+
use Cmfcmf\OpenWeatherMap\Util\Weather as WeatherObj;
25+
use Cmfcmf\OpenWeatherMap\Util\Wind;
26+
use Cmfcmf\OpenWeatherMap\Util\Time;
2727

28-
class WeatherForecast extends OpenWeatherMap\Weather
28+
/**
29+
* Class Forecast.
30+
*/
31+
class Forecast extends CurrentWeather
2932
{
3033
/**
3134
* @var Time The time of the forecast.
32-
*
33-
* @see cmfcmf\OpenWeatherMap\Util\Time The time object.
3435
*/
3536
public $time;
3637

3738
/**
3839
* Create a new weather object for forecasts.
3940
* @param \SimpleXMLElement $xml The forecasts xml.
4041
* @param string $units Ths units used.
41-
* @param string $lang The language used.
4242
*
4343
* @internal
4444
*/
45-
public function __construct($xml, $units = 'imperial', $lang = 'en')
45+
public function __construct($xml, $units)
4646
{
4747
$this->city = new City($xml->city['id'], $xml->city['name'], $xml->city->coord['lon'], $xml->city->coord['lat'], $xml->city->country);
4848

4949
if ($units == 'metric') {
50-
$temperatureUnit = '°C';
50+
$temperatureUnit = "\xB0C";
5151
} else {
5252
$temperatureUnit = 'F';
5353
}

0 commit comments

Comments
 (0)