@@ -87,31 +87,53 @@ class CurrentWeather
8787 /**
8888 * Create a new weather object.
8989 *
90- * @param \SimpleXMLElement $xml
91- * @param string $units
90+ * @param mixed $data
91+ * @param string $units
9292 *
9393 * @internal
9494 */
95- public function __construct (\ SimpleXMLElement $ xml , $ units )
95+ public function __construct ($ data , $ units )
9696 {
97- $ this ->city = new City ($ xml ->city ['id ' ], $ xml ->city ['name ' ], $ xml ->city ->coord ['lon ' ], $ xml ->city ->coord ['lat ' ], $ xml ->city ->country );
98- $ 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 ' ]));
99- $ this ->humidity = new Unit ($ xml ->humidity ['value ' ], $ xml ->humidity ['unit ' ]);
100- $ this ->pressure = new Unit ($ xml ->pressure ['value ' ], $ xml ->pressure ['unit ' ]);
101-
102- // This is kind of a hack, because the units are missing in the xml document.
97+ // This is kind of a hack, because the units are missing in the document.
10398 if ($ units == 'metric ' ) {
10499 $ windSpeedUnit = 'm/s ' ;
105100 } else {
106101 $ windSpeedUnit = 'mph ' ;
107102 }
108- $ 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 ' ]));
109103
110- $ this ->clouds = new Unit ($ xml ->clouds ['value ' ], null , $ xml ->clouds ['name ' ]);
111- $ this ->precipitation = new Unit ($ xml ->precipitation ['value ' ], $ xml ->precipitation ['unit ' ], $ xml ->precipitation ['mode ' ]);
112104 $ utctz = new \DateTimeZone ('UTC ' );
113- $ this ->sun = new Sun (new \DateTime ($ xml ->city ->sun ['rise ' ], $ utctz ), new \DateTime ($ xml ->city ->sun ['set ' ], $ utctz ));
114- $ this ->weather = new WeatherObj ($ xml ->weather ['number ' ], $ xml ->weather ['value ' ], $ xml ->weather ['icon ' ]);
115- $ this ->lastUpdate = new \DateTime ($ xml ->lastupdate ['value ' ], $ utctz );
105+
106+ if ($ data instanceof \SimpleXMLElement) {
107+ $ this ->city = new City ($ data ->city ['id ' ], $ data ->city ['name ' ], $ data ->city ->coord ['lon ' ], $ data ->city ->coord ['lat ' ], $ data ->city ->country );
108+ $ this ->temperature = new Temperature (new Unit ($ data ->temperature ['value ' ], $ data ->temperature ['unit ' ]), new Unit ($ data ->temperature ['min ' ], $ data ->temperature ['unit ' ]), new Unit ($ data ->temperature ['max ' ], $ data ->temperature ['unit ' ]));
109+ $ this ->humidity = new Unit ($ data ->humidity ['value ' ], $ data ->humidity ['unit ' ]);
110+ $ this ->pressure = new Unit ($ data ->pressure ['value ' ], $ data ->pressure ['unit ' ]);
111+ $ this ->wind = new Wind (new Unit ($ data ->wind ->speed ['value ' ], $ windSpeedUnit , $ data ->wind ->speed ['name ' ]), new Unit ($ data ->wind ->direction ['value ' ], $ data ->wind ->direction ['code ' ], $ data ->wind ->direction ['name ' ]));
112+ $ this ->clouds = new Unit ($ data ->clouds ['value ' ], null , $ data ->clouds ['name ' ]);
113+ $ this ->precipitation = new Unit ($ data ->precipitation ['value ' ], $ data ->precipitation ['unit ' ], $ data ->precipitation ['mode ' ]);
114+ $ this ->sun = new Sun (new \DateTime ($ data ->city ->sun ['rise ' ], $ utctz ), new \DateTime ($ data ->city ->sun ['set ' ], $ utctz ));
115+ $ this ->weather = new WeatherObj ($ data ->weather ['number ' ], $ data ->weather ['value ' ], $ data ->weather ['icon ' ]);
116+ $ this ->lastUpdate = new \DateTime ($ data ->lastupdate ['value ' ], $ utctz );
117+
118+ } else {
119+ $ this ->city = new City ($ data ->id , $ data ->name , $ data ->coord ->lon , $ data ->coord ->lat , $ data ->sys ->country );
120+ $ this ->temperature = new Temperature (new Unit ($ data ->main ->temp , $ units ), new Unit ($ data ->main ->temp_min , $ units ), new Unit ($ data ->main ->temp_max , $ units ));
121+ $ this ->humidity = new Unit ($ data ->main ->humidity , '% ' );
122+ $ this ->pressure = new Unit ($ data ->main ->pressure , 'hPa ' );
123+ $ this ->wind = new Wind (new Unit ($ data ->wind ->speed , $ windSpeedUnit ), new Unit ($ data ->wind ->deg ));
124+ $ this ->clouds = new Unit ($ data ->clouds ->all , '% ' );
125+
126+ // the rain field is not always present in the JSON response
127+ // and sometimes it contains the field '1h', sometimes the field '3h'
128+ $ rain = isset ($ data ->rain ) ? (array ) $ data ->rain : [];
129+ $ rainUnit = !empty ($ rain ) ? key ($ rain ) : '' ;
130+ $ rainValue = !empty ($ rain ) ? current ($ rain ) : 0.0 ;
131+ $ this ->precipitation = new Unit ($ rainValue , $ rainUnit );
132+
133+ $ this ->sun = new Sun (\DateTime::createFromFormat ('U ' , $ data ->sys ->sunrise , $ utctz ), \DateTime::createFromFormat ('U ' , $ data ->sys ->sunset , $ utctz ));
134+ $ this ->weather = new WeatherObj ($ data ->weather [0 ]->id , $ data ->weather [0 ]->description , $ data ->weather [0 ]->icon );
135+ $ this ->lastUpdate = \DateTime::createFromFormat ('U ' , $ data ->dt , $ utctz );
136+ }
116137 }
138+
117139}
0 commit comments