Skip to content

Commit 4bbf1da

Browse files
committed
Swap $lat and $lon for Location and City.
1 parent 9af66d3 commit 4bbf1da

File tree

9 files changed

+58
-53
lines changed

9 files changed

+58
-53
lines changed

Cmfcmf/OpenWeatherMap.php

Lines changed: 43 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ class OpenWeatherMap
7171
/**
7272
* @var string The basic api url to fetch uv index data from.
7373
*/
74-
private $uviUrl = 'http://api.openweathermap.org/v3/uvi';
74+
private $uvIndexUrl = 'http://api.openweathermap.org/v3/uvi';
7575

7676
/**
7777
* @var AbstractCache|bool $cache The cache to use.
@@ -310,7 +310,7 @@ public function getWeatherHistory($query, \DateTime $start, $endOrCount = 1, $ty
310310
}
311311

312312
/**
313-
* Returns the uv index at the location you specified.
313+
* Returns the uv index at date, time and location you specified.
314314
*
315315
* @param float $lat The location's latitude.
316316
* @param float $lon The location's longitude.
@@ -324,9 +324,6 @@ public function getWeatherHistory($query, \DateTime $start, $endOrCount = 1, $ty
324324
*
325325
* @return UVIndex The uvi object.
326326
*
327-
* There are three ways to specify the place to get weather information for:
328-
* - Use the coordinates: $query must be an associative array containing the 'lat' and 'lon' values.
329-
*
330327
* @api
331328
*/
332329
public function getUVIndex($lat, $lon, $dateTime, $timePrecision = 'day')
@@ -495,33 +492,7 @@ public function getRawUVIndexData($lat, $lon, $dateTime, $timePrecision = 'day')
495492
if (interface_exists('DateTimeInterface') && !$dateTime instanceof \DateTimeInterface || !$dateTime instanceof \DateTime) {
496493
throw new \InvalidArgumentException('$dateTime must be an instance of \DateTime or \DateTimeInterface');
497494
}
498-
$format = '\Z';
499-
switch ($timePrecision) {
500-
/** @noinspection PhpMissingBreakStatementInspection */
501-
case 'second':
502-
$format = ':s' . $format;
503-
/** @noinspection PhpMissingBreakStatementInspection */
504-
case 'minute':
505-
$format = ':i' . $format;
506-
/** @noinspection PhpMissingBreakStatementInspection */
507-
case 'hour':
508-
$format = '\TH' . $format;
509-
/** @noinspection PhpMissingBreakStatementInspection */
510-
case 'day':
511-
$format = '-d' . $format;
512-
/** @noinspection PhpMissingBreakStatementInspection */
513-
case 'month':
514-
$format = '-m' . $format;
515-
case 'year':
516-
$format = 'Y' . $format;
517-
break;
518-
default:
519-
throw new \InvalidArgumentException('$timePrecision is invalid.');
520-
}
521-
// OWM only accepts UTC timezones.
522-
$dateTime->setTimezone(new \DateTimeZone('UTC'));
523-
524-
$url = sprintf($this->uviUrl . '/%s,%s/%s.json?appid=%s', $lat, $lon, $dateTime->format($format), $this->apiKey);
495+
$url = $this->buildUVIndexUrl($lat, $lon, $dateTime, $timePrecision);
525496

526497
return $this->cacheOrFetchResult($url);
527498
}
@@ -593,6 +564,46 @@ private function buildUrl($query, $units, $lang, $appid, $mode, $url)
593564
return $url;
594565
}
595566

