@@ 19-77 (lines=59) @@ | ||
16 | use Marek\OpenWeatherMap\API\Value\Response\WeatherValue; |
|
17 | use Marek\OpenWeatherMap\API\Value\Response\Wind; |
|
18 | ||
19 | class DailyForecastDenormalizer extends AbstractDenormalizer |
|
20 | { |
|
21 | public function denormalize(array $data, APIResponse $response): APIResponse |
|
22 | { |
|
23 | if ($response instanceof AggregatedWeather) { |
|
24 | return $this->denormalizeMultiple($data, $response); |
|
25 | } |
|
26 | ||
27 | return $this->denormalizeSingle($data, $response); |
|
28 | } |
|
29 | ||
30 | /** |
|
31 | * @param $data |
|
32 | * @param Weather $weather |
|
33 | * |
|
34 | * @return Weather |
|
35 | */ |
|
36 | protected function denormalizeSingle(array $data, Weather $weather): Weather |
|
37 | { |
|
38 | $innerWeather = []; |
|
39 | foreach ($data['weather'] as $w) { |
|
40 | $innerWeather[] = $this->denormalizer->denormalize($w, WeatherValue::class); |
|
41 | } |
|
42 | ||
43 | $weather->id = $data['id']; |
|
44 | $weather->name = $data['name']; |
|
45 | $weather->visibility = empty($data['visibility']) ?? $data['visibility']; |
|
46 | $weather->coord = $this->getValue('coord', $data, GeographicCoordinates::class); |
|
47 | $weather->rain = $this->getValue('rain', $data, Rain::class); |
|
48 | $weather->snow = $this->getValue('snow', $data, Snow::class); |
|
49 | $weather->wind = $this->getValue('wind', $data, Wind::class); |
|
50 | $weather->clouds = $this->getValue('clouds', $data, Clouds::class); |
|
51 | $weather->main = $this->getValue('main', $data, Main::class); |
|
52 | $weather->sys = $this->getValue('sys', $data, Sys::class); |
|
53 | $weather->weather = $innerWeather; |
|
54 | $weather->dt = empty($data['dt']) ? null : new \DateTimeImmutable("@{$data['dt']}"); |
|
55 | ||
56 | return $weather; |
|
57 | } |
|
58 | ||
59 | /** |
|
60 | * @param array $data |
|
61 | * @param AggregatedWeather $weather |
|
62 | * |
|
63 | * @return AggregatedWeather |
|
64 | */ |
|
65 | protected function denormalizeMultiple($data, AggregatedWeather $weather): AggregatedWeather |
|
66 | { |
|
67 | $weathers = []; |
|
68 | foreach ($data['list'] as $datum) { |
|
69 | $weathers[] = $this->denormalizeSingle($datum, new Weather()); |
|
70 | } |
|
71 | ||
72 | $weather->count = empty($data['cnt']) ? $data['count'] : $data['cnt']; |
|
73 | $weather->weathers = $weathers; |
|
74 | ||
75 | return $weather; |
|
76 | } |
|
77 | } |
|
78 |
@@ 19-78 (lines=60) @@ | ||
16 | use Marek\OpenWeatherMap\API\Value\Response\WeatherValue; |
|
17 | use Marek\OpenWeatherMap\API\Value\Response\Wind; |
|
18 | ||
19 | class HourForecastDenormalizer extends AbstractDenormalizer |
|
20 | { |
|
21 | public function denormalize(array $data, APIResponse $response): APIResponse |
|
22 | { |
|
23 | if ($response instanceof AggregatedWeather) { |
|
24 | return $this->denormalizeMultiple($data, $response); |
|
25 | } |
|
26 | ||
27 | return $this->denormalizeSingle($data, $response); |
|
28 | } |
|
29 | ||
30 | /** |
|
31 | * @param $data |
|
32 | * @param Weather $weather |
|
33 | * |
|
34 | * @return Weather |
|
35 | */ |
|
36 | protected function denormalizeSingle(array $data, Weather $weather): Weather |
|
37 | { |
|
38 | $innerWeather = []; |
|
39 | foreach ($data['weather'] as $w) { |
|
40 | $innerWeather[] = $this->denormalizer->denormalize($w, WeatherValue::class); |
|
41 | } |
|
42 | ||
43 | $weather->id = $data['id']; |
|
44 | $weather->name = $data['name']; |
|
45 | $weather->visibility = empty($data['visibility']) ?? $data['visibility']; |
|
46 | $weather->coord = $this->getValue('coord', $data, GeographicCoordinates::class); |
|
47 | $weather->rain = $this->getValue('rain', $data, Rain::class); |
|
48 | $weather->snow = $this->getValue('snow', $data, Snow::class); |
|
49 | $weather->wind = $this->getValue('wind', $data, Wind::class); |
|
50 | $weather->clouds = $this->getValue('clouds', $data, Clouds::class); |
|
51 | $weather->main = $this->getValue('main', $data, Main::class); |
|
52 | $weather->sys = $this->getValue('sys', $data, Sys::class); |
|
53 | $weather->weather = $innerWeather; |
|
54 | $weather->dt = empty($data['dt']) ? null : new \DateTimeImmutable("@{$data['dt']}"); |
|
55 | ||
56 | ||
57 | return $weather; |
|
58 | } |
|
59 | ||
60 | /** |
|
61 | * @param array $data |
|
62 | * @param AggregatedWeather $weather |
|
63 | * |
|
64 | * @return AggregatedWeather |
|
65 | */ |
|
66 | protected function denormalizeMultiple($data, AggregatedWeather $weather): AggregatedWeather |
|
67 | { |
|
68 | $weathers = []; |
|
69 | foreach ($data['list'] as $datum) { |
|
70 | $weathers[] = $this->denormalizeSingle($datum, new Weather()); |
|
71 | } |
|
72 | ||
73 | $weather->count = empty($data['cnt']) ? $data['count'] : $data['cnt']; |
|
74 | $weather->weathers = $weathers; |
|
75 | ||
76 | return $weather; |
|
77 | } |
|
78 | } |
|
79 |
@@ 19-78 (lines=60) @@ | ||
16 | use Marek\OpenWeatherMap\API\Value\Response\WeatherValue; |
|
17 | use Marek\OpenWeatherMap\API\Value\Response\Wind; |
|
18 | ||
19 | class WeatherDenormalizer extends AbstractDenormalizer |
|
20 | { |
|
21 | public function denormalize(array $data, APIResponse $response): APIResponse |
|
22 | { |
|
23 | if ($response instanceof AggregatedWeather) { |
|
24 | return $this->denormalizeMultiple($data, $response); |
|
25 | } |
|
26 | ||
27 | return $this->denormalizeSingle($data, $response); |
|
28 | } |
|
29 | ||
30 | /** |
|
31 | * @param $data |
|
32 | * @param Weather $weather |
|
33 | * |
|
34 | * @return Weather |
|
35 | */ |
|
36 | protected function denormalizeSingle(array $data, Weather $weather): Weather |
|
37 | { |
|
38 | $innerWeather = []; |
|
39 | foreach ($data['weather'] as $w) { |
|
40 | $innerWeather[] = $this->denormalizer->denormalize($w, WeatherValue::class); |
|
41 | } |
|
42 | ||
43 | $weather->id = $data['id']; |
|
44 | $weather->name = $data['name']; |
|
45 | $weather->visibility = empty($data['visibility']) ?? $data['visibility']; |
|
46 | $weather->coord = $this->getValue('coord', $data, GeographicCoordinates::class); |
|
47 | $weather->rain = $this->getValue('rain', $data, Rain::class); |
|
48 | $weather->snow = $this->getValue('snow', $data, Snow::class); |
|
49 | $weather->wind = $this->getValue('wind', $data, Wind::class); |
|
50 | $weather->clouds = $this->getValue('clouds', $data, Clouds::class); |
|
51 | $weather->main = $this->getValue('main', $data, Main::class); |
|
52 | $weather->sys = $this->getValue('sys', $data, Sys::class); |
|
53 | $weather->weather = $innerWeather; |
|
54 | $weather->dt = empty($data['dt']) ? null : new \DateTimeImmutable("@{$data['dt']}"); |
|
55 | ||
56 | ||
57 | return $weather; |
|
58 | } |
|
59 | ||
60 | /** |
|
61 | * @param array $data |
|
62 | * @param AggregatedWeather $weather |
|
63 | * |
|
64 | * @return AggregatedWeather |
|
65 | */ |
|
66 | protected function denormalizeMultiple($data, AggregatedWeather $weather): AggregatedWeather |
|
67 | { |
|
68 | $weathers = []; |
|
69 | foreach ($data['list'] as $datum) { |
|
70 | $weathers[] = $this->denormalizeSingle($datum, new Weather()); |
|
71 | } |
|
72 | ||
73 | $weather->count = empty($data['cnt']) ? $data['count'] : $data['cnt']; |
|
74 | $weather->weathers = $weathers; |
|
75 | ||
76 | return $weather; |
|
77 | } |
|
78 | } |
|
79 |