|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/* |
|
4
|
|
|
* This file is part of the geo-api-library package. |
|
5
|
|
|
* |
|
6
|
|
|
* (c) 2020 WEBEWEB |
|
7
|
|
|
* |
|
8
|
|
|
* For the full copyright and license information, please view the LICENSE |
|
9
|
|
|
* file that was distributed with this source code. |
|
10
|
|
|
*/ |
|
11
|
|
|
|
|
12
|
|
|
namespace WBW\Library\GeoAPI\Serializer; |
|
13
|
|
|
|
|
14
|
|
|
use WBW\Library\Core\Argument\Helper\ArrayHelper; |
|
15
|
|
|
use WBW\Library\GeoAPI\Model\Commune; |
|
16
|
|
|
use WBW\Library\GeoAPI\Model\Departement; |
|
17
|
|
|
use WBW\Library\GeoAPI\Model\Region; |
|
18
|
|
|
use WBW\Library\GeoAPI\Response\CommunesResponse; |
|
19
|
|
|
use WBW\Library\GeoAPI\Response\DepartementsResponse; |
|
20
|
|
|
use WBW\Library\GeoAPI\Response\RegionsResponse; |
|
21
|
|
|
use WBW\Library\GeoJSON\Serializer\JsonDeserializer; |
|
22
|
|
|
|
|
23
|
|
|
/** |
|
24
|
|
|
* Découpage administratif response deserializer. |
|
25
|
|
|
* |
|
26
|
|
|
* @author webeweb <https://github.com/webeweb/> |
|
27
|
|
|
* @package WBW\Library\GeoAPI\Serializer |
|
28
|
|
|
*/ |
|
29
|
|
|
class DecoupageAdministratifResponseDeserializer extends JsonDeserializer { |
|
30
|
|
|
|
|
31
|
|
|
/** |
|
32
|
|
|
* Deserializes a commune. |
|
33
|
|
|
* |
|
34
|
|
|
* @param array $response The response. |
|
35
|
|
|
* @return Commune Returns the commune. |
|
36
|
15 |
|
*/ |
|
37
|
|
|
protected static function deserializeCommune(array $response): Commune { |
|
38
|
15 |
|
|
|
39
|
15 |
|
$model = new Commune(); |
|
40
|
15 |
|
$model->setNom(ArrayHelper::get($response, "nom")); |
|
41
|
15 |
|
$model->setCode(ArrayHelper::get($response, "code")); |
|
42
|
15 |
|
$model->setCodeDepartement(ArrayHelper::get($response, "codeDepartement")); |
|
43
|
15 |
|
$model->setCodeRegion(ArrayHelper::get($response, "codeRegion")); |
|
44
|
15 |
|
$model->setCentre(static::deserializeGeometry(ArrayHelper::get($response, "centre", []))); |
|
45
|
15 |
|
$model->setContour(static::deserializeGeometry(ArrayHelper::get($response, "contour", []))); |
|
46
|
|
|
$model->setSurface(ArrayHelper::get($response, "surface")); |
|
47
|
15 |
|
$model->setPopulation(ArrayHelper::get($response, "population")); |
|
48
|
|
|
$model->setCodesPostaux(ArrayHelper::get($response, "codesPostaux", [])); |
|
49
|
|
|
$model->setScore(ArrayHelper::get($response, "_score")); |
|
50
|
|
|
$model->setDepartement(static::deserializeDepartement(ArrayHelper::get($response, "departement", []))); |
|
51
|
|
|
$model->setRegion(static::deserializeRegion(ArrayHelper::get($response, "region", []))); |
|
52
|
|
|
|
|
53
|
|
|
return $model; |
|
54
|
|
|
} |
|
55
|
|
|
|
|
56
|
15 |
|
/** |
|
57
|
|
|
* Deserializes a communes response. |
|
58
|
15 |
|
* |
|
59
|
15 |
|
* @param string $rawResponse The raw response. |
|
60
|
|
|
* @return CommunesResponse Returns the communes response. |
|
61
|
15 |
|
*/ |
|
62
|
15 |
|
public static function deserializeCommunesResponse(string $rawResponse): CommunesResponse { |
|
63
|
|
|
|
|
64
|
|
|
$model = new CommunesResponse(); |
|
65
|
|
|
$model->setRawResponse($rawResponse); |
|
66
|
15 |
|
|
|
67
|
15 |
|
$response = json_decode($rawResponse, true); |
|
68
|
|
|
if (null === $response) { |
|
69
|
|
|
return $model; |
|
70
|
15 |
|
} |
|
71
|
|
|
|
|
72
|
|
|
$response = static::toArray($response); |
|
73
|
|
|
foreach ($response as $current) { |
|
74
|
|
|
$model->addCommune(static::deserializeCommune($current)); |
|
75
|
|
|
} |
|
76
|
|
|
|
|
77
|
|
|
return $model; |
|
78
|
|
|
} |
|
79
|
15 |
|
|
|
80
|
|
|
/** |
|
81
|
15 |
|
* Deserializes a département. |
|
82
|
15 |
|
* |
|
83
|
15 |
|
* @param array $response The response. |
|
84
|
15 |
|
* @return Departement|null Returns the département. |
|
85
|
15 |
|
*/ |
|
86
|
|
|
protected static function deserializeDepartement(array $response): ?Departement { |
|
87
|
15 |
|
|
|
88
|
|
|
if (0 === count($response)) { |
|
89
|
|
|
return null; |
|
90
|
|
|
} |
|
91
|
|
|
|
|
92
|
|
|
$model = new Departement(); |
|
93
|
|
|
$model->setNom(ArrayHelper::get($response, "nom")); |
|
94
|
|
|
$model->setCode(ArrayHelper::get($response, "code")); |
|
95
|
|
|
$model->setCodeRegion(ArrayHelper::get($response, "codeRegion")); |
|
96
|
15 |
|
$model->setScore(ArrayHelper::get($response, "_score")); |
|
97
|
|
|
|
|
98
|
15 |
|
return $model; |
|
99
|
15 |
|
} |
|
100
|
|
|
|
|
101
|
15 |
|
/** |
|
102
|
15 |
|
* Deserializes a départements response. |
|
103
|
|
|
* |
|
104
|
|
|
* @param string $rawResponse The raw response. |
|
105
|
|
|
* @return DepartementsResponse Returns the départements response. |
|
106
|
15 |
|
*/ |
|
107
|
15 |
|
public static function deserializeDepartementsResponse(string $rawResponse): DepartementsResponse { |
|
108
|
|
|
|
|
109
|
|
|
$model = new DepartementsResponse(); |
|
110
|
15 |
|
$model->setRawResponse($rawResponse); |
|
111
|
|
|
|
|
112
|
|
|
$response = json_decode($rawResponse, true); |
|
113
|
|
|
if (null === $response) { |
|
114
|
|
|
return $model; |
|
115
|
|
|
} |
|
116
|
|
|
|
|
117
|
|
|
$response = static::toArray($response); |
|
118
|
|
|
foreach ($response as $current) { |
|
119
|
10 |
|
$model->addDepartement(static::deserializeDepartement($current)); |
|
|
|
|
|
|
120
|
|
|
} |
|
121
|
10 |
|
|
|
122
|
10 |
|
return $model; |
|
123
|
10 |
|
} |
|
124
|
10 |
|
|
|
125
|
|
|
/** |
|
126
|
10 |
|
* Deserializes a région. |
|
127
|
|
|
* |
|
128
|
|
|
* @param array $response The response. |
|
129
|
|
|
* @return Region|null Returns the région. |
|
130
|
|
|
*/ |
|
131
|
|
|
protected static function deserializeRegion(array $response): ?Region { |
|
132
|
|
|
|
|
133
|
|
|
if (0 === count($response)) { |
|
134
|
|
|
return null; |
|
135
|
10 |
|
} |
|
136
|
|
|
|
|
137
|
10 |
|
$model = new Region(); |
|
138
|
10 |
|
$model->setCode(ArrayHelper::get($response, "code")); |
|
139
|
|
|
$model->setNom(ArrayHelper::get($response, "nom")); |
|
140
|
10 |
|
$model->setScore(ArrayHelper::get($response, "_score")); |
|
141
|
10 |
|
|
|
142
|
|
|
return $model; |
|
143
|
|
|
} |
|
144
|
|
|
|
|
145
|
10 |
|
/** |
|
146
|
10 |
|
* Deserializes a régions response. |
|
147
|
|
|
* |
|
148
|
|
|
* @param string $rawResponse The raw response. |
|
149
|
10 |
|
* @return RegionsResponse Returns the régions response. |
|
150
|
|
|
*/ |
|
151
|
3 |
|
public static function deserializeRegionsResponse(string $rawResponse): RegionsResponse { |
|
152
|
|
|
|
|
153
|
|
|
$model = new RegionsResponse(); |
|
154
|
|
|
$model->setRawResponse($rawResponse); |
|
155
|
|
|
|
|
156
|
|
|
$response = json_decode($rawResponse, true); |
|
157
|
|
|
if (null === $response) { |
|
158
|
|
|
return $model; |
|
159
|
|
|
} |
|
160
|
|
|
|
|
161
|
|
|
$response = static::toArray($response); |
|
162
|
|
|
foreach ($response as $current) { |
|
163
|
|
|
$model->addRegion(static::deserializeRegion($current)); |
|
|
|
|
|
|
164
|
|
|
} |
|
165
|
|
|
|
|
166
|
|
|
return $model; |
|
167
|
|
|
} |
|
168
|
|
|
|
|
169
|
|
|
/** |
|
170
|
|
|
* Convert an object into an array of object. |
|
171
|
|
|
* |
|
172
|
|
|
* @param array $response The response. |
|
173
|
|
|
* @return array Returns the converted array. |
|
174
|
|
|
*/ |
|
175
|
|
|
protected static function toArray(array $response): array { |
|
176
|
|
|
if (true === ArrayHelper::isObject($response)) { |
|
177
|
|
|
return [$response]; |
|
178
|
|
|
} |
|
179
|
|
|
return $response; |
|
180
|
|
|
} |
|
181
|
|
|
} |
Unless you are absolutely sure that the expression can never be null because of other conditions, we strongly recommend to add an additional type check to your code: