@@ -309,6 +309,27 @@ public function getWeatherHistory($query, \DateTime $start, $endOrCount = 1, $ty
309
309
return new WeatherHistory ($ xml , $ query );
310
310
}
311
311
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
+
312
333
/**
313
334
* Returns the uv index at date, time and location you specified.
314
335
*
@@ -468,7 +489,30 @@ public function getRawWeatherHistory($query, \DateTime $start, $endOrCount = 1,
468
489
}
469
490
470
491
/**
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.
472
516
*
473
517
* @param float $lat The location's latitude.
474
518
* @param float $lon The location's longitude.
@@ -572,36 +616,40 @@ private function buildUrl($query, $units, $lang, $appid, $mode, $url)
572
616
*
573
617
* @return string
574
618
*/
575
- private function buildUVIndexUrl ($ lat , $ lon , $ dateTime , $ timePrecision )
619
+ private function buildUVIndexUrl ($ lat , $ lon , $ dateTime = null , $ timePrecision = null )
576
620
{
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 ' ;
599
650
}
600
- // OWM only accepts UTC timezones.
601
- $ dateTime ->setTimezone (new \DateTimeZone ('UTC ' ));
602
651
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 );
605
653
}
606
654
607
655
/**
0 commit comments