567+
/**
568+
* @param float $lat
569+
* @param float $lon
570+
* @param \DateTime|\DateTimeImmutable $dateTime
571+
* @param string $timePrecision
572+
*
573+
* @return string
574+
*/
575+
private function buildUVIndexUrl($lat, $lon, $dateTime, $timePrecision)
576+
{
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.');
599+
}
600+
// OWM only accepts UTC timezones.
601+
$dateTime->setTimezone(new \DateTimeZone('UTC'));
602+
603+
$url = sprintf($this->uvIndexUrl . '/%s,%s/%s.json?appid=%s', $lat, $lon, $dateTime->format($format), $this->apiKey);
604+
return $url;
605+
}
606+
596607
/**
597608
* Builds the query string for the url.
598609
*

Cmfcmf/OpenWeatherMap/CurrentWeather.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,7 @@ public function __construct($data, $units)
103103
$utctz = new \DateTimeZone('UTC');
104104

105105
if ($data instanceof \SimpleXMLElement) {
106-
$this->city = new City($data->city['id'], $data->city['name'], $data->city->coord['lon'], $data->city->coord['lat'], $data->city->country);
106+
$this->city = new City($data->city['id'], $data->city['name'], $data->city->coord['lat'], $data->city->coord['lon'], $data->city->country);
107107
$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']));
108108
$this->humidity = new Unit($data->humidity['value'], $data->humidity['unit']);
109109
$this->pressure = new Unit($data->pressure['value'], $data->pressure['unit']);
@@ -114,7 +114,7 @@ public function __construct($data, $units)
114114
$this->weather = new Weather($data->weather['number'], $data->weather['value'], $data->weather['icon']);
115115
$this->lastUpdate = new \DateTime($data->lastupdate['value'], $utctz);
116116
} else {
117-
$this->city = new City($data->id, $data->name, $data->coord->lon, $data->coord->lat, $data->sys->country);
117+
$this->city = new City($data->id, $data->name, $data->coord->lat, $data->coord->lon, $data->sys->country);
118118
$this->temperature = new Temperature(new Unit($data->main->temp, $units), new Unit($data->main->temp_min, $units), new Unit($data->main->temp_max, $units));
119119
$this->humidity = new Unit($data->main->humidity, '%');
120120
$this->pressure = new Unit($data->main->pressure, 'hPa');

Cmfcmf/OpenWeatherMap/CurrentWeatherGroup.php

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,6 @@
1717

1818
namespace Cmfcmf\OpenWeatherMap;
1919

20-
use Cmfcmf\OpenWeatherMap;
21-
2220
/**
2321
* Class CurrentWeatherGroup used to hold the current weather data for a group of cities.
2422
*/

Cmfcmf/OpenWeatherMap/UVIndex.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ class UVIndex
4949
public function __construct($data)
5050
{
5151
$this->time = new \DateTime($data->time);
52-
$this->location = new Location($data->location->longitude, $data->location->latitude);
52+
$this->location = new Location($data->location->latitude, $data->location->longitude);
5353
$this->uvIndex = (float)$data->data;
5454
}
5555
}

Cmfcmf/OpenWeatherMap/Util/City.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -47,20 +47,20 @@ class City extends Location
4747
*
4848
* @param int $id The city id.
4949
* @param string $name The name of the city.
50-
* @param float $lon The longitude of the city.
5150
* @param float $lat The latitude of the city.
51+
* @param float $lon The longitude of the city.
5252
* @param string $country The abbreviation of the country the city is located in
5353
* @param int $population The city's population.
5454
*
5555
* @internal
5656
*/
57-
public function __construct($id, $name = null, $lon = null, $lat = null, $country = null, $population = null)
57+
public function __construct($id, $name = null, $lat = null, $lon = null, $country = null, $population = null)
5858
{
5959
$this->id = (int)$id;
6060
$this->name = isset($name) ? (string)$name : null;
6161
$this->country = isset($country) ? (string)$country : null;
6262
$this->population = isset($population) ? (int)$population : null;
6363

64-
parent::__construct($lon, $lat);
64+
parent::__construct($lat, $lon);
6565
}
6666
}

Cmfcmf/OpenWeatherMap/Util/Location.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -35,14 +35,14 @@ class Location
3535
/**
3636
* Create a new location object.
3737
*
38-
* @param float $lon The longitude of the city.
39-
* @param float $lat The latitude of the city.
38+
* @param float $lat The latitude of the city.
39+
* @param float $lon The longitude of the city.
4040
*
4141
* @internal
4242
*/
43-
public function __construct($lon = null, $lat = null)
43+
public function __construct($lat = null, $lon = null)
4444
{
45-
$this->lon = isset($lon) ? (float)$lon : null;
4645
$this->lat = isset($lat) ? (float)$lat : null;
46+
$this->lon = isset($lon) ? (float)$lon : null;
4747
}
4848
}

