Skip to content

Commit 11cc53a

Browse files
committed
feat: add weather overview endpoint
1 parent 6362645 commit 11cc53a

File tree

4 files changed

+80
-7
lines changed

4 files changed

+80
-7
lines changed
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
<?php
2+
3+
namespace ProgrammatorDev\OpenWeatherMap\Entity\OneCall;
4+
5+
use ProgrammatorDev\OpenWeatherMap\Entity\Coordinate;
6+
use ProgrammatorDev\OpenWeatherMap\Entity\Timezone;
7+
8+
class WeatherOverview
9+
{
10+
private Coordinate $coordinate;
11+
12+
private Timezone $timezone;
13+
14+
private \DateTimeImmutable $dateTime;
15+
16+
private string $overview;
17+
18+
public function __construct(array $data)
19+
{
20+
$this->coordinate = new Coordinate($data);
21+
22+
$this->timezone = new Timezone([
23+
'timezone_offset' => \DateTimeImmutable::createFromFormat('P', $data['tz'])->getOffset()
24+
]);
25+
26+
$this->dateTime = \DateTimeImmutable::createFromFormat(
27+
'Y-m-d H:i:s P',
28+
sprintf('%s 00:00:00 %s', $data['date'], $data['tz'])
29+
);
30+
31+
$this->overview = $data['weather_overview'];
32+
}
33+
34+
public function getCoordinate(): Coordinate
35+
{
36+
return $this->coordinate;
37+
}
38+
39+
public function getTimezone(): Timezone
40+
{
41+
return $this->timezone;
42+
}
43+
44+
public function getDateTime(): \DateTimeImmutable
45+
{
46+
return $this->dateTime;
47+
}
48+
49+
public function getOverview(): string
50+
{
51+
return $this->overview;
52+
}
53+
}

src/Entity/OneCall/WeatherSummary.php

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ public function __construct(array $data)
3636

3737
$this->dateTime = \DateTimeImmutable::createFromFormat(
3838
'Y-m-d H:i:s P',
39-
\sprintf('%s 00:00:00 %s', $data['date'], $data['tz'])
39+
sprintf('%s 00:00:00 %s', $data['date'], $data['tz'])
4040
);
4141

4242
$this->cloudiness = \round($data['cloud_cover']['afternoon']);
@@ -52,11 +52,11 @@ public function __construct(array $data)
5252
'max' => $data['temperature']['max']
5353
]);
5454

55-
$this->atmosphericPressure = \round($data['pressure']['afternoon']);
55+
$this->atmosphericPressure = round($data['pressure']['afternoon']);
5656

5757
$this->wind = new Wind([
5858
'speed' => $data['wind']['max']['speed'],
59-
'deg' => \round($data['wind']['max']['direction'])
59+
'deg' => round($data['wind']['max']['direction'])
6060
]);
6161
}
6262

@@ -70,9 +70,6 @@ public function getTimezone(): Timezone
7070
return $this->timezone;
7171
}
7272

73-
/**
74-
* DateTime in UTC
75-
*/
7673
public function getDateTime(): \DateTimeImmutable
7774
{
7875
return $this->dateTime;

src/Exception/ApiErrorException.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,9 @@ class ApiErrorException extends \Exception
88

99
public function __construct(array $error)
1010
{
11-
parent::__construct($error['message'], $error['cod']);
11+
$code = $error['cod'] ?? $error['code'];
12+
13+
parent::__construct($error['message'], $code);
1214

1315
$this->parameters = $error['parameters'] ?? null;
1416
}

src/Resource/OneCallResource.php

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
use ProgrammatorDev\Api\Method;
66
use ProgrammatorDev\OpenWeatherMap\Entity\OneCall\Weather;
77
use ProgrammatorDev\OpenWeatherMap\Entity\OneCall\WeatherMoment;
8+
use ProgrammatorDev\OpenWeatherMap\Entity\OneCall\WeatherOverview;
89
use ProgrammatorDev\OpenWeatherMap\Entity\OneCall\WeatherSummary;
910
use ProgrammatorDev\OpenWeatherMap\Resource\Util\LanguageTrait;
1011
use ProgrammatorDev\OpenWeatherMap\Resource\Util\UnitSystemTrait;
@@ -77,4 +78,24 @@ public function getWeatherSummaryByDate(float $latitude, float $longitude, \Date
7778

7879
return new WeatherSummary($data);
7980
}
81+
82+
/**
83+
* Get the weather overview with a human-readable summary for today and tomorrow's forecast, using OpenWeather AI
84+
*
85+
* @throws ClientExceptionInterface
86+
*/
87+
public function getWeatherOverviewByDate(float $latitude, float $longitude, \DateTimeInterface $date): WeatherOverview
88+
{
89+
$data = $this->api->request(
90+
method: Method::GET,
91+
path: '/data/3.0/onecall/overview',
92+
query: [
93+
'lat' => $latitude,
94+
'lon' => $longitude,
95+
'date' => $date->format('Y-m-d')
96+
]
97+
);
98+
99+
return new WeatherOverview($data);
100+
}
80101
}

0 commit comments

Comments
 (0)