Skip to content

Commit 153dea9

Browse files
committed
Add back current uv index.
1 parent 44d3145 commit 153dea9

File tree

3 files changed

+112
-30
lines changed

3 files changed

+112
-30
lines changed

Cmfcmf/OpenWeatherMap.php

Lines changed: 76 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -309,6 +309,27 @@ public function getWeatherHistory($query, \DateTime $start, $endOrCount = 1, $ty
309309
return new WeatherHistory($xml, $query);
310310
}
311311

312+
/**
313+
* Returns the current uv index at the location you specified.
314+
*
315+
* @param float $lat The location's latitude.
316+
* @param float $lon The location's longitude.
317+
*
318+
* @throws OpenWeatherMap\Exception If OpenWeatherMap returns an error.
319+
* @throws \InvalidArgumentException If an argument error occurs.
320+
*
321+
* @return UVIndex The uvi object.
322+
*
323+
* @api
324+
*/
325+
public function getCurrentUVIndex($lat, $lon)
326+
{
327+
$answer = $this->getRawCurrentUVIndexData($lat, $lon);
328+
$json = $this->parseJson($answer);
329+
330+
return new UVIndex($json);
331+
}
332+
312333
/**
313334
* Returns the uv index at date, time and location you specified.
314335
*
@@ -468,7 +489,30 @@ public function getRawWeatherHistory($query, \DateTime $start, $endOrCount = 1,
468489
}
469490

470491
/**
471-
* Directly returns the json string returned by OpenWeatherMap for the UVI data.
492+
* Directly returns the json string returned by OpenWeatherMap for the current UV index data.
493+
*
494+
* @param float $lat The location's latitude.
495+
* @param float $lon The location's longitude.
496+
*
497+
* @return bool|string Returns the fetched data.
498+
*
499+
* @api
500+
*/
501+
public function getRawCurrentUVIndexData($lat, $lon)
502+
{
503+
if (!$this->apiKey) {
504+
throw new \RuntimeException('Before using this method, you must set the api key using ->setApiKey()');
505+
}
506+
if (!is_float($lat) || !is_float($lon)) {
507+
throw new \InvalidArgumentException('$lat and $lon must be floating point numbers');
508+
}
509+
$url = $this->buildUVIndexUrl($lat, $lon);
510+
511+
return $this->cacheOrFetchResult($url);
512+
}
513+
514+
/**
515+
* Directly returns the json string returned by OpenWeatherMap for the UV index data.
472516
*
473517
* @param float $lat The location's latitude.
474518
* @param float $lon The location's longitude.
@@ -572,36 +616,40 @@ private function buildUrl($query, $units, $lang, $appid, $mode, $url)
572616
*
573617
* @return string
574618
*/
575-
private function buildUVIndexUrl($lat, $lon, $dateTime, $timePrecision)
619+
private function buildUVIndexUrl($lat, $lon, $dateTime = null, $timePrecision = null)
576620
{
577-
$format = '\Z';
578-
switch ($timePrecision) {
579-
/** @noinspection PhpMissingBreakStatementInspection */
580-
case 'second':
581-
$format = ':s' . $format;
582-
/** @noinspection PhpMissingBreakStatementInspection */
583-
case 'minute':
584-
$format = ':i' . $format;
585-
/** @noinspection PhpMissingBreakStatementInspection */
586-
case 'hour':
587-
$format = '\TH' . $format;
588-
/** @noinspection PhpMissingBreakStatementInspection */
589-
case 'day':
590-
$format = '-d' . $format;
591-
/** @noinspection PhpMissingBreakStatementInspection */
592-
case 'month':
593-
$format = '-m' . $format;
594-
case 'year':
595-
$format = 'Y' . $format;
596-
break;
597-
default:
598-
throw new \InvalidArgumentException('$timePrecision is invalid.');
621+
if ($dateTime !== null) {
622+
$format = '\Z';
623+
switch ($timePrecision) {
624+
/** @noinspection PhpMissingBreakStatementInspection */
625+
case 'second':
626+
$format = ':s' . $format;
627+
/** @noinspection PhpMissingBreakStatementInspection */
628+
case 'minute':
629+
$format = ':i' . $format;
630+
/** @noinspection PhpMissingBreakStatementInspection */
631+
case 'hour':
632+
$format = '\TH' . $format;
633+
/** @noinspection PhpMissingBreakStatementInspection */
634+
case 'day':
635+
$format = '-d' . $format;
636+
/** @noinspection PhpMissingBreakStatementInspection */
637+
case 'month':
638+
$format = '-m' . $format;
639+
case 'year':
640+
$format = 'Y' . $format;
641+
break;
642+
default:
643+
throw new \InvalidArgumentException('$timePrecision is invalid.');
644+
}
645+
// OWM only accepts UTC timezones.
646+
$dateTime->setTimezone(new \DateTimeZone('UTC'));
647+
$dateTime = $dateTime->format($format);
648+
} else {
649+
$dateTime = 'current';
599650
}
600-
// OWM only accepts UTC timezones.
601-
$dateTime->setTimezone(new \DateTimeZone('UTC'));
602651

603-
$url = sprintf($this->uvIndexUrl . '/%s,%s/%s.json?appid=%s', $lat, $lon, $dateTime->format($format), $this->apiKey);
604-
return $url;
652+
return sprintf($this->uvIndexUrl . '/%s,%s/%s.json?appid=%s', $lat, $lon, $dateTime, $this->apiKey);
605653
}
606654

607655
/**

tests/Exceptions/OpenWeatherMapExceptionTest.php

Lines changed: 29 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -119,13 +119,22 @@ public function testGetRawWeatherHistoryWithEndDateException()
119119

120120
/**
121121
* @expectedException \InvalidArgumentException
122-
* @dataProvider uviExceptionDataProvider
122+
* @dataProvider uvIndexExceptionDataProvider
123123
*/
124124
public function testGetRawUVIndexWithQueryErrorException($lat, $lon, $dateTime)
125125
{
126126
$this->owm->getRawUVIndexData($lat, $lon, $dateTime);
127127
}
128128

129+
/**
130+
* @expectedException \InvalidArgumentException
131+
* @dataProvider currentUVIndexExceptionDataProvider
132+
*/
133+
public function testGetRawCurrentUVIndexWithQueryErrorException($lat, $lon)
134+
{
135+
$this->owm->getRawCurrentUVIndexData($lat, $lon);
136+
}
137+
129138
/**
130139
* @expectedException \RuntimeException
131140
*/
@@ -135,6 +144,15 @@ public function testGetRawUVIndexWithoutApiKey()
135144
$this->owm->getRawUVIndexData(1.1, 1.1, new \DateTime());
136145
}
137146

