1
|
|
|
<?php namespace FlatPlan; |
2
|
|
|
|
3
|
|
|
use FlatPlan\Components\AbstractComponent; |
4
|
|
|
|
5
|
|
|
class Article { |
6
|
|
|
|
7
|
|
|
private $components = array(); |
8
|
|
|
private $status; |
9
|
|
|
private $articleId; |
10
|
|
|
private $title; |
11
|
|
|
private $subTitle; |
12
|
|
|
private $language = 'en'; |
13
|
|
|
private $category; |
14
|
|
|
private $template; |
15
|
|
|
private $metaData; |
16
|
|
|
private $allowedMetaKeys = array( |
17
|
|
|
'authors' => 'string', |
18
|
|
|
'canonicalURL' => 'uri', |
19
|
|
|
'coverArt' => 'array', |
20
|
|
|
'dateCreated' => 'datetime', |
21
|
|
|
'dateModified' => 'datetime', |
22
|
|
|
'datePublished' => 'datetime', |
23
|
|
|
'excerpt' => 'string', |
24
|
|
|
'keywords' => 'string', |
25
|
|
|
'links' => 'array', |
26
|
|
|
'thumbnailURL' => 'uri', |
27
|
|
|
'transparentToolbar' => 'boolean', |
28
|
|
|
'videoURL' => 'uri', |
29
|
|
|
'accessoryText' => 'string', |
30
|
|
|
'isCandidateToBeFeatured' => 'boolean', |
31
|
|
|
'isHidden' => 'boolean', |
32
|
|
|
'isPreview' => 'boolean', |
33
|
|
|
'isSponsored' => 'boolean', |
34
|
|
|
'maturityRating' => 'string' |
35
|
|
|
); |
36
|
|
|
|
37
|
|
|
public function __construct($articleId, $status = 'draft') |
38
|
|
|
{ |
39
|
|
|
$this->articleId = $articleId; |
40
|
|
|
$this->status = $status; |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
public function setTitle($title) { |
44
|
|
|
$this->title = $title; |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
public function getTitle() |
48
|
|
|
{ |
49
|
|
|
return $this->title; |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
public function setSubTitle($subTitle) { |
53
|
|
|
$this->subTitle = $subTitle; |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
public function getSubTitle() |
57
|
|
|
{ |
58
|
|
|
return $this->subTitle; |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
public function setLanguage($language) |
62
|
|
|
{ |
63
|
|
|
$this->language = $language; |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
public function getLanguage() |
67
|
|
|
{ |
68
|
|
|
return $this->language; |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
public function setCategory($category) |
72
|
|
|
{ |
73
|
|
|
$this->category = $category; |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
public function getCategory() |
77
|
|
|
{ |
78
|
|
|
return $this->category; |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
public function setTemplate($template) |
82
|
|
|
{ |
83
|
|
|
$this->template = $template; |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
public function getTemplate() |
87
|
|
|
{ |
88
|
|
|
return $this->template; |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
public function setMetaData($metaData) |
92
|
|
|
{ |
93
|
|
|
$metaObj = new \stdClass(); |
94
|
|
|
$errors = array(); |
95
|
|
|
if (is_array($metaData)) { |
96
|
|
|
foreach ($metaData as $key => $value) { |
97
|
|
|
if (isset($this->allowedMetaKeys[$key])) { |
98
|
|
|
$type = gettype($value); |
99
|
|
|
switch ($this->allowedMetaKeys[$key]) { |
100
|
|
|
case 'uri': |
101
|
|
|
if (filter_var($value, FILTER_VALIDATE_URL)) { |
102
|
|
|
$metaObj->{$key} = $value; |
103
|
|
|
} else { |
104
|
|
|
$errors[] = array( |
105
|
|
|
'key' => $key, |
106
|
|
|
'message' => 'Not a valid URI' |
107
|
|
|
); |
108
|
|
|
} |
109
|
|
|
break; |
110
|
|
|
|
111
|
|
|
case 'datetime': |
112
|
|
|
if ($value instanceof \DateTime) { |
113
|
|
|
$metaObj->{$key} = $value; |
114
|
|
|
} else { |
115
|
|
|
$errors[] = array( |
116
|
|
|
'key' => $key, |
117
|
|
|
'message' => 'Not a valid DateTime object' |
118
|
|
|
); |
119
|
|
|
} |
120
|
|
|
break; |
121
|
|
|
|
122
|
|
|
default: |
123
|
|
|
if ($type === $this->allowedMetaKeys[$key]) { |
124
|
|
|
$metaObj->{$key} = $value; |
125
|
|
|
} else { |
126
|
|
|
$errors[] = array( |
127
|
|
|
'key' => $key, |
128
|
|
|
'message' => 'Expected ' . $this->allowedMetaKeys[$key] . '; received ' . $type |
129
|
|
|
); |
130
|
|
|
} |
131
|
|
|
break; |
132
|
|
|
} |
133
|
|
|
} else { |
134
|
|
|
$errors[] = array( |
135
|
|
|
'key' => $key, |
136
|
|
|
'message' => 'Not a valid MetaData key' |
137
|
|
|
); |
138
|
|
|
} |
139
|
|
|
} |
140
|
|
|
} |
141
|
|
|
|
142
|
|
|
if (!empty($errors)) { |
143
|
|
|
throw new \ErrorException('Invalid MetaData: ' . print_r($errors, true)); |
144
|
|
|
} |
145
|
|
|
|
146
|
|
|
$this->metaData = $metaObj; |
147
|
|
|
} |
148
|
|
|
|
149
|
|
|
public function getMetaData() |
150
|
|
|
{ |
151
|
|
|
return $this->metaData; |
152
|
|
|
} |
153
|
|
|
|
154
|
|
|
public function getJson() |
155
|
|
|
{ |
156
|
|
|
$article = new \stdClass(); |
157
|
|
|
$article->articleId = $this->articleId; |
158
|
|
|
$article->language = $this->getLanguage(); |
159
|
|
|
$article->status = $this->status; |
160
|
|
|
$article->title = $this->getTitle(); |
161
|
|
|
$article->subTitle = $this->getSubTitle(); |
162
|
|
|
$article->category = $this->getCategory(); |
163
|
|
|
$article->template = $this->getTemplate(); |
164
|
|
|
$article->metaData = $this->getMetaData(); |
165
|
|
|
$article->components = $this->getComponents(); |
166
|
|
|
|
167
|
|
|
return json_encode($article); |
168
|
|
|
} |
169
|
|
|
|
170
|
|
|
public function getComponents($format = null) |
171
|
|
|
{ |
172
|
|
|
$output = array(); |
173
|
|
|
foreach ($this->components as $component) { |
174
|
|
|
array_push($output, $component->getComponent()); |
175
|
|
|
} |
176
|
|
|
|
177
|
|
|
if ($format === 'json') { |
178
|
|
|
return json_encode($output); |
179
|
|
|
} |
180
|
|
|
|
181
|
|
|
return $output; |
182
|
|
|
} |
183
|
|
|
|
184
|
|
|
/** |
185
|
|
|
* @param AbstractComponent $component |
186
|
|
|
* @throws \ErrorException |
187
|
|
|
* @return void |
188
|
|
|
*/ |
189
|
|
|
public function setComponents($components) |
190
|
|
|
{ |
191
|
|
|
if (is_array($components)) { |
192
|
|
|
foreach ($components as $component) { |
193
|
|
|
if ($component instanceof AbstractComponent) { |
194
|
|
|
$component->updateStyles('root'); |
195
|
|
|
array_push($this->components, $component); |
196
|
|
|
} |
197
|
|
|
} |
198
|
|
|
} else if ($components instanceof AbstractComponent) { |
199
|
|
|
$components->updateStyles('root'); |
200
|
|
|
array_push($this->components, $components); |
201
|
|
|
} else { |
202
|
|
|
throw new \ErrorException('Invalid component supplied.'); |
203
|
|
|
} |
204
|
|
|
} |
205
|
|
|
} |
206
|
|
|
|