1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Charcoal\Cms; |
4
|
|
|
|
5
|
|
|
use \DateTime; |
6
|
|
|
use \DateTimeInterface; |
7
|
|
|
use \InvalidArgumentException; |
8
|
|
|
|
9
|
|
|
use \Psr\Http\Message\RequestInterface; |
10
|
|
|
use \Psr\Http\Message\ResponseInterface; |
11
|
|
|
|
12
|
|
|
// Dependencies from `charcoal-translation` |
13
|
|
|
use \Charcoal\Translation\TranslationString; |
14
|
|
|
|
15
|
|
|
// Dependencies from `charcoal-base` |
16
|
|
|
use \Charcoal\Object\Content; |
17
|
|
|
use \Charcoal\Object\CategorizableInterface; |
18
|
|
|
use \Charcoal\Object\CategorizableTrait; |
19
|
|
|
use \Charcoal\Object\PublishableInterface; |
20
|
|
|
use \Charcoal\Object\PublishableTrait; |
21
|
|
|
use \Charcoal\Object\RoutableInterface; |
22
|
|
|
use \Charcoal\Object\RoutableTrait; |
23
|
|
|
|
24
|
|
|
// Intra-module (`charcoal-cms`) dependencies |
25
|
|
|
use \Charcoal\Cms\MetatagInterface; |
26
|
|
|
use \Charcoal\Cms\NewsInterface; |
27
|
|
|
use \Charcoal\Cms\SearchableInterface; |
28
|
|
|
use \Charcoal\Cms\SearchableTrait; |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* News |
32
|
|
|
*/ |
33
|
|
|
abstract class AbstractNews extends Content implements |
34
|
|
|
CategorizableInterface, |
35
|
|
|
MetatagInterface, |
36
|
|
|
NewsInterface, |
37
|
|
|
PublishableInterface, |
38
|
|
|
RoutableInterface, |
39
|
|
|
SearchableInterface |
40
|
|
|
{ |
41
|
|
|
use CategorizableTrait; |
42
|
|
|
use PublishableTrait; |
43
|
|
|
use MetatagTrait; |
44
|
|
|
use RoutableTrait; |
45
|
|
|
use SearchableTrait; |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* @var TranslationString $title |
49
|
|
|
*/ |
50
|
|
|
private $title; |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* @var TranslationString $title |
54
|
|
|
*/ |
55
|
|
|
private $subtitle; |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* @var TranslationString $content |
59
|
|
|
*/ |
60
|
|
|
private $content; |
61
|
|
|
|
62
|
|
|
/** |
63
|
|
|
* @var TranslationString $image |
64
|
|
|
*/ |
65
|
|
|
private $image; |
66
|
|
|
|
67
|
|
|
/** |
68
|
|
|
* @var DateTime $newsDate |
69
|
|
|
*/ |
70
|
|
|
private $newsDate; |
71
|
|
|
|
72
|
|
|
/** |
73
|
|
|
* @var TranslationString $infoUrl |
74
|
|
|
*/ |
75
|
|
|
private $infoUrl; |
76
|
|
|
|
77
|
|
|
/** |
78
|
|
|
* Generate a slug on preSave |
79
|
|
|
* @see RoutableTrait |
80
|
|
|
* @return parent::preSave |
|
|
|
|
81
|
|
|
*/ |
82
|
|
|
public function preSave() |
83
|
|
|
{ |
84
|
|
|
$this->setSlug($this->generateSlug()); |
85
|
|
|
|
86
|
|
|
return parent::preSave(); |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
/** |
90
|
|
|
* Generate a slug on preUpdate |
91
|
|
|
* @see RoutableTrait |
92
|
|
|
* @param array $properties Properties to update. |
93
|
|
|
* @return parent::preUpdate |
|
|
|
|
94
|
|
|
*/ |
95
|
|
|
public function preUpdate(array $properties = null) |
96
|
|
|
{ |
97
|
|
|
$this->setSlug($this->generateSlug()); |
98
|
|
|
|
99
|
|
|
return parent::preUpdate($properties); |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
|
103
|
|
|
/** |
104
|
|
|
* @param mixed $title The news title (localized). |
105
|
|
|
* @return TranslationString |
106
|
|
|
*/ |
107
|
|
|
public function setTitle($title) |
108
|
|
|
{ |
109
|
|
|
$this->title = new TranslationString($title); |
110
|
|
|
return $this; |
|
|
|
|
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
/** |
114
|
|
|
* @return TranslationString |
115
|
|
|
*/ |
116
|
|
|
public function title() |
117
|
|
|
{ |
118
|
|
|
return $this->title; |
119
|
|
|
} |
120
|
|
|
|
121
|
|
|
/** |
122
|
|
|
* @param mixed $subtitle The news subtitle (localized). |
123
|
|
|
* @return Event Chainable |
124
|
|
|
*/ |
125
|
|
|
public function setSubtitle($subtitle) |
126
|
|
|
{ |
127
|
|
|
$this->subtitle = new TranslationString($subtitle); |
128
|
|
|
return $this; |
129
|
|
|
} |
130
|
|
|
|
131
|
|
|
/** |
132
|
|
|
* @return TranslationString |
133
|
|
|
*/ |
134
|
|
|
public function subtitle() |
135
|
|
|
{ |
136
|
|
|
return $this->subtitle; |
137
|
|
|
} |
138
|
|
|
|
139
|
|
|
/** |
140
|
|
|
* @param mixed $content The news content (localized). |
141
|
|
|
* @return Event Chainable |
142
|
|
|
*/ |
143
|
|
|
public function setContent($content) |
144
|
|
|
{ |
145
|
|
|
$this->content = new TranslationString($content); |
146
|
|
|
return $this; |
147
|
|
|
} |
148
|
|
|
|
149
|
|
|
/** |
150
|
|
|
* @return TranslationString |
151
|
|
|
*/ |
152
|
|
|
public function content() |
153
|
|
|
{ |
154
|
|
|
return $this->content; |
155
|
|
|
} |
156
|
|
|
|
157
|
|
|
/** |
158
|
|
|
* @param mixed $image The section main image (localized). |
159
|
|
|
* @return Section Chainable |
160
|
|
|
*/ |
161
|
|
|
public function setImage($image) |
162
|
|
|
{ |
163
|
|
|
$this->image = new TranslationString($image); |
164
|
|
|
return $this; |
165
|
|
|
} |
166
|
|
|
|
167
|
|
|
/** |
168
|
|
|
* @return TranslationString |
169
|
|
|
*/ |
170
|
|
|
public function image() |
171
|
|
|
{ |
172
|
|
|
return $this->image; |
173
|
|
|
} |
174
|
|
|
|
175
|
|
|
/** |
176
|
|
|
* @param mixed $template The section template (ident). |
177
|
|
|
* @return SectionInterface Chainable |
178
|
|
|
*/ |
179
|
|
|
public function setTemplateIdent($template) |
180
|
|
|
{ |
181
|
|
|
$this->templateIdent = $template; |
|
|
|
|
182
|
|
|
return $this; |
|
|
|
|
183
|
|
|
} |
184
|
|
|
|
185
|
|
|
/** |
186
|
|
|
* @return mixed |
187
|
|
|
*/ |
188
|
|
|
public function templateIdent() |
189
|
|
|
{ |
190
|
|
|
if (!$this->templateIdent) { |
|
|
|
|
191
|
|
|
$metadata = $this->metadata(); |
192
|
|
|
return $metadata['template_ident']; |
193
|
|
|
} |
194
|
|
|
return $this->templateIdent; |
|
|
|
|
195
|
|
|
} |
196
|
|
|
|
197
|
|
|
/** |
198
|
|
|
* @param array|string $templateOptions Extra template options, if any. |
199
|
|
|
* @return SectionInterface Chainable |
200
|
|
|
*/ |
201
|
|
|
public function setTemplateOptions($templateOptions) |
202
|
|
|
{ |
203
|
|
|
$this->templateOptions = $templateOptions; |
|
|
|
|
204
|
|
|
return $this; |
205
|
|
|
} |
206
|
|
|
|
207
|
|
|
/** |
208
|
|
|
* @return array |
209
|
|
|
*/ |
210
|
|
|
public function templateOptions() |
211
|
|
|
{ |
212
|
|
|
if (!$this->templateOptions) { |
213
|
|
|
$metadata = $this->metadata(); |
214
|
|
|
return $metadata['template_options']; |
215
|
|
|
} |
216
|
|
|
return $this->templateOptions; |
217
|
|
|
} |
218
|
|
|
|
219
|
|
|
/** |
220
|
|
|
* @param mixed $newsDate The news date. |
221
|
|
|
* @throws InvalidArgumentException If the timestamp is invalid. |
222
|
|
|
* @return ObjectRevision Chainable |
223
|
|
|
*/ |
224
|
|
|
public function setNewsDate($newsDate) |
225
|
|
|
{ |
226
|
|
|
if ($newsDate === null) { |
227
|
|
|
$this->newsDate = null; |
228
|
|
|
return $this; |
|
|
|
|
229
|
|
|
} |
230
|
|
|
if (is_string($newsDate)) { |
231
|
|
|
$newsDate = new DateTime($newsDate); |
232
|
|
|
} |
233
|
|
|
if (!($newsDate instanceof DateTimeInterface)) { |
234
|
|
|
throw new InvalidArgumentException( |
235
|
|
|
'Invalid "Revision Date" value. Must be a date/time string or a DateTimeInterface object.' |
236
|
|
|
); |
237
|
|
|
} |
238
|
|
|
$this->newsDate = $newsDate; |
239
|
|
|
return $this; |
|
|
|
|
240
|
|
|
} |
241
|
|
|
|
242
|
|
|
/** |
243
|
|
|
* @return DateTime|null |
244
|
|
|
*/ |
245
|
|
|
public function newsDate() |
246
|
|
|
{ |
247
|
|
|
return $this->newsDate; |
248
|
|
|
} |
249
|
|
|
|
250
|
|
|
/** |
251
|
|
|
* @param mixed $url The info URL (news source or where to find more information; localized). |
252
|
|
|
* @return NewsInterface Chainable |
253
|
|
|
*/ |
254
|
|
|
public function setInfoUrl($url) |
255
|
|
|
{ |
256
|
|
|
$this->infoUrl = new TranslationString($url); |
257
|
|
|
return $this; |
258
|
|
|
} |
259
|
|
|
|
260
|
|
|
/** |
261
|
|
|
* @return TranslationString |
262
|
|
|
*/ |
263
|
|
|
public function infoUrl() |
264
|
|
|
{ |
265
|
|
|
return $this->infoUrl; |
266
|
|
|
} |
267
|
|
|
|
268
|
|
|
/** |
269
|
|
|
* MetatagTrait > canonical_url |
270
|
|
|
* |
271
|
|
|
* @return string |
272
|
|
|
* @todo |
273
|
|
|
*/ |
274
|
|
|
public function canonicalUrl() |
275
|
|
|
{ |
276
|
|
|
return ''; |
277
|
|
|
} |
278
|
|
|
|
279
|
|
|
/** |
280
|
|
|
* RoutableInterface > handle_route() |
281
|
|
|
* |
282
|
|
|
* @param string $path The request path. |
283
|
|
|
* @param RequestInterface $request PSR-7 (http) request. |
284
|
|
|
* @param ResponseInterface $response PSR-7 (http) response. |
285
|
|
|
* @throws InvalidArgumentException If the path is not a string. |
286
|
|
|
* @return callable|null Route dispatcher |
287
|
|
|
*/ |
288
|
|
|
public function routeHandler($path, RequestInterface $request, ResponseInterface $response) |
|
|
|
|
289
|
|
|
{ |
290
|
|
|
if (!is_string($path)) { |
291
|
|
|
throw new InvalidArgumentException( |
292
|
|
|
'Route path must be a string' |
293
|
|
|
); |
294
|
|
|
} |
295
|
|
|
$match_path = $path == 'xxx'; |
296
|
|
|
|
297
|
|
|
// Insert logic here... |
298
|
|
|
if ($match_path) { |
299
|
|
|
return function(RequestInterface $request, ResponseInterface $response) use ($path) { |
300
|
|
|
$response->write($path); |
301
|
|
|
}; |
302
|
|
|
} else { |
303
|
|
|
return null; |
304
|
|
|
} |
305
|
|
|
} |
306
|
|
|
|
307
|
|
|
/** |
308
|
|
|
* @return TranslationString |
309
|
|
|
*/ |
310
|
|
|
public function defaultMetaTitle() |
311
|
|
|
{ |
312
|
|
|
return $this->title(); |
|
|
|
|
313
|
|
|
} |
314
|
|
|
|
315
|
|
|
/** |
316
|
|
|
* @return TranslationString |
317
|
|
|
*/ |
318
|
|
|
public function defaultMetaDescription() |
319
|
|
|
{ |
320
|
|
|
return $this->content(); |
|
|
|
|
321
|
|
|
} |
322
|
|
|
|
323
|
|
|
/** |
324
|
|
|
* @return TranslationString |
325
|
|
|
*/ |
326
|
|
|
public function defaultMetaImage() |
327
|
|
|
{ |
328
|
|
|
return $this->image(); |
|
|
|
|
329
|
|
|
} |
330
|
|
|
} |
331
|
|
|
|
This check marks PHPDoc comments that could not be parsed by our parser. To see which comment annotations we can parse, please refer to our documentation on supported doc-types.