Skip to content
This repository was archived by the owner on Nov 27, 2019. It is now read-only.

Commit aa69c2f

Browse files
Merge pull request #1 from nateritter/weather-history
Weather history (nateritter)
2 parents 402adfd + c79d2c6 commit aa69c2f

File tree

6 files changed

+107
-17
lines changed

6 files changed

+107
-17
lines changed

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
composer.lock
2+
vendor/

README.md

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,30 @@ public function bar()
7777
Visit [https://github.com/cmfcmf/OpenWeatherMap-PHP-Api/blob/master/Examples/WeatherForecast.php](https://github.com/cmfcmf/OpenWeatherMap-PHP-Api/blob/master/Examples/WeatherForecast.php) for more info.
7878

7979

80+
#### History
81+
82+
```
83+
...
84+
use Gmopx\LaravelOWM\LaravelOWM;
85+
...
86+
87+
public function bar()
88+
{
89+
$lowm = new LaravelOWM();
90+
91+
// Get yesterday's date
92+
$date = new \DateTime();
93+
$date->add(\DateInterval::createFromDateString('yesterday'));
94+
95+
$history = $lowm->getWeatherHistory('london', $date);
96+
97+
dd($history);
98+
}
99+
100+
```
101+
102+
Visit [https://github.com/cmfcmf/OpenWeatherMap-PHP-Api/blob/master/Examples/WeatherForecast.php](https://github.com/cmfcmf/OpenWeatherMap-PHP-Api/blob/master/Examples/WeatherForecast.php) for more info.
103+
80104
##### Parameters:
81105

82106
Note:
@@ -130,6 +154,30 @@ There are three ways to specify the place to get weather information for:
130154
131155
```
132156

157+
##### getWeatherHistory:
158+
```
159+
/**
160+
* Get the forecast of the requested location/city.
161+
*
162+
* More info about how to interact with the results:
163+
*
164+
* https://github.com/cmfcmf/OpenWeatherMap-PHP-Api/blob/master/Examples/WeatherHistory.php
165+
*
166+
* @param $query (required)
167+
* @param \DateTime $start (default: today)
168+
* @param int $endOrCount (default: 1)
169+
* @param string $type (default: hour) - 'tick', 'hour', or 'day'
170+
* @param string $lang (default: en) - http://openweathermap.org/current#multi.
171+
* @param string $units (default: metric) - 'metric' or 'imperial'.
172+
* @param bool $cache (default: false)
173+
* @param int $time (default: 600)
174+
* @return OpenWeatherMap\WeatherForecast
175+
*/
176+
public function getWeatherHistory($query, \DateTime $start, $endOrCount = 1, $type = 'hour', $lang = 'en', $units = 'metric', $cache = false, $time = 600)
177+
...
178+
179+
```
180+
133181

134182
### Routes
135183

src/Http/routes.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
<?php
22

3-
Route::group(['prefix' => 'owmapi', 'namespace' => 'Gmopx\LaravelOWM\Http\Controllers'], function() {
3+
Route::group(['prefix' => 'owmapi', 'namespace' => 'Gmopx\LaravelOWM\Http\Controllers'], function() {
44

5-
Route::get('current-weather', ['uses' => 'LaravelOWMController@currentweather']);
6-
Route::get('forecast', ['uses' => 'LaravelOWMController@forecast']);
5+
Route::get('current-weather', ['uses' => 'LaravelOWMController@currentweather']);
6+
Route::get('forecast', ['uses' => 'LaravelOWMController@forecast']);
77

8-
});
8+
});

src/LaravelOWM.php

Lines changed: 46 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
<?php namespace Gmopx\LaravelOWM;
1+
<?php
2+
namespace Gmopx\LaravelOWM;
23

34
use Cmfcmf\OpenWeatherMap;
45

@@ -18,11 +19,11 @@ public function __construct()
1819
{
1920
$this->config = config('laravel-owm');
2021

21-
if($this->config === null){
22+
if ($this->config === null) {
2223
throw new \Exception('config/laravel-owm.php not found');
2324
}
2425

25-
if($this->config['api_key'] === null){
26+
if ($this->config['api_key'] === null) {
2627
throw new \Exception('laravel-owm.api_key not found');
2728
}
2829

@@ -53,7 +54,7 @@ public function getCurrentWeather($query, $lang = 'en', $units = 'metric', $cach
5354
$lang = $lang ?: 'en';
5455
$units = $units ?: 'metric';
5556

56-
if($cache){
57+
if ($cache) {
5758
$owm = new OpenWeatherMap($this->api_key, null, new Cache(), $time);
5859
return $owm->getWeather($query, $units, $lang);
5960
}
@@ -88,13 +89,51 @@ public function getWeatherForecast($query, $lang = 'en', $units = 'metric', $day
8889
$units = $units ?: 'metric';
8990
$days = $days ?: 6;
9091

91-
if($cache){
92+
if ($cache) {
9293
$owm = new OpenWeatherMap($this->api_key, null, new Cache(), $time);
9394
return $owm->getWeatherForecast($query, $units, $lang, '', $days);
9495
}
9596

9697
$owm = new OpenWeatherMap($this->api_key);
9798
return $owm->getWeatherForecast($query, $units, $lang, '', $days);
9899
}
99-
100-
}
100+
101+
/**
102+
* Returns the weather history for the place you specified.
103+
*
104+
* More info about how to interact with the results:
105+
*
106+
* https://github.com/cmfcmf/OpenWeatherMap-PHP-Api/blob/master/Examples/WeatherHistory.php
107+
*
108+
* There are three ways to specify the place to get weather information for:
109+
* - Use the city name: $query must be a string containing the city name.
110+
* - Use the city id: $query must be an integer containing the city id.
111+
* - Use the coordinates: $query must be an associative array containing the 'lat' and 'lon' values.
112+
*
113+
* @param array|int|string $query
114+
* @param \DateTime $start
115+
* @param int $endOrCount
116+
* @param string $type
117+
* @param string $lang
118+
* @param string $units
119+
* @param bool $cache
120+
* @param int $time
121+
* @return OpenWeatherMap\WeatherHistory
122+
*/
123+
public function getWeatherHistory($query, \DateTime $start, $endOrCount = 1, $type = 'hour', $lang = 'en', $units = 'metric', $cache = false, $time = 600)
124+
{
125+
$lang = $lang ?: 'en';
126+
$units = $units ?: 'metric';
127+
$start = $start ?: new \DateTime;
128+
$endOrCount = $endOrCount ?: 1;
129+
$type = $type ?: 'hour';
130+
131+
if ($cache) {
132+
$owm = new OpenWeatherMap($this->api_key, null, new Cache(), $time);
133+
return $owm->getWeatherHistory($query, $start, $endOrCount, $type, $units, $lang, '');
134+
}
135+
136+
$owm = new OpenWeatherMap($this->api_key);
137+
return $owm->getWeatherHistory($query, $start, $endOrCount, $type, $units, $lang, '');
138+
}
139+
}

src/LaravelOWMServiceProvider.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,11 @@ public function boot()
1616

1717
// Loading routes file
1818
if ($config !== null) {
19-
if($config['routes_enabled']){
19+
20+
if ($config['routes_enabled']) {
2021
require __DIR__ . '/Http/routes.php';
2122
}
23+
2224
} else {
2325
require __DIR__ . '/Http/routes.php';
2426
}

src/config/laravel-owm.php

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
<?php
22

3-
return [
4-
'api_key' => '', // visit: http://openweathermap.org/appid#get for more info.
5-
6-
'routes_enabled' => true, // If the routes have to be enabled.
7-
];
3+
return [
4+
'api_key' => '', // visit: http://openweathermap.org/appid#get for more info.
5+
'routes_enabled' => true, // If the routes have to be enabled.
6+
];

0 commit comments

Comments
 (0)