|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace one; |
|
4
|
|
|
|
|
5
|
|
|
use One\Model\Article; |
|
6
|
|
|
use one\Model\Gallery; |
|
7
|
|
|
use One\Model\Page; |
|
8
|
|
|
use one\Model\Photo; |
|
9
|
|
|
use One\Model\Video; |
|
10
|
|
|
|
|
11
|
|
|
class FormatMapping |
|
12
|
|
|
{ |
|
13
|
|
|
|
|
14
|
|
|
/** |
|
15
|
|
|
* map a single article to main attributes in Article Class |
|
16
|
|
|
* @param string $singleJsonArticle JSON response |
|
17
|
|
|
* @return Article |
|
18
|
|
|
* @throws Exception |
|
19
|
|
|
*/ |
|
20
|
|
|
public function article($singleJsonArticle) |
|
21
|
|
|
{ |
|
22
|
|
|
if (json_decode($singleJsonArticle, true)) { |
|
23
|
|
|
$dataArticle = json_decode($singleJsonArticle, true)['data']; |
|
24
|
|
|
|
|
25
|
|
|
$article = new Article( |
|
26
|
|
|
|
|
27
|
|
|
$title = $this->filterString($this->getValue('title', $dataArticle)), |
|
28
|
|
|
|
|
29
|
|
|
$body = $this->filterString($this->getValue('body', $dataArticle)), |
|
30
|
|
|
|
|
31
|
|
|
$source = $this->filterString($this->getValue('source', $dataArticle)), |
|
32
|
|
|
|
|
33
|
|
|
$uniqueId = $this->getValue('unique_id', $dataArticle), |
|
34
|
|
|
|
|
35
|
|
|
$typeId = $this->filterInteger($this->getValue('type_id', $dataArticle['type'])), |
|
36
|
|
|
|
|
37
|
|
|
$categoryId = $this->filterInteger($this->getValue( |
|
38
|
|
|
'category_id', |
|
39
|
|
|
$dataArticle['category'] |
|
40
|
|
|
)), |
|
41
|
|
|
|
|
42
|
|
|
$reporter = $this->getValue('reporter', $dataArticle), |
|
43
|
|
|
|
|
44
|
|
|
$lead = $this->filterString($this->getValue('lead', $dataArticle)), |
|
45
|
|
|
|
|
46
|
|
|
$tags = $this->getValue('tag_name', $dataArticle['tags']), |
|
47
|
|
|
|
|
48
|
|
|
$publishedAt = $this->filterString($this->getValue('published_at', $dataArticle)), |
|
49
|
|
|
|
|
50
|
|
|
$identifier = $this->filterInteger($this->getValue('id', $dataArticle)) |
|
51
|
|
|
|
|
52
|
|
|
); |
|
53
|
|
|
|
|
54
|
|
|
$photo_attachments = $this->photo($dataArticle); |
|
55
|
|
|
|
|
56
|
|
|
for ($i = 0; $i < count($photo_attachments); $i++) { |
|
|
|
|
|
|
57
|
|
|
$article->attachPhoto($photo_attachments[$i]); |
|
58
|
|
|
} |
|
59
|
|
|
|
|
60
|
|
|
$page_attachments = $this->page($dataArticle); |
|
61
|
|
|
|
|
62
|
|
|
for ($i = 0; $i < count($page_attachments); $i++) { |
|
|
|
|
|
|
63
|
|
|
$article->attachPage($page_attachments[$i]); |
|
64
|
|
|
} |
|
65
|
|
|
|
|
66
|
|
|
$gallery_attachments = $this->gallery($dataArticle); |
|
67
|
|
|
|
|
68
|
|
|
for ($i = 0; $i < count($gallery_attachments); $i++) { |
|
|
|
|
|
|
69
|
|
|
$article->attachGallery($gallery_attachments[$i]); |
|
70
|
|
|
} |
|
71
|
|
|
|
|
72
|
|
|
$video_attachments = $this->video($dataArticle); |
|
73
|
|
|
|
|
74
|
|
|
for ($i = 0; $i < count($video_attachments); $i++) { |
|
|
|
|
|
|
75
|
|
|
$article->attachVideo($video_attachments[$i]); |
|
76
|
|
|
} |
|
77
|
|
|
|
|
78
|
|
|
return $article; |
|
79
|
|
|
} |
|
80
|
|
|
|
|
81
|
|
|
throw new \Exception("Empty or invalid JSON Response", 1); |
|
82
|
|
|
} |
|
83
|
|
|
|
|
84
|
|
|
/** |
|
85
|
|
|
* Return photo(s) attachment of an article |
|
86
|
|
|
* @param Associative array $dataArticle |
|
87
|
|
|
* @return Array of photos |
|
88
|
|
|
*/ |
|
89
|
|
|
private function photo($dataArticle) |
|
90
|
|
|
{ |
|
91
|
|
|
$dataPhotos = $dataArticle['photos']; |
|
92
|
|
|
|
|
93
|
|
|
$encoded = json_encode($dataPhotos); |
|
94
|
|
|
|
|
95
|
|
|
$photos = array(); |
|
96
|
|
|
|
|
97
|
|
|
if ($this->filterArray($dataPhotos)) { |
|
98
|
|
|
$decodedDataPhotos = json_decode($encoded, true); |
|
99
|
|
|
|
|
100
|
|
|
for ($i = 0; $i < count($decodedDataPhotos); $i++) { |
|
|
|
|
|
|
101
|
|
|
$dataPhoto = $decodedDataPhotos[$i]; |
|
102
|
|
|
|
|
103
|
|
|
$photo = new Photo( |
|
104
|
|
|
$this->getValue('photo_url', $dataPhoto), |
|
105
|
|
|
$this->photoRatio($this->getValue('photo_ratio', $dataPhoto)), |
|
106
|
|
|
'', |
|
107
|
|
|
'' |
|
108
|
|
|
); |
|
109
|
|
|
|
|
110
|
|
|
array_push($photos, $photo); |
|
111
|
|
|
} |
|
112
|
|
|
} |
|
113
|
|
|
|
|
114
|
|
|
return $photos; |
|
115
|
|
|
} |
|
116
|
|
|
|
|
117
|
|
|
/** |
|
118
|
|
|
* Return page(s) attachment in an article |
|
119
|
|
|
* @param Associative array $dataArticle |
|
120
|
|
|
* @return Array of pages |
|
121
|
|
|
*/ |
|
122
|
|
View Code Duplication |
private function page($dataArticle) |
|
|
|
|
|
|
123
|
|
|
{ |
|
124
|
|
|
$dataPages = $dataArticle['pages']; |
|
125
|
|
|
|
|
126
|
|
|
$encoded = json_encode($dataPages); |
|
127
|
|
|
|
|
128
|
|
|
$pages = array(); |
|
129
|
|
|
|
|
130
|
|
|
if ($this->filterArray($dataPages)) { |
|
131
|
|
|
$decodedDataPages = json_decode($encoded, true); |
|
132
|
|
|
|
|
133
|
|
|
for ($i = 0; $i < count($decodedDataPages); $i++) { |
|
|
|
|
|
|
134
|
|
|
$dataPage = $decodedDataPages[$i]; |
|
135
|
|
|
|
|
136
|
|
|
$page = new Page( |
|
137
|
|
|
$this->getValue('page_title', $dataPage), |
|
138
|
|
|
$this->getValue('page_body', $dataPage), |
|
139
|
|
|
$this->getValue('page_source', $dataPage), |
|
140
|
|
|
$this->getValue('page_order', $dataPage), |
|
141
|
|
|
$this->getValue('page_cover', $dataPage), |
|
142
|
|
|
$this->getValue('page_lead', $dataPage) |
|
143
|
|
|
); |
|
144
|
|
|
|
|
145
|
|
|
array_push($pages, $page); |
|
146
|
|
|
} |
|
147
|
|
|
} |
|
148
|
|
|
|
|
149
|
|
|
return $pages; |
|
150
|
|
|
} |
|
151
|
|
|
|
|
152
|
|
|
/** |
|
153
|
|
|
* Return gallery(ies) attachment |
|
154
|
|
|
* @param Associative arrray $dataArticle |
|
155
|
|
|
* @return Array of galleries |
|
156
|
|
|
*/ |
|
157
|
|
View Code Duplication |
private function gallery($dataArticle) |
|
|
|
|
|
|
158
|
|
|
{ |
|
159
|
|
|
$dataGalleries = $dataArticle['galleries']; |
|
160
|
|
|
|
|
161
|
|
|
$encoded = json_encode($dataGalleries); |
|
162
|
|
|
|
|
163
|
|
|
$galleries = array(); |
|
164
|
|
|
|
|
165
|
|
|
if ($this->filterArray($dataGalleries)) { |
|
166
|
|
|
$decodedDataGalleries = json_decode($encoded, true); |
|
167
|
|
|
|
|
168
|
|
|
for ($i = 0; $i < count($decodedDataGalleries); $i++) { |
|
|
|
|
|
|
169
|
|
|
$dataGallery = $decodedDataGalleries[$i]; |
|
170
|
|
|
|
|
171
|
|
|
$gallery = new Gallery( |
|
172
|
|
|
$this->getValue('gallery_body', $dataGallery), |
|
173
|
|
|
$this->getValue('gallery_order', $dataGallery), |
|
174
|
|
|
$this->getValue('gallery_photo', $dataGallery), |
|
175
|
|
|
$this->getValue('gallery_source', $dataGallery), |
|
176
|
|
|
$this->getValue('gallery_lead', $dataGallery) |
|
177
|
|
|
); |
|
178
|
|
|
|
|
179
|
|
|
array_push($galleries, $gallery); |
|
180
|
|
|
} |
|
181
|
|
|
} |
|
182
|
|
|
|
|
183
|
|
|
return $galleries; |
|
184
|
|
|
} |
|
185
|
|
|
|
|
186
|
|
|
/** |
|
187
|
|
|
* Return videos attachment |
|
188
|
|
|
* @param Associative arrray $dataArticle |
|
189
|
|
|
* @return Array of videos |
|
190
|
|
|
*/ |
|
191
|
|
View Code Duplication |
private function video($dataArticle) |
|
|
|
|
|
|
192
|
|
|
{ |
|
193
|
|
|
$dataVideos = $dataArticle['videos']; |
|
194
|
|
|
|
|
195
|
|
|
$encoded = json_encode($dataVideos); |
|
196
|
|
|
|
|
197
|
|
|
$videos = array(); |
|
198
|
|
|
|
|
199
|
|
|
if ($this->filterArray($dataVideos)) { |
|
200
|
|
|
$decodedDataVideos = json_decode($encoded, true); |
|
201
|
|
|
|
|
202
|
|
|
for ($i = 0; $i < count($decodedDataVideos); $i++) { |
|
|
|
|
|
|
203
|
|
|
$dataVideo = $decodedDataVideos[$i]; |
|
204
|
|
|
|
|
205
|
|
|
$video = new Video( |
|
206
|
|
|
$this->getValue('video_body', $dataVideo), |
|
207
|
|
|
$this->getValue('video_source', $dataVideo), |
|
208
|
|
|
$this->getValue('video_order', $dataVideo), |
|
209
|
|
|
$this->getValue('video_cover', $dataVideo), |
|
210
|
|
|
$this->getValue('video_lead', $dataVideo) |
|
211
|
|
|
); |
|
212
|
|
|
|
|
213
|
|
|
array_push($videos, $video); |
|
214
|
|
|
} |
|
215
|
|
|
} |
|
216
|
|
|
|
|
217
|
|
|
return $videos; |
|
218
|
|
|
} |
|
219
|
|
|
|
|
220
|
|
|
/** |
|
221
|
|
|
* Map ratio to photo ratio constants |
|
222
|
|
|
* @param string $ratio |
|
223
|
|
|
* @return constant ratio |
|
224
|
|
|
* @throws Exception |
|
225
|
|
|
*/ |
|
226
|
|
|
private function photoRatio($ratio) |
|
227
|
|
|
{ |
|
228
|
|
|
if ($ratio == "1:1") { |
|
229
|
|
|
return Photo::RATIO_SQUARE; |
|
230
|
|
|
} elseif ($ratio == "2:1") { |
|
231
|
|
|
return Photo::RATIO_RECTANGLE; |
|
232
|
|
|
} elseif ($ratio == "3:2") { |
|
233
|
|
|
return Photo::RATIO_HEADLINE; |
|
234
|
|
|
} elseif ($ratio == "9:16") { |
|
235
|
|
|
return Photo::RATIO_VERTICAL; |
|
236
|
|
|
} elseif ($ratio == 'cover') { |
|
237
|
|
|
return Photo::RATIO_COVER; |
|
238
|
|
|
} else { |
|
239
|
|
|
throw new \Exception("Unknown ratio", 1); |
|
240
|
|
|
} |
|
241
|
|
|
} |
|
242
|
|
|
|
|
243
|
|
|
/** |
|
244
|
|
|
* Make sure value is integer |
|
245
|
|
|
* @param mixed $int |
|
246
|
|
|
* @return int |
|
247
|
|
|
* @throws Exception |
|
248
|
|
|
*/ |
|
249
|
|
|
private function filterInteger($int) |
|
250
|
|
|
{ |
|
251
|
|
|
if (is_int((int) $int)) { |
|
252
|
|
|
return $int; |
|
253
|
|
|
} |
|
254
|
|
|
throw new \Exception("Invalid Integer", 1); |
|
255
|
|
|
} |
|
256
|
|
|
|
|
257
|
|
|
/** |
|
258
|
|
|
* Make sure string is not null or empty |
|
259
|
|
|
* @param mixed $str |
|
260
|
|
|
* @return string if it is valid or exception |
|
261
|
|
|
* @throws Exception |
|
262
|
|
|
*/ |
|
263
|
|
|
private function filterString($str) |
|
264
|
|
|
{ |
|
265
|
|
|
if (is_string($str) && strlen($str) > 0 && !is_null($str)) { |
|
266
|
|
|
return $str; |
|
267
|
|
|
} |
|
268
|
|
|
throw new \Exception("String required", 1); |
|
269
|
|
|
} |
|
270
|
|
|
|
|
271
|
|
|
/** |
|
272
|
|
|
* Make sure variable is type of array |
|
273
|
|
|
* @param mixed $array |
|
274
|
|
|
* @return array |
|
275
|
|
|
* @throws Exception |
|
276
|
|
|
*/ |
|
277
|
|
|
private function filterArray($array) |
|
278
|
|
|
{ |
|
279
|
|
|
if (is_array($array)) { |
|
280
|
|
|
return $array; |
|
281
|
|
|
} |
|
282
|
|
|
throw new \Exception("Array required", 1); |
|
283
|
|
|
} |
|
284
|
|
|
|
|
285
|
|
|
/** |
|
286
|
|
|
* Get value of array based on attributes(keys) |
|
287
|
|
|
* @param supported php variables $attribute |
|
288
|
|
|
* @param array $data |
|
289
|
|
|
* @return supported php variables |
|
290
|
|
|
*/ |
|
291
|
|
|
private function getValue($attribute, $data) |
|
292
|
|
|
{ |
|
293
|
|
|
return isset($data[$attribute]) ? $data[$attribute] : null; |
|
294
|
|
|
} |
|
295
|
|
|
} |
|
296
|
|
|
|
If the size of the collection does not change during the iteration, it is generally a good practice to compute it beforehand, and not on each iteration: