|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace One\Model; |
|
4
|
|
|
|
|
5
|
|
|
use Psr\Http\Message\UriInterface; |
|
6
|
|
|
use One\Collection; |
|
7
|
|
|
|
|
8
|
|
|
/** |
|
9
|
|
|
* Article Class |
|
10
|
|
|
*/ |
|
11
|
|
|
class Article extends Model |
|
12
|
|
|
{ |
|
13
|
|
|
const CATEGORY_NASIONAL = 1; |
|
14
|
|
|
const CATEGORY_INTERNASIONAL = 2; |
|
15
|
|
|
const CATEGORY_BISNIS = 3; |
|
16
|
|
|
const CATEGORY_SEPAK_BOLA = 4; |
|
17
|
|
|
const CATEGORY_OLAHRAGA = 5; |
|
18
|
|
|
const CATEGORY_HIBURAN = 6; |
|
19
|
|
|
const CATEGORY_TEKNOLOGI = 7; |
|
20
|
|
|
const CATEGORY_TRAVEL = 8; |
|
21
|
|
|
const CATEGORY_LIFESTYLE = 9; |
|
22
|
|
|
const CATEGORY_WANITA = 10; |
|
23
|
|
|
const CATEGORY_HIJAB = 11; |
|
24
|
|
|
const CATEGORY_KULINER = 12; |
|
25
|
|
|
const CATEGORY_SEHAT = 13; |
|
26
|
|
|
const CATEGORY_OTOMOTIF = 14; |
|
27
|
|
|
const CATEGORY_INSPIRASI = 15; |
|
28
|
|
|
const CATEGORY_UNIK = 16; |
|
29
|
|
|
const CATEGORY_EVENT = 17; |
|
30
|
|
|
const CATEGORY_KOMUNITAS = 18; |
|
31
|
|
|
|
|
32
|
|
|
const TYPE_TEXT = 1; |
|
33
|
|
|
const TYPE_PHOTO = 2; |
|
34
|
|
|
const TYPE_VIDEO = 3; |
|
35
|
|
|
|
|
36
|
|
|
const ATTACHMENT_FIELD_PHOTO = 'photo'; |
|
37
|
|
|
const ATTACHMENT_FIELD_PAGE = 'page'; |
|
38
|
|
|
const ATTACHMENT_FIELD_VIDEO = 'video'; |
|
39
|
|
|
const ATTACHMENT_FIELD_GALLERY = 'gallery'; |
|
40
|
|
|
|
|
41
|
|
|
/** |
|
42
|
|
|
* attachment property |
|
43
|
|
|
* |
|
44
|
|
|
* @var array $attachment |
|
45
|
|
|
*/ |
|
46
|
|
|
private $attachment = array(); |
|
47
|
|
|
|
|
48
|
|
|
/** |
|
49
|
|
|
* identifier |
|
50
|
|
|
* |
|
51
|
|
|
* @var string $identifier |
|
52
|
|
|
*/ |
|
53
|
|
|
protected $identifier = null; |
|
54
|
|
|
|
|
55
|
|
|
/** |
|
56
|
|
|
* constructor |
|
57
|
|
|
* |
|
58
|
|
|
* @param string $title |
|
59
|
|
|
* @param string $body |
|
60
|
|
|
* @param \Psr\Http\Message\UriInterface|string $source |
|
61
|
|
|
* @param string $uniqueId |
|
62
|
|
|
* @param integer $typeId |
|
63
|
|
|
* @param integer $categoryId |
|
64
|
|
|
* @param string $reporter |
|
65
|
|
|
* @param string $lead |
|
66
|
|
|
* @param string $tags |
|
67
|
|
|
* @param \DateTimeInterface|string $publishedAt |
|
68
|
|
|
*/ |
|
69
|
|
|
public function __construct( |
|
70
|
|
|
$title, |
|
71
|
|
|
$body, |
|
72
|
|
|
$source, |
|
73
|
|
|
$uniqueId, |
|
74
|
|
|
$typeId = self::TYPE_TEXT, |
|
75
|
|
|
$categoryId = self::CATEGORY_NASIONAL, |
|
76
|
|
|
$reporter = '', |
|
77
|
|
|
$lead = '', |
|
78
|
|
|
$tags = '', |
|
79
|
|
|
$publishedAt = null, |
|
80
|
|
|
$identifier = null |
|
81
|
|
|
) { |
|
82
|
|
|
$source = $this->filterUriInstance($source); |
|
83
|
|
|
$publishedAt = $this->filterDateInstance($publishedAt); |
|
84
|
|
|
|
|
85
|
|
|
if (empty($lead)) { |
|
86
|
|
|
$lead = $this->createLeadFromBody($body); |
|
87
|
|
|
} |
|
88
|
|
|
|
|
89
|
|
|
$allowedType = array( |
|
90
|
|
|
self::TYPE_PHOTO, |
|
91
|
|
|
self::TYPE_TEXT, |
|
92
|
|
|
self::TYPE_VIDEO |
|
93
|
|
|
); |
|
94
|
|
|
|
|
95
|
|
|
if (!in_array($typeId, $allowedType)) { |
|
96
|
|
|
throw new \InvalidArgumentException("Invalid typeId : $typeId, allowed typeId are " . implode(', ', $allowedType)); |
|
97
|
|
|
} |
|
98
|
|
|
|
|
99
|
|
|
$allowedCategory = array( |
|
100
|
|
|
self::CATEGORY_NASIONAL, |
|
101
|
|
|
self::CATEGORY_INTERNASIONAL, |
|
102
|
|
|
self::CATEGORY_BISNIS, |
|
103
|
|
|
self::CATEGORY_SEPAK_BOLA, |
|
104
|
|
|
self::CATEGORY_OLAHRAGA, |
|
105
|
|
|
self::CATEGORY_HIBURAN, |
|
106
|
|
|
self::CATEGORY_TEKNOLOGI, |
|
107
|
|
|
self::CATEGORY_TRAVEL, |
|
108
|
|
|
self::CATEGORY_LIFESTYLE, |
|
109
|
|
|
self::CATEGORY_WANITA, |
|
110
|
|
|
self::CATEGORY_HIJAB, |
|
111
|
|
|
self::CATEGORY_KULINER, |
|
112
|
|
|
self::CATEGORY_SEHAT, |
|
113
|
|
|
self::CATEGORY_OTOMOTIF, |
|
114
|
|
|
self::CATEGORY_INSPIRASI, |
|
115
|
|
|
self::CATEGORY_UNIK, |
|
116
|
|
|
self::CATEGORY_EVENT, |
|
117
|
|
|
self::CATEGORY_KOMUNITAS, |
|
118
|
|
|
); |
|
119
|
|
|
|
|
120
|
|
|
if (!in_array($categoryId, $allowedCategory)) { |
|
121
|
|
|
throw new \InvalidArgumentException("Invalid categoryId : $categoryId, allowed category are " . implode(', ', $allowedCategory)); |
|
122
|
|
|
} |
|
123
|
|
|
|
|
124
|
|
|
$this->collection = new Collection( |
|
125
|
|
|
array( |
|
126
|
|
|
'title' => $title, |
|
127
|
|
|
'reporter' => $reporter, |
|
128
|
|
|
'lead' => $lead, |
|
129
|
|
|
'body' => $body, |
|
130
|
|
|
'source' => $source, |
|
131
|
|
|
'uniqueId' => $uniqueId, |
|
132
|
|
|
'type_id' => $typeId, |
|
133
|
|
|
'category_id' => $categoryId, |
|
134
|
|
|
'tags' => $tags, |
|
135
|
|
|
'published_at' => $publishedAt |
|
136
|
|
|
) |
|
137
|
|
|
); |
|
138
|
|
|
|
|
139
|
|
|
if ($identifier) { |
|
140
|
|
|
$this->setId($identifier); |
|
141
|
|
|
} |
|
142
|
|
|
} |
|
143
|
|
|
|
|
144
|
|
|
/** |
|
145
|
|
|
* get ALL Possible attachment for an article, return arrays of field name. Used for consistency accross sdk |
|
146
|
|
|
* leveraging php version 5.3 cannot use array constant |
|
147
|
|
|
* |
|
148
|
|
|
* @return array |
|
149
|
|
|
*/ |
|
150
|
|
|
public static function getPossibleAttachment() |
|
151
|
|
|
{ |
|
152
|
|
|
return array_merge( |
|
153
|
|
|
self::getDeleteableAttachment(), |
|
154
|
|
|
array( |
|
155
|
|
|
self::ATTACHMENT_FIELD_PHOTO |
|
156
|
|
|
) |
|
157
|
|
|
); |
|
158
|
|
|
} |
|
159
|
|
|
|
|
160
|
|
|
/** |
|
161
|
|
|
* get deleteable attachment for constant usage across sdk |
|
162
|
|
|
* |
|
163
|
|
|
* @return array |
|
164
|
|
|
*/ |
|
165
|
|
|
public static function getDeleteableAttachment() |
|
166
|
|
|
{ |
|
167
|
|
|
return array( |
|
168
|
|
|
self::ATTACHMENT_FIELD_GALLERY, |
|
169
|
|
|
self::ATTACHMENT_FIELD_PAGE, |
|
170
|
|
|
self::ATTACHMENT_FIELD_VIDEO |
|
171
|
|
|
); |
|
172
|
|
|
} |
|
173
|
|
|
|
|
174
|
|
|
/** |
|
175
|
|
|
* setIdentifier from rest api response |
|
176
|
|
|
* |
|
177
|
|
|
* @param string $identifier |
|
178
|
|
|
* @return void |
|
179
|
|
|
*/ |
|
180
|
|
|
public function setId($identifier) |
|
181
|
|
|
{ |
|
182
|
|
|
$this->identifier = $identifier; |
|
183
|
|
|
} |
|
184
|
|
|
|
|
185
|
|
|
/** |
|
186
|
|
|
* getIdentifier set before |
|
187
|
|
|
* |
|
188
|
|
|
* @return string |
|
189
|
|
|
*/ |
|
190
|
|
|
public function getId() |
|
191
|
|
|
{ |
|
192
|
|
|
return $this->identifier; |
|
193
|
|
|
} |
|
194
|
|
|
|
|
195
|
|
|
/** |
|
196
|
|
|
* check if this object has attachment assigned to it |
|
197
|
|
|
* |
|
198
|
|
|
* @param string $field |
|
199
|
|
|
* @return boolean |
|
200
|
|
|
*/ |
|
201
|
|
|
public function hasAttachment($field) |
|
202
|
|
|
{ |
|
203
|
|
|
return isset($this->attachment[$field]); |
|
204
|
|
|
} |
|
205
|
|
|
|
|
206
|
|
|
/** |
|
207
|
|
|
* getAttachment based on fields |
|
208
|
|
|
* |
|
209
|
|
|
* @param string $field |
|
210
|
|
|
* @return array |
|
211
|
|
|
*/ |
|
212
|
|
|
public function getAttachmentByField($field) |
|
213
|
|
|
{ |
|
214
|
|
|
if (isset($this->attachment[$field])) { |
|
215
|
|
|
return $this->attachment[$field]; |
|
216
|
|
|
} |
|
217
|
|
|
|
|
218
|
|
|
return array(); |
|
219
|
|
|
} |
|
220
|
|
|
|
|
221
|
|
|
/** |
|
222
|
|
|
* get ALL attachment assigned to this object |
|
223
|
|
|
* |
|
224
|
|
|
* @return array|null |
|
225
|
|
|
*/ |
|
226
|
|
|
public function getAttachments() |
|
227
|
|
|
{ |
|
228
|
|
|
return $this->attachment; |
|
229
|
|
|
} |
|
230
|
|
|
|
|
231
|
|
|
/** |
|
232
|
|
|
* add attach an attachment to this model |
|
233
|
|
|
* |
|
234
|
|
|
* @param string $field |
|
235
|
|
|
* @param Model $item |
|
236
|
|
|
* @return Article |
|
237
|
|
|
*/ |
|
238
|
|
|
public function attach($field, Model $item) |
|
239
|
|
|
{ |
|
240
|
|
|
$this->attachment[$field][] = $item; |
|
241
|
|
|
|
|
242
|
|
|
return $this; |
|
243
|
|
|
} |
|
244
|
|
|
|
|
245
|
|
|
/** |
|
246
|
|
|
* Attach Photo Attachment to article |
|
247
|
|
|
* |
|
248
|
|
|
* @param Photo $photo |
|
249
|
|
|
* @return self |
|
250
|
|
|
*/ |
|
251
|
|
|
public function attachPhoto(Photo $photo) |
|
252
|
|
|
{ |
|
253
|
|
|
return $this->attach( |
|
254
|
|
|
self::ATTACHMENT_FIELD_PHOTO, |
|
255
|
|
|
$photo |
|
256
|
|
|
); |
|
257
|
|
|
} |
|
258
|
|
|
|
|
259
|
|
|
/** |
|
260
|
|
|
* Attach Paging |
|
261
|
|
|
* |
|
262
|
|
|
* @param Page $page |
|
263
|
|
|
* @return self |
|
264
|
|
|
*/ |
|
265
|
|
|
public function attachPage(Page $page) |
|
266
|
|
|
{ |
|
267
|
|
|
return $this->attach( |
|
268
|
|
|
self::ATTACHMENT_FIELD_PAGE, |
|
269
|
|
|
$this->ensureOrder( |
|
|
|
|
|
|
270
|
|
|
$page, |
|
271
|
|
|
self::ATTACHMENT_FIELD_PAGE |
|
272
|
|
|
) |
|
273
|
|
|
); |
|
274
|
|
|
} |
|
275
|
|
|
|
|
276
|
|
|
/** |
|
277
|
|
|
* Attach gallery here |
|
278
|
|
|
* |
|
279
|
|
|
* @param Gallery $gallery |
|
280
|
|
|
* @return self |
|
281
|
|
|
*/ |
|
282
|
|
|
public function attachGallery(Gallery $gallery) |
|
283
|
|
|
{ |
|
284
|
|
|
return $this->attach( |
|
285
|
|
|
self::ATTACHMENT_FIELD_GALLERY, |
|
286
|
|
|
$this->ensureOrder( |
|
|
|
|
|
|
287
|
|
|
$gallery, |
|
288
|
|
|
self::ATTACHMENT_FIELD_GALLERY |
|
289
|
|
|
) |
|
290
|
|
|
); |
|
291
|
|
|
} |
|
292
|
|
|
|
|
293
|
|
|
/** |
|
294
|
|
|
* attach Video |
|
295
|
|
|
* |
|
296
|
|
|
* @param Video $video |
|
297
|
|
|
* @return self |
|
298
|
|
|
*/ |
|
299
|
|
|
public function attachVideo(Video $video) |
|
300
|
|
|
{ |
|
301
|
|
|
return $this->attach( |
|
302
|
|
|
self::ATTACHMENT_FIELD_VIDEO, |
|
303
|
|
|
$this->ensureOrder( |
|
|
|
|
|
|
304
|
|
|
$video, |
|
305
|
|
|
self::ATTACHMENT_FIELD_VIDEO |
|
306
|
|
|
) |
|
307
|
|
|
); |
|
308
|
|
|
} |
|
309
|
|
|
|
|
310
|
|
|
/** |
|
311
|
|
|
* ensuring order |
|
312
|
|
|
* |
|
313
|
|
|
* @param Model|Collection $attachment |
|
314
|
|
|
* @param string $type |
|
315
|
|
|
* @return Model|Collection |
|
316
|
|
|
*/ |
|
317
|
|
|
private function ensureOrder($attachment, $type) |
|
318
|
|
|
{ |
|
319
|
|
|
if (empty($attachment->get('order'))) { |
|
|
|
|
|
|
320
|
|
|
$attachment->set( |
|
|
|
|
|
|
321
|
|
|
'order', |
|
322
|
|
|
count($this->getAttachmentByField($type)) + 1 |
|
323
|
|
|
); |
|
324
|
|
|
} |
|
325
|
|
|
|
|
326
|
|
|
return $attachment; |
|
327
|
|
|
} |
|
328
|
|
|
} |
|
329
|
|
|
|
This check looks at variables that are passed out again to other methods.
If the outgoing method call has stricter type requirements than the method itself, an issue is raised.
An additional type check may prevent trouble.