@@ -87,31 +87,51 @@ class CurrentWeather
87
87
/**
88
88
* Create a new weather object.
89
89
*
90
- * @param \SimpleXMLElement $xml
91
- * @param string $units
90
+ * @param mixed $data
91
+ * @param string $units
92
92
*
93
93
* @internal
94
94
*/
95
- public function __construct (\ SimpleXMLElement $ xml , $ units )
95
+ public function __construct ($ data , $ units )
96
96
{
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.
103
98
if ($ units == 'metric ' ) {
104
99
$ windSpeedUnit = 'm/s ' ;
105
100
} else {
106
101
$ windSpeedUnit = 'mph ' ;
107
102
}
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 ' ]));
109
103
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 ' ]);
112
104
$ 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
+ } else {
118
+ $ this ->city = new City ($ data ->id , $ data ->name , $ data ->coord ->lon , $ data ->coord ->lat , $ data ->sys ->country );
119
+ $ this ->temperature = new Temperature (new Unit ($ data ->main ->temp , $ units ), new Unit ($ data ->main ->temp_min , $ units ), new Unit ($ data ->main ->temp_max , $ units ));
120
+ $ this ->humidity = new Unit ($ data ->main ->humidity , '% ' );
121
+ $ this ->pressure = new Unit ($ data ->main ->pressure , 'hPa ' );
122
+ $ this ->wind = new Wind (new Unit ($ data ->wind ->speed , $ windSpeedUnit ), new Unit ($ data ->wind ->deg ));
123
+ $ this ->clouds = new Unit ($ data ->clouds ->all , '% ' );
124
+
125
+ // the rain field is not always present in the JSON response
126
+ // and sometimes it contains the field '1h', sometimes the field '3h'
127
+ $ rain = isset ($ data ->rain ) ? (array ) $ data ->rain : [];
128
+ $ rainUnit = !empty ($ rain ) ? key ($ rain ) : '' ;
129
+ $ rainValue = !empty ($ rain ) ? current ($ rain ) : 0.0 ;
130
+ $ this ->precipitation = new Unit ($ rainValue , $ rainUnit );
131
+
132
+ $ this ->sun = new Sun (\DateTime::createFromFormat ('U ' , $ data ->sys ->sunrise , $ utctz ), \DateTime::createFromFormat ('U ' , $ data ->sys ->sunset , $ utctz ));
133
+ $ this ->weather = new WeatherObj ($ data ->weather [0 ]->id , $ data ->weather [0 ]->description , $ data ->weather [0 ]->icon );
134
+ $ this ->lastUpdate = \DateTime::createFromFormat ('U ' , $ data ->dt , $ utctz );
135
+ }
116
136
}
117
137
}
0 commit comments