Cmfcmf/OpenWeatherMap/WeatherForecast.php

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,14 +17,13 @@
1717

1818
namespace Cmfcmf\OpenWeatherMap;
1919

20-
use Cmfcmf\OpenWeatherMap;
2120
use Cmfcmf\OpenWeatherMap\Util\City;
2221
use Cmfcmf\OpenWeatherMap\Util\Sun;
2322

2423
/**
2524
* Weather class returned by Cmfcmf\OpenWeatherMap->getWeather().
2625
*
27-
* @see Cmfcmf\OpenWeatherMap::getWeather() The function using it.
26+
* @see \Cmfcmf\OpenWeatherMap::getWeather() The function using it.
2827
*/
2928
class WeatherForecast implements \Iterator
3029
{
@@ -74,7 +73,7 @@ class WeatherForecast implements \Iterator
7473
*/
7574
public function __construct($xml, $units, $days)
7675
{
77-
$this->city = new City($xml->location->location['geobaseid'], $xml->location->name, $xml->location->location['longitude'], $xml->location->location['latitude'], $xml->location->country);
76+
$this->city = new City($xml->location->location['geobaseid'], $xml->location->name, $xml->location->location['latitude'], $xml->location->location['longitude'], $xml->location->country);
7877
$utctz = new \DateTimeZone('UTC');
7978
$this->sun = new Sun(new \DateTime($xml->sun['rise'], $utctz), new \DateTime($xml->sun['set'], $utctz));
8079
$this->lastUpdate = new \DateTime($xml->meta->lastupdate);

Cmfcmf/OpenWeatherMap/WeatherHistory.php

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@
1717

1818
namespace Cmfcmf\OpenWeatherMap;
1919

20-
use Cmfcmf\OpenWeatherMap;
2120
use Cmfcmf\OpenWeatherMap\Util\City;
2221

2322
/**
@@ -66,8 +65,8 @@ public function __construct($weatherHistory, $query)
6665
$this->city = new City(
6766
$weatherHistory['city_id'],
6867
(is_string($query)) ? $query : null,
69-
(isset($query['lon'])) ? $query['lon'] : null,
7068
(isset($query['lat'])) ? $query['lat'] : null,
69+
(isset($query['lon'])) ? $query['lon'] : null,
7170
$country,
7271
$population
7372
);

tests/OpenWeatherMapTest.php

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,6 @@
1616

1717
use \Cmfcmf\OpenWeatherMap;
1818
use Cmfcmf\OpenWeatherMap\Tests\TestFetcher;
19-
use \Cmfcmf\OpenWeatherMap\WeatherHistory;
20-
use \Cmfcmf\OpenWeatherMap\Tests\OpenWeatherMap\ExampleCacheTest;
2119

2220
class OpenWeatherMapTest extends \PHPUnit_Framework_TestCase
2321
{
@@ -113,7 +111,7 @@ public function testGetWeather()
113111

114112
public function testGetWeatherGroup()
115113
{
116-
$currentWeather = $this->owm->getWeatherGroup('2950159', 'imperial', 'en', '');
114+
$currentWeather = $this->owm->getWeatherGroup(array('2950159'), 'imperial', 'en', '');
117115

118116
$this->assertInstanceOf('\Cmfcmf\OpenWeatherMap\CurrentWeatherGroup', $currentWeather);
119117
}
@@ -157,7 +155,6 @@ public function testGetDailyWeatherForecast()
157155

158156
public function testGetWeatherHistory()
159157
{
160-
// @TODO!!
161158
$this->markTestSkipped('This getWeatherHistory method ignored because the api key need to have a paid permission.');
162159
}
163160

@@ -193,6 +190,7 @@ public function testBuildQueryUrlParameter()
193190

194191
public function testAbstractCache()
195192
{
193+
/** @var OpenWeatherMap\AbstractCache $sut */
196194
$sut = $this->getMockForAbstractClass('\Cmfcmf\OpenWeatherMap\AbstractCache');
197195
$this->assertNull($sut->setSeconds(10));
198196
}

0 commit comments

Comments
 (0)