Skip to content

Commit a856e3e

Browse files
committed
add UVI API feature
1 parent a5f0e05 commit a856e3e

File tree

7 files changed

+328
-3
lines changed

7 files changed

+328
-3
lines changed

.travis.yml

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,6 @@ php:
1414

1515
matrix:
1616
fast_finish: true
17-
allow_failures:
18-
- php: nightly
1917

2018
before_install:
2119
- if [[ ! $TRAVIS_PHP_VERSION = hhvm* ]]; then phpenv config-rm xdebug.ini || echo "xdebug not available"; fi

Cmfcmf/OpenWeatherMap.php

Lines changed: 142 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919

2020
use Cmfcmf\OpenWeatherMap\AbstractCache;
2121
use Cmfcmf\OpenWeatherMap\CurrentWeather;
22+
use Cmfcmf\OpenWeatherMap\CurrentUvi;
2223
use Cmfcmf\OpenWeatherMap\CurrentWeatherGroup;
2324
use Cmfcmf\OpenWeatherMap\Exception as OWMException;
2425
use Cmfcmf\OpenWeatherMap\Fetcher\CurlFetcher;
@@ -67,6 +68,16 @@ class OpenWeatherMap
6768
*/
6869
private $weatherHistoryUrl = 'http://history.openweathermap.org/data/2.5/history/city?';
6970

71+
/**
72+
* @var string The basic api url to fetch current uv data from.
73+
*/
74+
private $uviUrl = 'http://api.openweathermap.org/v3/uvi/%s,%s/current.json?';
75+
76+
/**
77+
* @var string The basic api url to fetch current uv data from.
78+
*/
79+
private $uviHistoryUrl = 'http://api.openweathermap.org/v3/uvi/%s,%s/%s.json?';
80+
7081
/**
7182
* @var AbstractCache|bool $cache The cache to use.
7283
*/
@@ -436,6 +447,109 @@ public function getRawWeatherHistory($query, \DateTime $start, $endOrCount = 1,
436447
return $this->cacheOrFetchResult($url);
437448
}
438449

