14
14
* @see http://openweathermap.org/appid
15
15
*/
16
16
17
- namespace cmfcmf \OpenWeatherMap ;
17
+ namespace Cmfcmf \OpenWeatherMap ;
18
18
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 ;
27
26
28
27
/**
29
- * Weather class returned by {@link OpenWeatherMap::getWeather()} .
28
+ * Weather class used to hold the current weather data .
30
29
*/
31
- class Weather
30
+ class CurrentWeather
32
31
{
33
32
/**
34
33
* The city object.
@@ -84,62 +83,21 @@ class Weather
84
83
*/
85
84
public $ lastUpdate ;
86
85
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
-
96
86
/**
97
87
* 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
104
88
*
105
- * @throws OWMException If OpenWeatherMap returns an error.
106
- * @throws \Exception If the parameters are invalid.
89
+ * @param $xml
90
+ * @param string $units
107
91
*
108
92
* @internal
109
93
*/
110
- public function __construct ($ query , $ units = ' imperial ' , $ lang = ' en ' , $ appid = '' , $ cacheClass = false , $ seconds = 600 )
94
+ public function __construct ($ xml , $ units )
111
95
{
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
-
137
96
$ this ->city = new City ($ xml ->city ['id ' ], $ xml ->city ['name ' ], $ xml ->city ->coord ['lon ' ], $ xml ->city ->coord ['lat ' ], $ xml ->city ->country );
138
97
$ 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 ' ]));
139
98
$ this ->humidity = new Unit ($ xml ->humidity ['value ' ], $ xml ->humidity ['unit ' ]);
140
99
$ this ->pressure = new Unit ($ xml ->pressure ['value ' ], $ xml ->pressure ['unit ' ]);
141
-
142
-
100
+
143
101
// This is kind of a hack, because the units are missing in the xml document.
144
102
if ($ units == 'metric ' ) {
145
103
$ windSpeedUnit = 'm/s ' ;
@@ -148,7 +106,6 @@ public function __construct($query, $units = 'imperial', $lang = 'en', $appid =
148
106
}
149
107
$ 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 ' ]));
150
108
151
-
152
109
$ this ->clouds = new Unit ($ xml ->clouds ['value ' ], null , $ xml ->clouds ['name ' ]);
153
110
$ this ->precipitation = new Unit ($ xml ->precipitation ['value ' ], $ xml ->precipitation ['unit ' ], $ xml ->precipitation ['mode ' ]);
154
111
$ this ->sun = new Sun (new \DateTime ($ xml ->city ->sun ['rise ' ]), new \DateTime ($ xml ->city ->sun ['set ' ]));
0 commit comments