1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace Marek\OpenWeatherMap\Denormalizer; |
6
|
|
|
|
7
|
|
|
use Marek\OpenWeatherMap\API\Value\Response\APIResponse; |
8
|
|
|
use Marek\OpenWeatherMap\API\Value\Response\GeographicCoordinates; |
9
|
|
|
use Marek\OpenWeatherMap\API\Value\Response\UltravioletIndex\AggregatedUltravioletIndex; |
10
|
|
|
use Marek\OpenWeatherMap\API\Value\Response\UltravioletIndex\UltravioletIndex; |
11
|
|
|
|
12
|
|
|
class UltravioletIndexDenormalizer extends AbstractDenormalizer |
13
|
|
|
{ |
14
|
|
|
public function denormalize(array $data, APIResponse $response): APIResponse |
15
|
|
|
{ |
16
|
|
|
if ($response instanceof AggregatedUltravioletIndex) { |
17
|
|
|
return $this->denormalizeMultiple($data, $response); |
18
|
|
|
} |
19
|
|
|
|
20
|
|
|
if (!$response instanceof UltravioletIndex) { |
21
|
|
|
return $response; |
22
|
|
|
} |
23
|
|
|
|
24
|
|
|
return $this->denormalizeSingle($data, $response); |
25
|
|
|
} |
26
|
|
|
|
27
|
|
|
protected function denormalizeSingle(array $data, UltravioletIndex $index): UltravioletIndex |
28
|
|
|
{ |
29
|
|
|
$index->value = $this->getData('value', $data); |
30
|
|
|
$index->date = $this->getDateTimeFromTimestamp('date', $data); |
|
|
|
|
31
|
|
|
$index->isoDate = $this->getValue('date_iso', $data, \DateTimeInterface::class); |
32
|
|
|
|
33
|
|
|
$location = new GeographicCoordinates(); |
34
|
|
|
$location->latitude = $this->getData('lat', $data); |
35
|
|
|
$location->longitude = $this->getData('lon', $data); |
36
|
|
|
|
37
|
|
|
$index->location = $location; |
|
|
|
|
38
|
|
|
|
39
|
|
|
return $index; |
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
protected function denormalizeMultiple($data, AggregatedUltravioletIndex $response): AggregatedUltravioletIndex |
43
|
|
|
{ |
44
|
|
|
$indexes = []; |
45
|
|
|
foreach ($data as $datum) { |
46
|
|
|
$indexes[] = $this->denormalizeSingle($datum, new UltravioletIndex()); |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
$response->count = count($indexes); |
50
|
|
|
$response->indexes = $indexes; |
51
|
|
|
|
52
|
|
|
return $response; |
53
|
|
|
} |
54
|
|
|
} |
55
|
|
|
|
Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.
Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..