450+
/**
451+
* Directly returns the json string returned by OpenWeatherMap for the UVI data.
452+
*
453+
* @param array $query The place to get information as follows: [latitude, longitude, date time]. For possible values see ::getWeather.
454+
* @param string $appid Your app id, default ''. See http://openweathermap.org/appid for more details.
455+
*
456+
* @throws \InvalidArgumentException
457+
*
458+
* @return string Returns false on failure and the fetched data in the format you specified on success.
459+
*
460+
* Warning If an error occurred, OpenWeatherMap ALWAYS returns data in json format.
461+
*
462+
* @api
463+
*/
464+
public function getRawUviData($query, $appid = '')
465+
{
466+
if (!is_array($query)) {
467+
throw new \InvalidArgumentException('$query must get information is as follows: [latitude, longitude]');
468+
} elseif(count($query) != 2) {
469+
throw new \InvalidArgumentException('$query must get information is as follows: [latitude, longitude]');
470+
} else {
471+
$url = $this->buildUviUrl($query, $appid);
472+
}
473+
474+
return $this->cacheOrFetchResult($url);
475+
}
476+
477+
/**
478+
* Directly returns the json string returned by OpenWeatherMap for the UVI history data.
479+
*
480+
* @param array|int|string $query The place to get weather information for. For possible values see ::getWeather.
481+
* @param string $appid Your app id, default ''. See http://openweathermap.org/appid for more details.
482+
*
483+
* @throws \InvalidArgumentException
484+
*
485+
* @return string Returns false on failure and the fetched data in the format you specified on success.
486+
*
487+
* Warning If an error occurred, OpenWeatherMap ALWAYS returns data in json format.
488+
*
489+
* @api
490+
*/
491+
public function getRawUviHistory($query, $appid = '')
492+
{
493+
if (!is_array($query)) {
494+
throw new \InvalidArgumentException('$query must get information is as follows: [latitude, longitude, ISO 8601 date format]');
495+
} elseif(count($query) != 3) {
496+
throw new \InvalidArgumentException('$query must get information is as follows: [latitude, longitude, ISO 8601 date format]');
497+
} else {
498+
$url = $this->buildUviUrl($query, $appid);
499+
}
500+
501+
return $this->cacheOrFetchResult($url);
502+
}
503+
504+
/**
505+
* Returns the current uvi at the location you specified.
506+
*
507+
* @param array|int|string $query The place to get weather information for. For possible values see below.
508+
* @param string $appid Your app id, default ''. See http://openweathermap.org/appid for more details.
509+
*
510+
* @throws OpenWeatherMap\Exception If OpenWeatherMap returns an error.
511+
* @throws \InvalidArgumentException If an argument error occurs.
512+
*
513+
* @return CurrentUvi The uvi object.
514+
*
515+
* There are three ways to specify the place to get weather information for:
516+
* - Use the coordinates: $query must be an associative array containing the 'lat' and 'lon' values.
517+
*
518+
* @api
519+
*/
520+
public function getUvi($query, $appid = '')
521+
{
522+
$answer = $this->getRawUviData($query, $appid);
523+
$json = $this->parseJson($answer);
524+
525+
return new CurrentUvi($json);
526+
}
527+
528+
/**
529+
* Returns the history uvi at the location you specified.
530+
*
531+
* @param array|int|string $query The place to get weather information for. For possible values see below.
532+
* @param string $appid Your app id, default ''. See http://openweathermap.org/appid for more details.
533+
* @param string $dateTime Your date time, default ''. See http://openweathermap.org/api/uvi for more details about date format.
534+
*
535+
* @throws OpenWeatherMap\Exception If OpenWeatherMap returns an error.
536+
* @throws \InvalidArgumentException If an argument error occurs.
537+
*
538+
* @return CurrentUvi The uvi object.
539+
*
540+
* There are three ways to specify the place to get weather information for:
541+
* - Use the coordinates: $query must be an associative array containing the 'lat' and 'lon' values.
542+
*
543+
* @api
544+
*/
545+
public function getUviHistory($query, $appid = '')
546+
{
547+
$answer = $this->getRawUviHistory($query, $appid);
548+
$json = $this->parseJson($answer);
549+
550+
return new CurrentUvi($json);
551+
}
552+
439553
/**
440554
* Returns whether or not the last result was fetched from the cache.
441555
*
@@ -503,6 +617,34 @@ private function buildUrl($query, $units, $lang, $appid, $mode, $url)
503617
return $url;
504618
}
505619

620+
/**
621+
* Build the url to fetch UVI data from.
622+
*
623+
* @param $query
624+
* @param $units
625+
* @param $lang
626+
* @param $appid
627+
* @param $mode
628+
* @param string $url The url to prepend.
629+
*
630+
* @return bool|string The fetched url, false on failure.
631+
*/
632+
private function buildUviUrl($query, $appid)
633+
{
634+
$queryLength = count($query);
635+
switch ($queryLength) {
636+
case 2:
637+
$queryUrl = sprintf($this->uviUrl, $query[0], $query[1]);
638+
break;
639+
case 3:
640+
$queryUrl = sprintf($this->uviHistoryUrl, $query[0], $query[1], $query[2]);
641+
break;
642+
}
643+
$queryUrl .= 'APPID=';
644+
645+
return $queryUrl .= empty($appid) ? $this->apiKey : $appid;
646+
}
647+
506648
/**
507649
* Builds the query string for the url.
508650
*
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
<?php
2+
/**
3+
* OpenWeatherMap-PHP-API — A php api to parse weather data from http://www.OpenWeatherMap.org .
4+
*
5+
* @license MIT
6+
*
7+
* Please see the LICENSE file distributed with this source code for further
8+
* information regarding copyright and licensing.
9+
*
10+
* Please visit the following links to read about the usage policies and the license of
11+
* OpenWeatherMap before using this class:
12+
*
13+
* @see http://www.OpenWeatherMap.org
14+
* @see http://www.OpenWeatherMap.org/terms
15+
* @see http://openweathermap.org/appid
16+
*/
17+
18+
namespace Cmfcmf\OpenWeatherMap;
19+
20+
use Cmfcmf\OpenWeatherMap\Util\Uvi;
21+
22+
/**
23+
* Weather class used to hold the current weather data.
24+
*/
25+
class CurrentUvi
26+
{
27+
/**
28+
* The city object.
29+
*
30+
* @var Util\Uvi
31+
*/
32+
public $uvi;
33+
34+
/**
35+
* Create a new uvi object.
36+
*
37+
* @param mixed $data
38+
*
39+
* @internal
40+
*/
41+
public function __construct($data)
42+
{
43+
// generate the object from response JSON.
44+
// ($time, $latitude, $longitude, $data)
45+
if (empty($data->message) && empty($data->code)) {
46+
$this->uvi = new Uvi($data->time, $data->location->latitude, $data->location->longitude, $data->data);
47+
} else {
48+
$this->uvi = null;
49+
}
50+
}
51+
}