147+
/**
148+
* @expectedException \RuntimeException
149+
*/
150+
public function testGetRawCurrentUVIndexWithoutApiKey()
151+
{
152+
$this->owm->setApiKey(null);
153+
$this->owm->getRawCurrentUVIndexData(1.1, 1.1);
154+
}
155+
138156
/**
139157
* @expectedException \InvalidArgumentException
140158
*/
@@ -180,12 +198,21 @@ public function testParseJsonException()
180198
$method->invoke($this->owm, $answer);
181199
}
182200

183-
public function uviExceptionDataProvider()
201+
public function uvIndexExceptionDataProvider()
184202
{
185203
return array(
186204
array('error-query-format', 'foo', new \DateTime()),
187205
array(5.4, 1.2, 'foo'),
188206
array(5.4, 12, 'foo'),
189207
);
190208
}
209+
210+
public function currentUVIndexExceptionDataProvider()
211+
{
212+
return array(
213+
array('error-query-format', 'foo'),
214+
array(5.4, 12),
215+
array(5.4, '1.2'),
216+
);
217+
}
191218
}

tests/OpenWeatherMapTest.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -129,6 +129,13 @@ public function testGetWeatherForecast()
129129
$this->assertInstanceOf('\Cmfcmf\OpenWeatherMap\WeatherForecast', $maxDay);
130130
}
131131

132+
public function testGetCurrentUVIndex()
133+
{
134+
$owm = $this->openWeather;
135+
$result = $owm->getCurrentUVIndex(40.7, -74.2);
136+
$this->assertInstanceOf('\Cmfcmf\OpenWeatherMap\UVIndex', $result);
137+
}
138+
132139
public function testGetUVIndex()
133140
{
134141
$owm = $this->openWeather;

0 commit comments

Comments
 (0)