1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Marek\OpenWeatherMap\Hydrator; |
4
|
|
|
|
5
|
|
|
use Marek\OpenWeatherMap\API\Value\Response\APIResponse; |
6
|
|
|
use Marek\OpenWeatherMap\API\Value\Response\Clouds; |
7
|
|
|
use Marek\OpenWeatherMap\API\Value\Response\GeographicCoordinates; |
8
|
|
|
use Marek\OpenWeatherMap\API\Value\Response\Main; |
9
|
|
|
use Marek\OpenWeatherMap\API\Value\Response\Rain; |
10
|
|
|
use Marek\OpenWeatherMap\API\Value\Response\Snow; |
11
|
|
|
use Marek\OpenWeatherMap\API\Value\Response\Sys; |
12
|
|
|
use Marek\OpenWeatherMap\API\Value\Response\Weather\AggregatedWeather; |
13
|
|
|
use Marek\OpenWeatherMap\API\Value\Response\Weather\Weather; |
14
|
|
|
use Marek\OpenWeatherMap\API\Value\Response\WeatherValue; |
15
|
|
|
use Marek\OpenWeatherMap\API\Value\Response\Wind; |
16
|
|
|
|
17
|
|
|
class WeatherHydrator extends BaseHydrator implements HydratorInterface |
18
|
|
|
{ |
19
|
|
|
/** |
20
|
|
|
* @inheritDoc |
21
|
|
|
*/ |
22
|
|
|
public function hydrate($data, APIResponse $response) |
23
|
|
|
{ |
24
|
|
|
if (is_string($data)) { |
25
|
|
|
$data = json_decode($data, true); |
26
|
|
|
} |
27
|
|
|
|
28
|
|
|
if ($response instanceof AggregatedWeather) { |
29
|
|
|
return $this->hydrateMultiple($data, $response); |
30
|
|
|
} |
31
|
|
|
|
32
|
|
|
return $this->hydrateSingle($data, $response); |
|
|
|
|
33
|
|
|
} |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* @param $data |
37
|
|
|
* @param Weather $weather |
38
|
|
|
* |
39
|
|
|
* @return Weather |
40
|
|
|
*/ |
41
|
|
|
protected function hydrateSingle($data, Weather $weather) |
42
|
|
|
{ |
43
|
|
|
$innerWeather = []; |
44
|
|
|
foreach ($data['weather'] as $w) { |
45
|
|
|
$innerWeather[] = $this->hydrator->hydrate($w, new WeatherValue); |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
$weather->id = $data['id']; |
49
|
|
|
$weather->name = $data['name']; |
50
|
|
|
$weather->visibility = empty($data['visibility']) ? null : $data['visibility']; |
51
|
|
|
$weather->coord = $this->getValue('coord', $data, new GeographicCoordinates()); |
52
|
|
|
$weather->rain = $this->getValue('rain', $data, new Rain()); |
53
|
|
|
$weather->snow = $this->getValue('snow', $data, new Snow()); |
54
|
|
|
$weather->wind = $this->getValue('wind', $data, new Wind()); |
55
|
|
|
$weather->clouds = $this->getValue('clouds', $data, new Clouds()); |
56
|
|
|
$weather->main = $this->getValue('main', $data, new Main()); |
57
|
|
|
$weather->sys = $this->getValue('sys', $data, new Sys()); |
58
|
|
|
$weather->weather = $innerWeather; |
59
|
|
|
$weather->dt = empty($data['dt']) ? null : new \DateTime("@{$data['dt']}"); |
60
|
|
|
|
61
|
|
|
return $weather; |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
/** |
65
|
|
|
* @param $data |
66
|
|
|
* @param AggregatedWeather $weather |
67
|
|
|
* |
68
|
|
|
* @return AggregatedWeather |
69
|
|
|
*/ |
70
|
|
|
protected function hydrateMultiple($data, AggregatedWeather $weather) |
71
|
|
|
{ |
72
|
|
|
$weathers = []; |
73
|
|
|
foreach ($data['list'] as $datum) { |
74
|
|
|
$weathers[] = $this->hydrateSingle($datum, new Weather()); |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
$weather->count = empty($data['cnt']) ? $data['count'] : $data['cnt']; |
78
|
|
|
$weather->weathers = $weathers; |
79
|
|
|
|
80
|
|
|
return $weather; |
81
|
|
|
} |
82
|
|
|
} |
83
|
|
|
|
This check looks for parameters that are defined as one type in their type hint or doc comment but seem to be used as a narrower type, i.e an implementation of an interface or a subclass.
Consider changing the type of the parameter or doing an instanceof check before assuming your parameter is of the expected type.