Cmfcmf/OpenWeatherMap/Util/Uvi.php

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
<?php
2+
/**
3+
* OpenWeatherMap-PHP-API — A php api to parse weather data from http://www.OpenWeatherMap.org .
4+
*
5+
* @license MIT
6+
*
7+
* Please see the LICENSE file distributed with this source code for further
8+
* information regarding copyright and licensing.
9+
*
10+
* Please visit the following links to read about the usage policies and the license of
11+
* OpenWeatherMap before using this class:
12+
*
13+
* @see http://www.OpenWeatherMap.org
14+
* @see http://www.OpenWeatherMap.org/terms
15+
* @see http://openweathermap.org/appid
16+
*/
17+
18+
namespace Cmfcmf\OpenWeatherMap\Util;
19+
20+
/**
21+
* The city class representing a city object.
22+
*/
23+
class Uvi
24+
{
25+
/**
26+
* @var string The date time.
27+
*/
28+
public $time;
29+
30+
/**
31+
* @var float The latitude.
32+
*/
33+
public $latitude;
34+
35+
/**
36+
* @var float The longitude.
37+
*/
38+
public $longitude;
39+
40+
/**
41+
* @var float The UVI data.
42+
*/
43+
public $data;
44+
45+
/**
46+
* Create a new city object.
47+
*
48+
* @param string $time The current time or time slot.
49+
* @param float $latitude The name of the city.
50+
* @param float $longitude The longitude of the city.
51+
* @param float $data The UVI value.
52+
*
53+
* @internal
54+
*/
55+
public function __construct($time, $latitude, $longitude, $data)
56+
{
57+
$this->time = $time;
58+
$this->latitude = (float)$latitude;
59+
$this->longitude = (float)$longitude;
60+
$this->data = (float)$data;
61+
}
62+
}

tests/Exceptions/OpenWeatherMapExceptionTest.php

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,24 @@ public function testGetRawWeatherHistoryWithEndDateException()
116116
{
117117
$this->owm->getRawWeatherHistory('Berlin', new \DateTime('now'), 'wrong-endOrCount', 'hour', 'imperial', 'en', '');
118118
}
119+
120+
/**
121+
* @expectedException \InvalidArgumentException
122+
* @dataProvider uviExceptionDataProvider
123+
*/
124+
public function testGetRawUviWithQueryErrorException($query)
125+
{
126+
$this->owm->getRawUviData($query);
127+
}
128+
129+
/**
130+
* @expectedException \InvalidArgumentException
131+
* @dataProvider uviExceptionDataProvider
132+
*/
133+
public function testGetRawUviHistoryWithQueryErrorException($query)
134+
{
135+
$this->owm->getRawUviHistory($query);
136+
}
119137

120138
/**
121139
* @expectedException \InvalidArgumentException
@@ -161,4 +179,12 @@ public function testParseJsonException()
161179

162180
$method->invoke($this->owm, $answer);
163181
}
182+
183+
public function uviExceptionDataProvider()
184+
{
185+
return array(
186+
array('error-query-format'),
187+
array(array())
188+
);
189+
}
164190
}

tests/OpenWeatherMapTest.php

Lines changed: 37 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,15 +31,23 @@ class OpenWeatherMapTest extends \PHPUnit_Framework_TestCase
3131
*/
3232
protected $owm;
3333

34+
/**
35+
* @var OpenWeatherMap
36+
*/
37+
protected $openWeather;
38+
3439
/**
3540
* @var ExampleCacheTest
3641
*/
3742
protected $cache;
3843

3944
protected function setUp()
4045
{
41-
$this->apiKey = 'unicorn-rainbow';
46+
$ini = parse_ini_file(__DIR__.'/../Examples/ApiKey.ini');
47+
$myApiKey = $ini['api_key'];
48+
$this->apiKey = $myApiKey;
4249
$this->owm = new OpenWeatherMap($this->apiKey, new TestFetcher(), false, 600);
50+
$this->openWeather = new OpenWeatherMap($this->apiKey, null, false, 600);
4351
$this->cache = new ExampleCacheTest();
4452
}
4553

@@ -122,6 +130,34 @@ public function testGetWeatherForecast()
122130
$this->assertInstanceOf('\Cmfcmf\OpenWeatherMap\WeatherForecast', $maxDay);
123131
}
124132

133+
public function testGetUvi()
134+
{
135+
$weather = $this->openWeather;
136+
$locationArray = array('40.7', '-74.2');
137+
$result = $weather->getUvi($locationArray);
138+
139+
$this->assertInstanceOf('\Cmfcmf\OpenWeatherMap\CurrentUvi', $result);
140+
}
141+
142+
public function testGetUviHistory()
143+
{
144+
$weather = $this->openWeather;
145+
$year = date('Y') . 'Z';
146+
$locationArray = array('40.7', '-74.2', $year);
147+
$result = $weather->getUviHistory($locationArray);
148+
149+
$this->assertInstanceOf('\Cmfcmf\OpenWeatherMap\CurrentUvi', $result);
150+
}
151+
152+
public function testGetUviHistoryWithNull()
153+
{
154+
$weather = $this->openWeather;
155+
$locationArray = array('40.7', '-74.2', 'invalid-date-format');
156+
$result = $weather->getUviHistory($locationArray);
157+
158+
$this->assertNull($result->uvi);
159+
}
160+
125161
public function testGetDailyWeatherForecast()
126162
{
127163
$days = 16;

0 commit comments

Comments
 (0)