Skip to content

Commit 72c9c08

Browse files
committed
feat(stations): add measurement aggregate entities
1 parent bc2cec2 commit 72c9c08

8 files changed

Lines changed: 621 additions & 0 deletions

File tree

Lines changed: 108 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,108 @@
1+
<?php
2+
3+
namespace ProgrammatorDev\OpenWeatherMap\Entity\Stations;
4+
5+
use ProgrammatorDev\Api\Context\Context;
6+
use ProgrammatorDev\Api\Contract\EntityInterface;
7+
use ProgrammatorDev\OpenWeatherMap\Entity\Stations\MeasurementAggregate\Humidity;
8+
use ProgrammatorDev\OpenWeatherMap\Entity\Stations\MeasurementAggregate\Precipitation;
9+
use ProgrammatorDev\OpenWeatherMap\Entity\Stations\MeasurementAggregate\Pressure;
10+
use ProgrammatorDev\OpenWeatherMap\Entity\Stations\MeasurementAggregate\Temperature;
11+
use ProgrammatorDev\OpenWeatherMap\Entity\Stations\MeasurementAggregate\Wind;
12+
use ProgrammatorDev\OpenWeatherMap\Enum\AggregationInterval;
13+
use ProgrammatorDev\OpenWeatherMap\Exception\HydrationException;
14+
use ProgrammatorDev\OpenWeatherMap\Hydration\PayloadReader;
15+
16+
final class MeasurementAggregate implements EntityInterface
17+
{
18+
private function __construct(
19+
private readonly ?AggregationInterval $interval,
20+
private readonly ?\DateTimeImmutable $dateTime,
21+
private readonly ?string $stationId,
22+
private readonly ?Temperature $temperature,
23+
private readonly ?Humidity $humidity,
24+
private readonly ?Wind $wind,
25+
private readonly ?Pressure $pressure,
26+
private readonly ?Precipitation $precipitation,
27+
) {}
28+
29+
public static function fromArray(array $data, ?Context $context = null): static
30+
{
31+
$reader = PayloadReader::from($data, self::class);
32+
$interval = $reader->nullableString('type');
33+
$temperature = $reader->nullableArray('temp');
34+
$humidity = $reader->nullableArray('humidity');
35+
$wind = $reader->nullableArray('wind');
36+
$pressure = $reader->nullableArray('pressure');
37+
$precipitation = $reader->nullableArray('precipitation');
38+
39+
if ($interval !== null) {
40+
$interval = AggregationInterval::tryFrom($interval)
41+
?? throw HydrationException::invalidValue(
42+
self::class,
43+
'type',
44+
'one of m, h, or d',
45+
$interval,
46+
);
47+
}
48+
49+
return new self(
50+
interval: $interval,
51+
dateTime: $reader->nullableTimestamp('date'),
52+
stationId: $reader->nullableString('station_id'),
53+
temperature: $temperature === null
54+
? null
55+
: Temperature::fromArray($temperature, $context),
56+
humidity: $humidity === null
57+
? null
58+
: Humidity::fromArray($humidity, $context),
59+
wind: $wind === null ? null : Wind::fromArray($wind, $context),
60+
pressure: $pressure === null
61+
? null
62+
: Pressure::fromArray($pressure, $context),
63+
precipitation: $precipitation === null
64+
? null
65+
: Precipitation::fromArray($precipitation, $context),
66+
);
67+
}
68+
69+
public function interval(): ?AggregationInterval
70+
{
71+
return $this->interval;
72+
}
73+
74+
public function dateTime(): ?\DateTimeImmutable
75+
{
76+
return $this->dateTime;
77+
}
78+
79+
public function stationId(): ?string
80+
{
81+
return $this->stationId;
82+
}
83+
84+
public function temperature(): ?Temperature
85+
{
86+
return $this->temperature;
87+
}
88+
89+
public function humidity(): ?Humidity
90+
{
91+
return $this->humidity;
92+
}
93+
94+
public function wind(): ?Wind
95+
{
96+
return $this->wind;
97+
}
98+
99+
public function pressure(): ?Pressure
100+
{
101+
return $this->pressure;
102+
}
103+
104+
public function precipitation(): ?Precipitation
105+
{
106+
return $this->precipitation;
107+
}
108+
}
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
<?php
2+
3+
namespace ProgrammatorDev\OpenWeatherMap\Entity\Stations\MeasurementAggregate;
4+
5+
use ProgrammatorDev\Api\Context\Context;
6+
use ProgrammatorDev\Api\Contract\EntityInterface;
7+
use ProgrammatorDev\OpenWeatherMap\Enum\Unit;
8+
use ProgrammatorDev\OpenWeatherMap\Formatting\MeasurementFormatter;
9+
use ProgrammatorDev\OpenWeatherMap\Hydration\PayloadReader;
10+
11+
final class Humidity implements EntityInterface
12+
{
13+
private function __construct(
14+
private readonly ?float $average,
15+
private readonly ?int $weight,
16+
) {}
17+
18+
public static function fromArray(array $data, ?Context $context = null): static
19+
{
20+
$reader = PayloadReader::from($data, self::class);
21+
22+
return new self(
23+
average: $reader->nullableFloat('average'),
24+
weight: $reader->nullableInt('weight'),
25+
);
26+
}
27+
28+
public function average(): ?float
29+
{
30+
return $this->average;
31+
}
32+
33+
public function averageUnit(): Unit
34+
{
35+
return Unit::PERCENT;
36+
}
37+
38+
public function averageWithUnit(): ?string
39+
{
40+
return MeasurementFormatter::format($this->average, $this->averageUnit());
41+
}
42+
43+
public function weight(): ?int
44+
{
45+
return $this->weight;
46+
}
47+
}
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
<?php
2+
3+
namespace ProgrammatorDev\OpenWeatherMap\Entity\Stations\MeasurementAggregate;
4+
5+
use ProgrammatorDev\Api\Context\Context;
6+
use ProgrammatorDev\Api\Contract\EntityInterface;
7+
use ProgrammatorDev\OpenWeatherMap\Enum\Unit;
8+
use ProgrammatorDev\OpenWeatherMap\Formatting\MeasurementFormatter;
9+
use ProgrammatorDev\OpenWeatherMap\Hydration\PayloadReader;
10+
11+
final class Precipitation implements EntityInterface
12+
{
13+
private function __construct(
14+
private readonly ?float $rain,
15+
private readonly ?float $snow,
16+
) {}
17+
18+
public static function fromArray(array $data, ?Context $context = null): static
19+
{
20+
$reader = PayloadReader::from($data, self::class);
21+
22+
return new self(
23+
rain: $reader->nullableFloat('rain'),
24+
snow: $reader->nullableFloat('snow'),
25+
);
26+
}
27+
28+
public function rain(): ?float
29+
{
30+
return $this->rain;
31+
}
32+
33+
public function rainUnit(): Unit
34+
{
35+
return Unit::MILLIMETER;
36+
}
37+
38+
public function rainWithUnit(): ?string
39+
{
40+
return $this->format($this->rain);
41+
}
42+
43+
public function snow(): ?float
44+
{
45+
return $this->snow;
46+
}
47+
48+
public function snowUnit(): Unit
49+
{
50+
return Unit::MILLIMETER;
51+
}
52+
53+
public function snowWithUnit(): ?string
54+
{
55+
return $this->format($this->snow);
56+
}
57+
58+
private function format(?float $precipitation): ?string
59+
{
60+
return MeasurementFormatter::format($precipitation, Unit::MILLIMETER);
61+
}
62+
}
Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
<?php
2+
3+
namespace ProgrammatorDev\OpenWeatherMap\Entity\Stations\MeasurementAggregate;
4+
5+
use ProgrammatorDev\Api\Context\Context;
6+
use ProgrammatorDev\Api\Contract\EntityInterface;
7+
use ProgrammatorDev\OpenWeatherMap\Enum\Unit;
8+
use ProgrammatorDev\OpenWeatherMap\Formatting\MeasurementFormatter;
9+
use ProgrammatorDev\OpenWeatherMap\Hydration\PayloadReader;
10+
11+
final class Pressure implements EntityInterface
12+
{
13+
private function __construct(
14+
private readonly ?float $minimum,
15+
private readonly ?float $maximum,
16+
private readonly ?float $average,
17+
private readonly ?int $weight,
18+
) {}
19+
20+
public static function fromArray(array $data, ?Context $context = null): static
21+
{
22+
$reader = PayloadReader::from($data, self::class);
23+
24+
return new self(
25+
minimum: $reader->nullableFloat('min'),
26+
maximum: $reader->nullableFloat('max'),
27+
average: $reader->nullableFloat('average'),
28+
weight: $reader->nullableInt('weight'),
29+
);
30+
}
31+
32+
public function minimum(): ?float
33+
{
34+
return $this->minimum;
35+
}
36+
37+
public function minimumUnit(): Unit
38+
{
39+
return Unit::HECTOPASCAL;
40+
}
41+
42+
public function minimumWithUnit(): ?string
43+
{
44+
return $this->format($this->minimum);
45+
}
46+
47+
public function maximum(): ?float
48+
{
49+
return $this->maximum;
50+
}
51+
52+
public function maximumUnit(): Unit
53+
{
54+
return Unit::HECTOPASCAL;
55+
}
56+
57+
public function maximumWithUnit(): ?string
58+
{
59+
return $this->format($this->maximum);
60+
}
61+
62+
public function average(): ?float
63+
{
64+
return $this->average;
65+
}
66+
67+
public function averageUnit(): Unit
68+
{
69+
return Unit::HECTOPASCAL;
70+
}
71+
72+
public function averageWithUnit(): ?string
73+
{
74+
return $this->format($this->average);
75+
}
76+
77+
public function weight(): ?int
78+
{
79+
return $this->weight;
80+
}
81+
82+
private function format(?float $pressure): ?string
83+
{
84+
return MeasurementFormatter::format($pressure, Unit::HECTOPASCAL);
85+
}
86+
}
Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
<?php
2+
3+
namespace ProgrammatorDev\OpenWeatherMap\Entity\Stations\MeasurementAggregate;
4+
5+
use ProgrammatorDev\Api\Context\Context;
6+
use ProgrammatorDev\Api\Contract\EntityInterface;
7+
use ProgrammatorDev\OpenWeatherMap\Enum\Unit;
8+
use ProgrammatorDev\OpenWeatherMap\Formatting\MeasurementFormatter;
9+
use ProgrammatorDev\OpenWeatherMap\Hydration\PayloadReader;
10+
11+
final class Temperature implements EntityInterface
12+
{
13+
private function __construct(
14+
private readonly ?float $minimum,
15+
private readonly ?float $maximum,
16+
private readonly ?float $average,
17+
private readonly ?int $weight,
18+
) {}
19+
20+
public static function fromArray(array $data, ?Context $context = null): static
21+
{
22+
$reader = PayloadReader::from($data, self::class);
23+
24+
return new self(
25+
minimum: $reader->nullableFloat('min'),
26+
maximum: $reader->nullableFloat('max'),
27+
average: $reader->nullableFloat('average'),
28+
weight: $reader->nullableInt('weight'),
29+
);
30+
}
31+
32+
public function minimum(): ?float
33+
{
34+
return $this->minimum;
35+
}
36+
37+
public function minimumUnit(): Unit
38+
{
39+
return Unit::CELSIUS;
40+
}
41+
42+
public function minimumWithUnit(): ?string
43+
{
44+
return $this->format($this->minimum);
45+
}
46+
47+
public function maximum(): ?float
48+
{
49+
return $this->maximum;
50+
}
51+
52+
public function maximumUnit(): Unit
53+
{
54+
return Unit::CELSIUS;
55+
}
56+
57+
public function maximumWithUnit(): ?string
58+
{
59+
return $this->format($this->maximum);
60+
}
61+
62+
public function average(): ?float
63+
{
64+
return $this->average;
65+
}
66+
67+
public function averageUnit(): Unit
68+
{
69+
return Unit::CELSIUS;
70+
}
71+
72+
public function averageWithUnit(): ?string
73+
{
74+
return $this->format($this->average);
75+
}
76+
77+
public function weight(): ?int
78+
{
79+
return $this->weight;
80+
}
81+
82+
private function format(?float $temperature): ?string
83+
{
84+
return MeasurementFormatter::format($temperature, Unit::CELSIUS);
85+
}
86+
}

0 commit comments

Comments
 (0)