1 | <?php declare(strict_types=1); |
||
2 | |||
3 | namespace One\Model; |
||
4 | |||
5 | use One\Collection; |
||
6 | |||
7 | /** |
||
8 | * Article Class |
||
9 | */ |
||
10 | class Article extends Model |
||
11 | { |
||
12 | public const CATEGORY_NASIONAL = 1; |
||
13 | |||
14 | public const CATEGORY_INTERNASIONAL = 2; |
||
15 | |||
16 | public const CATEGORY_BISNIS = 3; |
||
17 | |||
18 | public const CATEGORY_SEPAK_BOLA = 4; |
||
19 | |||
20 | public const CATEGORY_OLAHRAGA = 5; |
||
21 | |||
22 | public const CATEGORY_HIBURAN = 6; |
||
23 | |||
24 | public const CATEGORY_TEKNOLOGI = 7; |
||
25 | |||
26 | public const CATEGORY_TRAVEL = 8; |
||
27 | |||
28 | public const CATEGORY_LIFESTYLE = 9; |
||
29 | |||
30 | public const CATEGORY_WANITA = 10; |
||
31 | |||
32 | public const CATEGORY_HIJAB = 11; |
||
33 | |||
34 | public const CATEGORY_KULINER = 12; |
||
35 | |||
36 | public const CATEGORY_SEHAT = 13; |
||
37 | |||
38 | public const CATEGORY_OTOMOTIF = 14; |
||
39 | |||
40 | public const CATEGORY_INSPIRASI = 15; |
||
41 | |||
42 | public const CATEGORY_UNIK = 16; |
||
43 | |||
44 | public const CATEGORY_EVENT = 17; |
||
45 | |||
46 | public const CATEGORY_KOMUNITAS = 18; |
||
47 | |||
48 | public const CATEGORY_E_SPORTS = 19; |
||
49 | |||
50 | public const CATEGORY_DANGDUT = 20; |
||
51 | |||
52 | public const CATEGORY_RAMADAN = 21; |
||
53 | |||
54 | public const CATEGORY_PEMILU = 22; |
||
55 | |||
56 | public const CATEGORY_CEK_FAKTA = 25; |
||
57 | |||
58 | public const CATEGORY_HAJI = 26; |
||
59 | |||
60 | public const CATEGORY_MOTO_GP = 28; |
||
61 | |||
62 | public const CATEGORY_PHOTO = 29; |
||
63 | |||
64 | public const CATEGORY_VIDEO = 30; |
||
65 | |||
66 | public const CATEGORY_PICTURE_FIRST = 46; |
||
67 | |||
68 | public const CATEGORY_LAIN_LAIN = 47; |
||
69 | |||
70 | public const CATEGORY_CRYPTO = 49; |
||
71 | |||
72 | public const CATEGORY_JATIM = 50; |
||
73 | |||
74 | public const CATEGORY_JATENG = 51; |
||
75 | |||
76 | public const CATEGORY_SAHAM = 52; |
||
77 | |||
78 | public const CATEGORY_ISLAMI = 34; |
||
79 | |||
80 | public const CATEGORY_PIALA_DUNIA = 53; |
||
81 | |||
82 | public const CATEGORY_TV = 324; |
||
83 | |||
84 | public const CATEGORY_FEEDS = 328; |
||
85 | |||
86 | public const CATEGORY_PICTURE_FIRST_STAGING = 137; |
||
87 | |||
88 | public const CATEGORY_CRYPTO_STAGING = 139; |
||
89 | |||
90 | public const CATEGORY_JATIM_STAGING = 140; |
||
91 | |||
92 | public const CATEGORY_JATENG_STAGING = 141; |
||
93 | |||
94 | public const CATEGORY_SAHAM_STAGING = 142; |
||
95 | |||
96 | public const CATEGORY_ISLAMI_STAGING = 122; |
||
97 | |||
98 | public const CATEGORY_PIALA_DUNIA_STAGING = 130; |
||
99 | |||
100 | public const CATEGORY_LAIN_LAIN_STAGING = 315; |
||
101 | |||
102 | public const CATEGORY_FEEDS_STAGING = 336; |
||
103 | |||
104 | public const CATEGORY_VIDEO_STAGING = 350; |
||
105 | |||
106 | public const CATEGORY_MOTO_GP_STAGING = 351; |
||
107 | |||
108 | public const TYPE_TEXT = 1; |
||
109 | |||
110 | public const TYPE_PHOTO = 2; |
||
111 | |||
112 | public const TYPE_VIDEO = 3; |
||
113 | |||
114 | public const ATTACHMENT_FIELD_PHOTO = 'photo'; |
||
115 | |||
116 | public const ATTACHMENT_FIELD_PAGE = 'page'; |
||
117 | |||
118 | public const ATTACHMENT_FIELD_VIDEO = 'video'; |
||
119 | |||
120 | public const ATTACHMENT_FIELD_GALLERY = 'gallery'; |
||
121 | |||
122 | /** |
||
123 | * identifier |
||
124 | * |
||
125 | * @var string |
||
126 | */ |
||
127 | protected $identifier = null; |
||
128 | |||
129 | /** |
||
130 | * attachment property |
||
131 | * |
||
132 | * @var array<mixed> |
||
133 | */ |
||
134 | private $attachment = []; |
||
135 | |||
136 | /** |
||
137 | * constructor |
||
138 | * |
||
139 | * @param \Psr\Http\Message\UriInterface|string $source |
||
140 | * @param string $uniqueId |
||
141 | * @param integer $typeId |
||
142 | * @param integer $categoryId |
||
143 | * @param string $reporter |
||
144 | * @param string $lead |
||
145 | * @param string $tags |
||
146 | * @param \DateTimeInterface|string $publishedAt |
||
147 | * @param string $identifier |
||
148 | * @param boolean $headline |
||
149 | * @param boolean $headline_lip6 |
||
150 | * @param boolean $seo |
||
151 | * @param string $photographer |
||
152 | * @param string $category |
||
153 | * @param string $editor |
||
154 | * @param boolean $curated |
||
155 | * @param string $pin |
||
156 | * @param boolean $recommendation |
||
157 | * @param boolean $timeless |
||
158 | * @param boolean $headlineCategory |
||
159 | * @param integer $aiSource |
||
160 | */ |
||
161 | public function __construct( |
||
162 | string $title, |
||
163 | string $body, |
||
164 | $source, |
||
165 | $uniqueId, |
||
166 | $typeId = self::TYPE_TEXT, |
||
167 | $categoryId = self::CATEGORY_NASIONAL, |
||
168 | $reporter = '', |
||
169 | $lead = '', |
||
170 | $tags = '', |
||
171 | $publishedAt = null, |
||
172 | $identifier = null, |
||
173 | $headline = false, |
||
174 | $headlineLip6 = false, |
||
175 | $seo = false, |
||
176 | $photographer = '', |
||
177 | $category = '', |
||
178 | $editor = '', |
||
179 | $curated = false, |
||
180 | $pin = '', |
||
181 | $recommendation = false, |
||
182 | $timeless = false, |
||
183 | $headlineCategory = false, |
||
184 | $aiType = 0 |
||
185 | ) { |
||
186 | if (! in_array($typeId, [self::TYPE_PHOTO, self::TYPE_TEXT, self::TYPE_VIDEO], true)) { |
||
187 | throw new \InvalidArgumentException("Invalid typeId : ${typeId}, allowed typeId are " . implode(', ', $allowedType)); |
||
0 ignored issues
–
show
Comprehensibility
Best Practice
introduced
by
![]() |
|||
188 | } |
||
189 | |||
190 | $allowedCategory = [ |
||
191 | self::CATEGORY_NASIONAL, |
||
192 | self::CATEGORY_INTERNASIONAL, |
||
193 | self::CATEGORY_BISNIS, |
||
194 | self::CATEGORY_SEPAK_BOLA, |
||
195 | self::CATEGORY_OLAHRAGA, |
||
196 | self::CATEGORY_HIBURAN, |
||
197 | self::CATEGORY_TEKNOLOGI, |
||
198 | self::CATEGORY_TRAVEL, |
||
199 | self::CATEGORY_LIFESTYLE, |
||
200 | self::CATEGORY_WANITA, |
||
201 | self::CATEGORY_HIJAB, |
||
202 | self::CATEGORY_KULINER, |
||
203 | self::CATEGORY_SEHAT, |
||
204 | self::CATEGORY_OTOMOTIF, |
||
205 | self::CATEGORY_INSPIRASI, |
||
206 | self::CATEGORY_UNIK, |
||
207 | self::CATEGORY_EVENT, |
||
208 | self::CATEGORY_KOMUNITAS, |
||
209 | self::CATEGORY_E_SPORTS, |
||
210 | self::CATEGORY_DANGDUT, |
||
211 | self::CATEGORY_RAMADAN, |
||
212 | self::CATEGORY_PEMILU, |
||
213 | self::CATEGORY_CEK_FAKTA, |
||
214 | self::CATEGORY_HAJI, |
||
215 | self::CATEGORY_PHOTO, |
||
216 | self::CATEGORY_VIDEO, |
||
217 | self::CATEGORY_VIDEO_STAGING, |
||
218 | self::CATEGORY_PICTURE_FIRST, |
||
219 | self::CATEGORY_PICTURE_FIRST_STAGING, |
||
220 | self::CATEGORY_JATIM, |
||
221 | self::CATEGORY_JATIM_STAGING, |
||
222 | self::CATEGORY_JATENG, |
||
223 | self::CATEGORY_JATENG_STAGING, |
||
224 | self::CATEGORY_CRYPTO, |
||
225 | self::CATEGORY_CRYPTO_STAGING, |
||
226 | self::CATEGORY_SAHAM, |
||
227 | self::CATEGORY_SAHAM_STAGING, |
||
228 | self::CATEGORY_ISLAMI, |
||
229 | self::CATEGORY_ISLAMI_STAGING, |
||
230 | self::CATEGORY_PIALA_DUNIA, |
||
231 | self::CATEGORY_PIALA_DUNIA_STAGING, |
||
232 | self::CATEGORY_LAIN_LAIN, |
||
233 | self::CATEGORY_LAIN_LAIN_STAGING, |
||
234 | self::CATEGORY_TV, |
||
235 | self::CATEGORY_MOTO_GP, |
||
236 | self::CATEGORY_MOTO_GP_STAGING, |
||
237 | self::CATEGORY_FEEDS, |
||
238 | self::CATEGORY_FEEDS_STAGING |
||
239 | ]; |
||
240 | |||
241 | if (! in_array($categoryId, $allowedCategory, true)) { |
||
242 | throw new \InvalidArgumentException("Invalid categoryId : ${categoryId}, allowed category are " . implode(', ', $allowedCategory)); |
||
243 | } |
||
244 | |||
245 | $this->collection = new Collection([ |
||
246 | 'title' => $this->filterStringInstance($title), |
||
247 | 'reporter' => $this->filterStringInstance($reporter), |
||
248 | 'lead' => empty($lead) ? $this->createLeadFromBody($body) : $this->filterStringInstance($lead), |
||
249 | 'body' => $this->filterStringInstance($body), |
||
250 | 'source' => $this->filterUriInstance($source), |
||
251 | 'uniqueId' => $uniqueId, |
||
252 | 'type_id' => $typeId, |
||
253 | 'category_id' => $categoryId, |
||
254 | 'tags' => $this->filterStringInstance($tags), |
||
255 | 'published_at' => $this->filterDateInstance($publishedAt), |
||
256 | 'headline' => $headline, |
||
257 | 'headline_lip6' => $headlineLip6 ? 1 : 0, |
||
258 | 'seo' => $seo ? 1 : 0, |
||
259 | 'category' => $this->filterStringInstance($category), |
||
260 | 'curated' => $curated ? 1 : 0, |
||
261 | 'recommendation' => $recommendation ? 1 : 0, |
||
262 | 'timeless' => $timeless ? 1 : 0, |
||
263 | 'headline_category' => $headlineCategory ? 1 : 0, |
||
264 | 'ai_type' => $aiType |
||
265 | ]); |
||
266 | |||
267 | if (!empty($this->filterStringInstance($photographer))) { |
||
268 | $this->collection->offsetSet('photographer', $this->filterStringInstance($photographer)); |
||
269 | } |
||
270 | |||
271 | if (!empty($this->filterStringInstance($editor))) { |
||
272 | $this->collection->offsetSet('editor', $this->filterStringInstance($editor)); |
||
273 | } |
||
274 | |||
275 | if (!empty($this->filterStringInstance($pin))) { |
||
276 | $this->collection->offsetSet('pin', $this->filterStringInstance($pin)); |
||
277 | } |
||
278 | |||
279 | if ($identifier) { |
||
280 | $this->setId((string) $identifier); |
||
281 | } |
||
282 | } |
||
283 | |||
284 | /** |
||
285 | * get ALL Possible attachment for an article, return arrays of field name. Used for consistency accross sdk |
||
286 | * leveraging php version 5.3 cannot use array constant |
||
287 | * |
||
288 | * @return string[] |
||
289 | */ |
||
290 | public static function getPossibleAttachment(): array |
||
291 | { |
||
292 | return array_merge( |
||
293 | self::getDeleteableAttachment(), |
||
294 | [ |
||
295 | self::ATTACHMENT_FIELD_PHOTO, |
||
296 | ] |
||
297 | ); |
||
298 | } |
||
299 | |||
300 | /** |
||
301 | * get deleteable attachment for constant usage across sdk |
||
302 | * |
||
303 | * @return string[] |
||
304 | */ |
||
305 | public static function getDeleteableAttachment(): array |
||
306 | { |
||
307 | return [ |
||
308 | self::ATTACHMENT_FIELD_GALLERY, |
||
309 | self::ATTACHMENT_FIELD_PAGE, |
||
310 | self::ATTACHMENT_FIELD_VIDEO, |
||
311 | ]; |
||
312 | } |
||
313 | |||
314 | /** |
||
315 | * setIdentifier from rest api response |
||
316 | */ |
||
317 | public function setId(string $identifier): void |
||
318 | { |
||
319 | $this->identifier = $identifier; |
||
320 | } |
||
321 | |||
322 | /** |
||
323 | * getIdentifier set before |
||
324 | */ |
||
325 | public function getId(): string |
||
326 | { |
||
327 | return $this->identifier; |
||
328 | } |
||
329 | |||
330 | /** |
||
331 | * set as headline |
||
332 | */ |
||
333 | public function setHeadline(bool $headline = true): void |
||
334 | { |
||
335 | $this->collection['headline'] = $headline; |
||
336 | } |
||
337 | |||
338 | /** |
||
339 | * check is headline |
||
340 | */ |
||
341 | public function isHeadline(): bool |
||
342 | { |
||
343 | return $this->collection['headline']; |
||
344 | } |
||
345 | |||
346 | public function setHeadlineLip6(bool $headline = true): void |
||
347 | { |
||
348 | $this->collection['headline_lip6'] = $headline; |
||
349 | } |
||
350 | |||
351 | public function isHeadlineLip6(): bool |
||
352 | { |
||
353 | return $this->collection['headline_lip6']; |
||
354 | } |
||
355 | |||
356 | /** |
||
357 | * check if this object has attachment assigned to it |
||
358 | */ |
||
359 | public function hasAttachment(string $field): bool |
||
360 | { |
||
361 | return isset($this->attachment[$field]); |
||
362 | } |
||
363 | |||
364 | /** |
||
365 | * getAttachment based on fields |
||
366 | */ |
||
367 | public function getAttachmentByField(string $field): array |
||
368 | { |
||
369 | if (isset($this->attachment[$field])) { |
||
370 | return $this->attachment[$field]; |
||
371 | } |
||
372 | |||
373 | return []; |
||
374 | } |
||
375 | |||
376 | /** |
||
377 | * get ALL attachment assigned to this object |
||
378 | */ |
||
379 | public function getAttachments(): ?array |
||
380 | { |
||
381 | return $this->attachment; |
||
382 | } |
||
383 | |||
384 | /** |
||
385 | * add attach an attachment to this model |
||
386 | */ |
||
387 | public function attach(string $field, Model $item): self |
||
388 | { |
||
389 | $this->attachment[$field][] = $item; |
||
390 | |||
391 | return $this; |
||
392 | } |
||
393 | |||
394 | /** |
||
395 | * Attach Photo Attachment to article |
||
396 | */ |
||
397 | public function attachPhoto(Photo $photo): self |
||
398 | { |
||
399 | return $this->attach( |
||
400 | self::ATTACHMENT_FIELD_PHOTO, |
||
401 | $photo |
||
402 | ); |
||
403 | } |
||
404 | |||
405 | /** |
||
406 | * Attach Paging |
||
407 | */ |
||
408 | public function attachPage(Page $page): self |
||
409 | { |
||
410 | return $this->attach( |
||
411 | self::ATTACHMENT_FIELD_PAGE, |
||
412 | $this->ensureOrder( |
||
413 | $page, |
||
414 | self::ATTACHMENT_FIELD_PAGE |
||
415 | ) |
||
416 | ); |
||
417 | } |
||
418 | |||
419 | /** |
||
420 | * Attach gallery here |
||
421 | */ |
||
422 | public function attachGallery(Gallery $gallery): self |
||
423 | { |
||
424 | return $this->attach( |
||
425 | self::ATTACHMENT_FIELD_GALLERY, |
||
426 | $this->ensureOrder( |
||
427 | $gallery, |
||
428 | self::ATTACHMENT_FIELD_GALLERY |
||
429 | ) |
||
430 | ); |
||
431 | } |
||
432 | |||
433 | /** |
||
434 | * attach Video |
||
435 | */ |
||
436 | public function attachVideo(Video $video): self |
||
437 | { |
||
438 | return $this->attach( |
||
439 | self::ATTACHMENT_FIELD_VIDEO, |
||
440 | $this->ensureOrder( |
||
441 | $video, |
||
442 | self::ATTACHMENT_FIELD_VIDEO |
||
443 | ) |
||
444 | ); |
||
445 | } |
||
446 | |||
447 | /** |
||
448 | * ensuring order |
||
449 | */ |
||
450 | private function ensureOrder(Model $attachment, string $type): Model |
||
451 | { |
||
452 | $attachmentOrder = $attachment->get('order'); |
||
453 | |||
454 | if (empty($attachmentOrder)) { |
||
455 | $attachment->set( |
||
456 | 'order', |
||
457 | count($this->getAttachmentByField($type)) + 1 |
||
458 | ); |
||
459 | } |
||
460 | |||
461 | return $attachment; |
||
462 | } |
||
463 | } |
||
464 |