1
|
|
|
<?php namespace FlatPlan; |
2
|
|
|
|
3
|
|
|
use FlatPlan\Components\AbstractComponent; |
4
|
|
|
|
5
|
|
|
class Article { |
6
|
|
|
|
7
|
|
|
private $components = array(); |
8
|
|
|
private $status; |
9
|
|
|
private $article_id; |
10
|
|
|
private $title; |
11
|
|
|
private $sub_title; |
12
|
|
|
private $language = 'en'; |
13
|
|
|
private $category; |
14
|
|
|
private $template; |
15
|
|
|
private $metaData = null; |
16
|
|
|
private $parameters = null; |
17
|
|
|
private $localization = null; |
18
|
|
|
private $allowedMetaKeys = array( |
19
|
|
|
'authors' => 'array', |
20
|
|
|
'campaignData' => 'array', |
21
|
|
|
'canonicalURL' => 'uri', |
22
|
|
|
'coverArt' => 'array', |
23
|
|
|
'dateCreated' => 'datetime', |
24
|
|
|
'dateModified' => 'datetime', |
25
|
|
|
'datePublished' => 'datetime', |
26
|
|
|
'excerpt' => 'string', |
27
|
|
|
'keywords' => 'array', |
28
|
|
|
'links' => 'array', |
29
|
|
|
'thumbnailURL' => 'uri', |
30
|
|
|
'transparentToolbar' => 'boolean', |
31
|
|
|
'videoURL' => 'uri', |
32
|
|
|
'accessoryText' => 'string', |
33
|
|
|
'isCandidateToBeFeatured' => 'boolean', |
34
|
|
|
'isHidden' => 'boolean', |
35
|
|
|
'isPreview' => 'boolean', |
36
|
|
|
'isSponsored' => 'boolean', |
37
|
|
|
'maturityRating' => 'string' |
38
|
|
|
); |
39
|
|
|
private $allowedParameters = array( |
40
|
|
|
'accessoryText' => 'string', |
41
|
|
|
'isCandidateToBeFeatured' => 'boolean', |
42
|
|
|
'isHidden' => 'boolean', |
43
|
|
|
'isPreview' => 'boolean', |
44
|
|
|
'isSponsored' => 'boolean', |
45
|
|
|
'maturityRating' => 'string', |
46
|
|
|
'targetTerritoryCountryCodes' => 'string', |
47
|
|
|
'isPaid' => 'boolean' |
48
|
|
|
); |
49
|
|
|
private $allowedLocalization = array( |
50
|
|
|
'region' => 'string', |
51
|
|
|
); |
52
|
|
|
|
53
|
|
|
public function __construct($articleId, $status = 'draft') |
54
|
|
|
{ |
55
|
|
|
$this->article_id = $articleId; |
56
|
|
|
$this->status = $status; |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
public function setTitle($title) { |
60
|
|
|
$this->title = $title; |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
public function getTitle() |
64
|
|
|
{ |
65
|
|
|
return $this->title; |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
public function setSubTitle($subTitle) { |
69
|
|
|
$this->sub_title = $subTitle; |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
public function getSubTitle() |
73
|
|
|
{ |
74
|
|
|
return $this->sub_title; |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
public function setLanguage($language) |
78
|
|
|
{ |
79
|
|
|
$this->language = $language; |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
public function getLanguage() |
83
|
|
|
{ |
84
|
|
|
return $this->language; |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
public function setCategory($category) |
88
|
|
|
{ |
89
|
|
|
$this->category = $category; |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
public function getCategory() |
93
|
|
|
{ |
94
|
|
|
return $this->category; |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
public function setTemplate($template) |
98
|
|
|
{ |
99
|
|
|
$this->template = $template; |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
public function getTemplate() |
103
|
|
|
{ |
104
|
|
|
return $this->template; |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
public function setMetaData($metaData) |
108
|
|
|
{ |
109
|
|
|
$metaObj = $this->getMetaData(); |
110
|
|
|
if (is_null($metaObj)) { |
111
|
|
|
$metaObj = new \stdClass(); |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
$errors = array(); |
115
|
|
|
if (is_array($metaData)) { |
116
|
|
|
foreach ($metaData as $key => $value) { |
117
|
|
|
if (isset($this->allowedMetaKeys[$key])) { |
118
|
|
|
$type = gettype($value); |
119
|
|
|
switch ($this->allowedMetaKeys[$key]) { |
120
|
|
|
case 'uri': |
121
|
|
|
if (substr($value, 0, 4) === 'http') { |
122
|
|
|
$metaObj->{$key} = $value; |
123
|
|
|
} else { |
124
|
|
|
$errors[] = array( |
125
|
|
|
'key' => $key, |
126
|
|
|
'message' => 'Not a valid URI' |
127
|
|
|
); |
128
|
|
|
} |
129
|
|
|
break; |
130
|
|
|
|
131
|
|
|
case 'datetime': |
132
|
|
|
if ($value instanceof \DateTime) { |
133
|
|
|
$metaObj->{$key} = $value->format('c'); |
134
|
|
|
} else { |
135
|
|
|
$errors[] = array( |
136
|
|
|
'key' => $key, |
137
|
|
|
'message' => 'Not a valid DateTime object' |
138
|
|
|
); |
139
|
|
|
} |
140
|
|
|
break; |
141
|
|
|
|
142
|
|
|
case 'array': |
143
|
|
|
if (is_array($value)) { |
144
|
|
|
$metaObj->{$key} = $value; |
145
|
|
|
} else { |
146
|
|
|
$errors[] = array( |
147
|
|
|
'key' => $key, |
148
|
|
|
'message' => 'Not a valid Array' |
149
|
|
|
); |
150
|
|
|
} |
151
|
|
|
break; |
152
|
|
|
|
153
|
|
|
default: |
154
|
|
|
if ($type === $this->allowedMetaKeys[$key]) { |
155
|
|
|
$metaObj->{$key} = $value; |
156
|
|
|
} else { |
157
|
|
|
$errors[] = array( |
158
|
|
|
'key' => $key, |
159
|
|
|
'message' => 'Expected ' . $this->allowedMetaKeys[$key] . '; received ' . $type |
160
|
|
|
); |
161
|
|
|
} |
162
|
|
|
break; |
163
|
|
|
} |
164
|
|
|
} else { |
165
|
|
|
$errors[] = array( |
166
|
|
|
'key' => $key, |
167
|
|
|
'message' => 'Not a valid MetaData key' |
168
|
|
|
); |
169
|
|
|
} |
170
|
|
|
} |
171
|
|
|
} |
172
|
|
|
|
173
|
|
|
if (!empty($errors)) { |
174
|
|
|
throw new \ErrorException('Invalid MetaData: ' . print_r($errors, true)); |
|
|
|
|
175
|
|
|
} |
176
|
|
|
|
177
|
|
|
$this->metaData = $metaObj; |
178
|
|
|
} |
179
|
|
|
|
180
|
|
|
public function getMetaData() |
181
|
|
|
{ |
182
|
|
|
return $this->metaData; |
183
|
|
|
} |
184
|
|
|
|
185
|
|
|
public function setParameters($parameters) |
186
|
|
|
{ |
187
|
|
|
$parameterObj = $this->getParameters(); |
188
|
|
|
if (is_null($parameterObj)) { |
189
|
|
|
$parameterObj = new \stdClass(); |
190
|
|
|
} |
191
|
|
|
|
192
|
|
|
$errors = array(); |
193
|
|
|
if (is_array($parameters)) { |
194
|
|
|
foreach ($parameters as $key => $value) { |
195
|
|
|
if (isset($this->allowedParameters[$key])) { |
196
|
|
|
$type = gettype($value); |
197
|
|
|
if ($type === $this->allowedParameters[$key]) { |
198
|
|
|
$parameterObj->{$key} = $value; |
199
|
|
|
} else { |
200
|
|
|
$errors[] = array( |
201
|
|
|
'key' => $key, |
202
|
|
|
'message' => 'Expected ' . $this->allowedParameters[$key] . '; received ' . $type |
203
|
|
|
); |
204
|
|
|
} |
205
|
|
|
} else { |
206
|
|
|
$errors[] = array( |
207
|
|
|
'key' => $key, |
208
|
|
|
'message' => 'Not a valid Parameter' |
209
|
|
|
); |
210
|
|
|
} |
211
|
|
|
} |
212
|
|
|
} |
213
|
|
|
|
214
|
|
|
if (!empty($errors)) { |
215
|
|
|
throw new \ErrorException('Invalid Parameters: ' . print_r($errors, true)); |
|
|
|
|
216
|
|
|
} |
217
|
|
|
|
218
|
|
|
$this->parameters = $parameterObj; |
219
|
|
|
} |
220
|
|
|
|
221
|
|
|
public function getParameters() |
222
|
|
|
{ |
223
|
|
|
return $this->parameters; |
224
|
|
|
} |
225
|
|
|
|
226
|
|
|
public function setLocalization($localization) |
227
|
|
|
{ |
228
|
|
|
$localizationObj = $this->getLocalization(); |
229
|
|
|
if (is_null($localizationObj)) { |
230
|
|
|
$localizationObj = new \stdClass(); |
231
|
|
|
} |
232
|
|
|
|
233
|
|
|
$errors = array(); |
234
|
|
|
if (is_array($localization)) { |
235
|
|
|
foreach ($localization as $key => $value) { |
236
|
|
|
if (isset($this->allowedLocalization[$key])) { |
237
|
|
|
$type = gettype($value); |
238
|
|
|
if ($type === $this->allowedLocalization[$key]) { |
239
|
|
|
$localizationObj->{$key} = $value; |
240
|
|
|
} else { |
241
|
|
|
$errors[] = array( |
242
|
|
|
'key' => $key, |
243
|
|
|
'message' => 'Expected ' . $this->allowedLocalization[$key] . '; received ' . $type |
244
|
|
|
); |
245
|
|
|
} |
246
|
|
|
} else { |
247
|
|
|
$errors[] = array( |
248
|
|
|
'key' => $key, |
249
|
|
|
'message' => 'Not a valid Localization Parameter' |
250
|
|
|
); |
251
|
|
|
} |
252
|
|
|
} |
253
|
|
|
} |
254
|
|
|
|
255
|
|
|
if (!empty($errors)) { |
256
|
|
|
throw new \ErrorException('Invalid Localization Parameters: ' . print_r($errors, true)); |
|
|
|
|
257
|
|
|
} |
258
|
|
|
|
259
|
|
|
$this->localization = $localizationObj; |
260
|
|
|
} |
261
|
|
|
|
262
|
|
|
public function getLocalization() |
263
|
|
|
{ |
264
|
|
|
return $this->localization; |
265
|
|
|
} |
266
|
|
|
|
267
|
|
|
public function getJson() |
268
|
|
|
{ |
269
|
|
|
$article = new \stdClass(); |
270
|
|
|
$article->article_id = $this->article_id; |
271
|
|
|
$article->language = $this->getLanguage(); |
272
|
|
|
$article->status = $this->status; |
273
|
|
|
$article->title = $this->getTitle(); |
274
|
|
|
$article->sub_title = $this->getSubTitle(); |
275
|
|
|
$article->category = $this->getCategory(); |
276
|
|
|
$article->template = $this->getTemplate(); |
277
|
|
|
$article->metadata = $this->getMetaData(); |
278
|
|
|
$article->parameters = $this->getParameters(); |
279
|
|
|
$article->localization = $this->getLocalization(); |
280
|
|
|
$article->components = $this->getComponents(); |
281
|
|
|
|
282
|
|
|
return json_encode($article, JSON_UNESCAPED_UNICODE); |
283
|
|
|
} |
284
|
|
|
|
285
|
|
|
public function getComponents($format = null) |
286
|
|
|
{ |
287
|
|
|
$output = array(); |
288
|
|
|
foreach ($this->components as $component) { |
289
|
|
|
array_push($output, $component->getComponent()); |
290
|
|
|
} |
291
|
|
|
|
292
|
|
|
if ($format === 'json') { |
293
|
|
|
return json_encode($output, JSON_UNESCAPED_UNICODE); |
294
|
|
|
} |
295
|
|
|
|
296
|
|
|
return $output; |
297
|
|
|
} |
298
|
|
|
|
299
|
|
|
/** |
300
|
|
|
* @param AbstractComponent $component |
301
|
|
|
* @throws \ErrorException |
302
|
|
|
* @return void |
303
|
|
|
*/ |
304
|
|
|
public function setComponents($components) |
305
|
|
|
{ |
306
|
|
|
if (is_array($components)) { |
307
|
|
|
foreach ($components as $component) { |
308
|
|
|
if ($component instanceof AbstractComponent && !is_null($component->getRole())) { |
309
|
|
|
$component->updateStyles('root'); |
310
|
|
|
array_push($this->components, $component); |
311
|
|
|
} |
312
|
|
|
} |
313
|
|
|
} else if ($components instanceof AbstractComponent && !is_null($components->getRole())) { |
314
|
|
|
$components->updateStyles('root'); |
315
|
|
|
array_push($this->components, $components); |
316
|
|
|
} |
317
|
|
|
} |
318
|
|
|
} |
319
|
|
|
|