Passed
Pull Request — master (#21)
by kenny
02:25
created

FormatMapping::getValue()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
nc 2
nop 2
dl 0
loc 2
rs 10
c 0
b 0
f 0
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
	 * Json Attributes of Photo
15
	 * @var array
16
	 */
17
	private $photoAttributes;
18
19
	/**
20
	 * Json Attributes of Page
21
	 * @var array
22
	 */
23
	private $pageAttributes;
24
25
	/**
26
	 * Json Attributes of gallery
27
	 * @var array
28
	 */
29
	private $galleryAttributes;
30
31
	/**
32
	 * Json Attributes of video
33
	 * @var array
34
	 */
35
	private $videoAttributes;
36
37
	/**
38
	 * Construct Json attribute variables
39
	 */
40
	public function __construct() {
41
		$this->photoAttributes = array(
42
			"photo_id",
43
			"photo_url",
44
			"photo_ratio",
45
			"photo_description",
46
			"photo_information",
47
		);
48
49
		$this->pageAttributes = array(
50
			"page_id",
51
			"page_title",
52
			"page_lead",
53
			"page_body",
54
			"page_source",
55
			"page_order",
56
			"page_cover",
57
		);
58
59
		$this->galleryAttributes = array(
60
			"gallery_id",
61
			"gallery_lead",
62
			"gallery_body",
63
			"gallery_source",
64
			"gallery_order",
65
			"gallery_photo",
66
		);
67
68
		$this->videoAttributes = array(
69
			"video_id",
70
			"video_lead",
71
			"video_body",
72
			"video_source",
73
			"video_order",
74
			"video_cover",
75
		);
76
	}
77
	/**
78
	 * map a single article to main attributes in Article Class
79
	 * @param  string $singleJsonArticle JSON response
80
	 * @return Article
81
	 * @throws Exception
82
	 */
83
	public function article($singleJsonArticle) {
84
		if (json_decode($singleJsonArticle, true)) {
85
			$dataArticle = json_decode($singleJsonArticle, true)['data'];
86
87
			$article = new Article(
88
89
				$title = $this->filterString($this->getValue('title', $dataArticle)),
90
91
				$body = $this->filterString($this->getValue('body', $dataArticle)),
92
93
				$source = $this->filterString($this->getValue('source', $dataArticle)),
94
95
				$uniqueId = $this->getValue('unique_id', $dataArticle),
96
97
				$typeId = $this->filterInteger($this->getValue('type_id', $dataArticle['type'])),
98
99
				$categoryId = $this->filterInteger($this->getValue(
100
					'category_id',
101
					$dataArticle['category']
102
				)),
103
104
				$reporter = $this->getValue('reporter', $dataArticle),
105
106
				$lead = $this->filterString($this->getValue('lead', $dataArticle)),
107
108
				$tags = $this->getValue('tag_name', $dataArticle['tags']),
109
110
				$publishedAt = $this->filterString($this->getValue('published_at', $dataArticle)),
111
112
				$identifier = $this->filterInteger($this->getValue('id', $dataArticle))
113
114
			);
115
116
			$photo_attachments = $this->attachment('photos', $this->photoAttributes, $dataArticle);
117
118
			$page_attachments = $this->attachment('pages', $this->pageAttributes, $dataArticle);
119
120
			$gallery_attachments = $this->attachment('galleries', $this->galleryAttributes, $dataArticle);
121
122
			$video_attachments = $this->attachment('videos', $this->videoAttributes, $dataArticle);
123
124
			for ($i = 0; $i < $photo_attachments['numberOfItems']; $i++) {
125
				$article->attachPhoto($photo_attachments['attachments'][$i]);
126
			}
127
128
			for ($i = 0; $i < $page_attachments['numberOfItems']; $i++) {
129
				$article->attachPage($page_attachments['attachments'][$i]);
130
			}
131
132
			for ($i = 0; $i < $gallery_attachments['numberOfItems']; $i++) {
133
				$article->attachGallery($gallery_attachments['attachments'][$i]);
134
			}
135
136
			for ($i = 0; $i < $video_attachments['numberOfItems']; $i++) {
137
				$article->attachVideo($video_attachments['attachments'][$i]);
138
			}
139
140
			return $article;
141
		}
142
143
		throw new \Exception("Empty or invalid JSON Response", 1);
144
	}
145
146
	/**
147
	 * Attachment(s) of a single article
148
	 * @param  string $attachmentType
149
	 * @param  array $attributes
150
	 * @param  assoc array $dataArticle
151
	 * @return array attachments
152
	 */
153
	private function attachment($attachmentType, $attributes, $dataArticle) {
154
		$data = $dataArticle[$attachmentType];
155
156
		$encoded = json_encode($data);
157
158
		$attachments = array();
159
160
		if ($this->filterArray($data)) {
161
			$decodedData = json_decode($encoded, true);
162
163
			$numberOfItems = count($decodedData);
164
165
			for ($i = 0; $i < $numberOfItems; $i++) {
166
				$item = $decodedData[$i];
167
168
				$attachment = $this->makeAttachmentObject($attachmentType, $attributes, $item);
169
170
				array_push($attachments, $attachment);
171
			}
172
		}
173
174
		$structure = array(
175
			'numberOfItems' => count($attachments),
176
			'attachments' => $attachments,
177
		);
178
179
		return $structure;
180
	}
181
182
	/**
183
	 * Make attachment object
184
	 * @param  string $attachmentType json field of attachment
185
	 * @param  array  $attributes     attributes of attachment
186
	 * @param  assoc array $item
187
	 * @return object
188
	 */
189
	private function makeAttachmentObject($attachmentType, $attributes, $item) {
190
		$numOfAttributes = count($attributes);
191
192
		$values = array();
193
194
		$object = null;
195
196
		for ($i = 0; $i < $numOfAttributes; $i++) {
197
			$val = $this->getValue($attributes[$i], $item);
198
			$values[$attributes[$i]] = $val;
199
		}
200
201
		extract($values);
202
203
		if ($attachmentType == 'photos') {
204
			$object = new Photo(
205
				$photo_url,
206
				$this->photoRatio($photo_ratio),
207
				'',
208
				''
209
			);
210
		} elseif ($attachmentType == 'pages') {
211
			$object = new Page(
212
				$page_title,
213
				$page_body,
214
				$page_source,
215
				$page_order,
216
				$page_cover,
217
				$page_lead
218
			);
219
		} elseif ($attachmentType == 'galleries') {
220
			$object = new Gallery(
221
				$gallery_body,
222
				$gallery_order,
223
				$gallery_photo,
224
				$gallery_source,
225
				$gallery_lead
226
			);
227
		} elseif ($attachmentType == 'videos') {
228
			$object = new Video(
229
230
				$video_body,
231
				$video_source,
232
				$video_order,
233
				$video_cover,
234
				$video_lead
235
			);
236
		}
237
238
		return $this->filterAttachmentObject($object);
239
	}
240
241
	/**
242
	 * Map ratio to photo ratio constants
243
	 * @param   string $ratio
244
	 * @return   string
245
	 * @throws Exception
246
	 */
247
	private function photoRatio($ratio) {
248
		if ($ratio == "1:1") {
249
			return Photo::RATIO_SQUARE;
250
		} elseif ($ratio == "2:1") {
251
			return Photo::RATIO_RECTANGLE;
252
		} elseif ($ratio == "3:2") {
253
			return Photo::RATIO_HEADLINE;
254
		} elseif ($ratio == "9:16") {
255
			return Photo::RATIO_VERTICAL;
256
		} elseif ($ratio == 'cover') {
257
			return Photo::RATIO_COVER;
258
		} else {
259
			throw new \Exception("Unknown ratio", 1);
260
		}
261
	}
262
263
	/**
264
	 * Make sure value is integer
265
	 * @param  mixed $int
266
	 * @return int
267
	 * @throws Exception
268
	 */
269
	private function filterInteger($int) {
270
		if (is_int((int) $int)) {
0 ignored issues
show
introduced by
The condition is_int((int)$int) is always true.
Loading history...
271
			return $int;
272
		}
273
		throw new \Exception("Invalid Integer", 1);
274
	}
275
276
	/**
277
	 * Make sure string is not null or empty
278
	 * @param   mixed $str
279
	 * @return string if it is valid or exception
280
	 * @throws Exception
281
	 */
282
	private function filterString($str) {
283
		if (is_string($str) && strlen($str) > 0 && !is_null($str)) {
284
			return $str;
285
		}
286
		throw new \Exception("String required", 1);
287
	}
288
289
	/**
290
	 * Make sure variable is type of array
291
	 * @param  mixed $array
292
	 * @return array
293
	 * @throws Exception
294
	 */
295
	private function filterArray($array) {
296
		if (is_array($array)) {
297
			return $array;
298
		}
299
		throw new \Exception("Array required", 1);
300
	}
301
302
	/**
303
	 * Make sure attachment object not null
304
	 * @param  mixed $object
305
	 * @return object
306
	 * @throws Exception
307
	 */
308
	private function filterAttachmentObject($object) {
309
		if (!is_null($object)) {
310
			return $object;
311
		}
312
		throw new \Exception("Attachment object required", 1);
313
	}
314
315
	/**
316
	 * Get value of array based on attributes(keys)
317
	 * @param  mixed $attribute
318
	 * @param  array $data
319
	 * @return mixed
320
	 */
321
	private function getValue($attribute, $data) {
322
		return isset($data[$attribute]) ? $data[$attribute] : null;
323
	}
324
}
325