-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathForecastResponse.php
251 lines (226 loc) · 7.26 KB
/
ForecastResponse.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
<?php
/**
* ForecastResponse.php
*/
/**
* ForecastTools: Response
*
* A ForecastResponse object is used to access the various data blocks returned
* from Forecast.io for a given request. In general, to determine the weather
* at a given point in time, one should examine the highest-precision data block
* defined (minutely, hourly, and daily respectively), taking any data available
* from from it and falling back to the next-highest precision data block for
* any properties that are missing for the point in time desired.
*
* @package ForecastTools
* @author Charlie Gorichanaz <[email protected]>
* @license http://opensource.org/licenses/MIT The MIT License
* @version 1.0
* @link http://github.com/CNG/ForecastTools
* @example ../example.php
*/
class ForecastResponse
{
private $_response;
/**
* Create ForecastResponse object
*
* @param object $response Entire JSON decoded response from API
*/
public function __construct($response)
{
$this->_response = $response;
}
/**
* Get a JSON formatted object that is the entire response from Forecast.io.
* This is useful if you do not wish to use any of the get methods provided
* by this class or for accessing new or otherwise not otherwise accessible
* data in the response.
*
* @return Object JSON-formatted object with the following properties defined:
* latitude, longitude, timezone, offset, currently[, minutely, hourly, daily,
* alerts, flags]
*/
public function getRawData()
{
return $this->_response;
}
/**
* The requested latitude.
*
* @return float The requested latitude
*/
public function getLatitude()
{
$field = 'latitude';
return property_exists($this->_response->$field);
}
/**
* The requested longitude.
*
* @return float The requested longitude
*/
public function getLongitude()
{
$field = 'longitude';
return property_exists($this->_response->$field);
}
/**
* The IANA timezone name for the requested location (e.g. America/New_York).
* This is the timezone used for text forecast summaries and for determining
* the exact start time of daily data points. (Developers are advised to rely
* on local system settings rather than this value if at all possible: users
* may deliberately set an unusual timezone, and furthermore are likely to
* know what they actually want better than our timezone database does.)
*
* @return string The IANA timezone name for the requested location
*/
public function getTimezone()
{
$field = 'timezone';
return property_exists($this->_response->$field);
}
/**
* The current timezone offset in hours from GMT.
*
* @return string The current timezone offset in hours from GMT.
*/
public function getOffset()
{
$field = 'offset';
return property_exists($this->_response->$field);
}
/**
* Get number of ForecastDataPoint objects that exist within specified block
*
* @param string $type Type of data block
*
* @return int Returns number of ForecastDataPoint objects that exist within
* specified block
*/
public function getCount($type)
{
$response = $this->_response;
return empty($response->$type->data) ? false : count($response->$type->data);
}
/**
* Get ForecastDataPoint object for current or specified time
*
* @return ForecastDataPoint ForecastDataPoint object for current or specified time
*/
public function getCurrently()
{
include_once 'ForecastDataPoint.php';
return new ForecastDataPoint($this->_response->currently);
}
/**
* Get ForecastDataPoint object(s) desired within the specified block
*
* @param string $type Type of data block (
* @param int $index Optional numeric index of desired data point in block
* beginning with 0
*
* @return array|ForecastDataPoint|bool Returns an array of ForecastDataPoint
* objects within the block OR a single ForecastDataPoint object for specified
* block OR false if no applicable block
*/
private function _getBlock($type, $index = null)
{
if ($this->getCount($type)) {
include_once 'ForecastDataPoint.php';
$block_data = $this->_response->$type->data;
if (is_null($index)) {
$points = array();
foreach ($block_data as $point_data) {
$points[] = new ForecastDataPoint($point_data);
}
return $points;
} elseif (is_int($index) && $this->getCount($type) > $index) {
return new ForecastDataPoint($block_data[$index]);
}
}
return false; // if no block, block but no data, or invalid index specified
}
/**
* Get ForecastDataPoint object(s) desired within the minutely block, which is
* weather conditions minute-by-minute for the next hour.
*
* @param int $index Optional numeric index of desired data point in block
* beginning with 0
*
* @return array|ForecastDataPoint|bool Returns an array of ForecastDataPoint
* objects within the block OR a single ForecastDataPoint object for specified
* block OR false if no applicable block
*/
public function getMinutely($index = null)
{
$type = 'minutely';
return $this->_getBlock($type, $index);
}
/**
* Get ForecastDataPoint object(s) desired within the hourly block, which is
* weather conditions hour-by-hour for the next two days.
*
* @param int $index Optional numeric index of desired data point in block
* beginning with 0
*
* @return array|ForecastDataPoint|bool Returns an array of ForecastDataPoint
* objects within the block OR a single ForecastDataPoint object for specified
* block OR false if no applicable block
*/
public function getHourly($index = null)
{
$type = 'hourly';
return $this->_getBlock($type, $index);
}
/**
* Get ForecastDataPoint object(s) desired within the daily block, which is
* weather conditions day-by-day for the next week.
*
* @param int $index Optional numeric index of desired data point in block
* beginning with 0
*
* @return array|ForecastDataPoint|bool Returns an array of ForecastDataPoint
* objects within the block OR a single ForecastDataPoint object for specified
* block OR false if no applicable block
*/
public function getDaily($index = null)
{
$type = 'daily';
return $this->_getBlock($type, $index);
}
/**
* Get an array of ForecastAlert objects, which, if present, contain any
* severe weather alerts, issued by a governmental weather authority,
* pertinent to the requested location.
*
* @return array|bool Array of ForecastAlert objects OR false if none
*/
public function getAlerts()
{
if (!empty($this->_response->alert)) {
include_once 'ForecastAlert.php';
$alerts = array();
foreach ($this->_response->alert as $alert) {
$alerts[] = new ForecastAlert($alert);
}
return $alerts;
} else {
return false;
}
}
/**
* Get ForecastFlags object of miscellaneous metadata concerning this request.
*
* @return ForecastFlags|bool ForecastFlags object OR false if none
*/
public function getFlags()
{
if (!empty($this->_response->flags)) {
include_once 'ForecastFlags.php';
return new ForecastFlags($this->_response->flags);
} else {
return false;
}
}
}