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
|
|
|
|
22
|
|
|
// Dependencies from `charcoal-app` |
23
|
|
|
use \Charcoal\App\Routable\RoutableInterface; |
24
|
|
|
use \Charcoal\App\Routable\RoutableTrait; |
25
|
|
|
|
26
|
|
|
// Intra-module (`charcoal-cms`) dependencies |
27
|
|
|
use \Charcoal\Cms\MetatagInterface; |
28
|
|
|
use \Charcoal\Cms\NewsInterface; |
29
|
|
|
use \Charcoal\Cms\SearchableInterface; |
30
|
|
|
use \Charcoal\Cms\SearchableTrait; |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* News |
34
|
|
|
*/ |
35
|
|
|
abstract class AbstractNews extends Content implements |
36
|
|
|
CategorizableInterface, |
37
|
|
|
MetatagInterface, |
38
|
|
|
NewsInterface, |
39
|
|
|
PublishableInterface, |
40
|
|
|
RoutableInterface, |
41
|
|
|
SearchableInterface |
42
|
|
|
{ |
43
|
|
|
use CategorizableTrait; |
44
|
|
|
use PublishableTrait; |
45
|
|
|
use MetatagTrait; |
46
|
|
|
use RoutableTrait; |
47
|
|
|
use SearchableTrait; |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* @var TranslationString $title |
51
|
|
|
*/ |
52
|
|
|
private $title; |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* @var TranslationString $title |
56
|
|
|
*/ |
57
|
|
|
private $subtitle; |
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* @var TranslationString $content |
61
|
|
|
*/ |
62
|
|
|
private $content; |
63
|
|
|
|
64
|
|
|
/** |
65
|
|
|
* @var TranslationString $image |
66
|
|
|
*/ |
67
|
|
|
private $image; |
68
|
|
|
|
69
|
|
|
/** |
70
|
|
|
* @var DateTime $newsDate |
71
|
|
|
*/ |
72
|
|
|
private $newsDate; |
73
|
|
|
|
74
|
|
|
/** |
75
|
|
|
* @var Collection $documents |
76
|
|
|
*/ |
77
|
|
|
public $documents; |
78
|
|
|
|
79
|
|
|
/** |
80
|
|
|
* @var TranslationString $infoUrl |
81
|
|
|
*/ |
82
|
|
|
private $infoUrl; |
83
|
|
|
|
84
|
|
|
/** |
85
|
|
|
* @param mixed $title The news title (localized). |
86
|
|
|
* @return TranslationString |
87
|
|
|
*/ |
88
|
|
|
public function setTitle($title) |
89
|
|
|
{ |
90
|
|
|
$this->title = new TranslationString($title); |
91
|
|
|
return $this; |
|
|
|
|
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
/** |
95
|
|
|
* @return TranslationString |
96
|
|
|
*/ |
97
|
|
|
public function title() |
98
|
|
|
{ |
99
|
|
|
return $this->title; |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
/** |
103
|
|
|
* @param mixed $subtitle The news subtitle (localized). |
104
|
|
|
* @return Event Chainable |
105
|
|
|
*/ |
106
|
|
|
public function setSubtitle($subtitle) |
107
|
|
|
{ |
108
|
|
|
$this->subtitle = new TranslationString($subtitle); |
109
|
|
|
return $this; |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
/** |
113
|
|
|
* @return TranslationString |
114
|
|
|
*/ |
115
|
|
|
public function subtitle() |
116
|
|
|
{ |
117
|
|
|
return $this->subtitle; |
118
|
|
|
} |
119
|
|
|
|
120
|
|
|
/** |
121
|
|
|
* @param mixed $content The news content (localized). |
122
|
|
|
* @return Event Chainable |
123
|
|
|
*/ |
124
|
|
|
public function setContent($content) |
125
|
|
|
{ |
126
|
|
|
$this->content = new TranslationString($content); |
127
|
|
|
return $this; |
128
|
|
|
} |
129
|
|
|
|
130
|
|
|
/** |
131
|
|
|
* @return TranslationString |
132
|
|
|
*/ |
133
|
|
|
public function content() |
134
|
|
|
{ |
135
|
|
|
return $this->content; |
136
|
|
|
} |
137
|
|
|
|
138
|
|
|
/** |
139
|
|
|
* @param mixed $image The section main image (localized). |
140
|
|
|
* @return Section Chainable |
141
|
|
|
*/ |
142
|
|
|
public function setImage($image) |
143
|
|
|
{ |
144
|
|
|
$this->image = new TranslationString($image); |
145
|
|
|
return $this; |
146
|
|
|
} |
147
|
|
|
|
148
|
|
|
/** |
149
|
|
|
* @return TranslationString |
150
|
|
|
*/ |
151
|
|
|
public function image() |
152
|
|
|
{ |
153
|
|
|
return $this->image; |
154
|
|
|
} |
155
|
|
|
|
156
|
|
|
/** |
157
|
|
|
* @param mixed $newsDate The news date. |
158
|
|
|
* @throws InvalidArgumentException If the timestamp is invalid. |
159
|
|
|
* @return ObjectRevision Chainable |
160
|
|
|
*/ |
161
|
|
View Code Duplication |
public function setNewsDate($newsDate) |
|
|
|
|
162
|
|
|
{ |
163
|
|
|
if ($newsDate === null) { |
164
|
|
|
$this->newsDate = null; |
165
|
|
|
return $this; |
|
|
|
|
166
|
|
|
} |
167
|
|
|
if (is_string($newsDate)) { |
168
|
|
|
$newsDate = new DateTime($newsDate); |
169
|
|
|
} |
170
|
|
|
if (!($newsDate instanceof DateTimeInterface)) { |
171
|
|
|
throw new InvalidArgumentException( |
172
|
|
|
'Invalid "Revision Date" value. Must be a date/time string or a DateTimeInterface object.' |
173
|
|
|
); |
174
|
|
|
} |
175
|
|
|
$this->newsDate = $newsDate; |
176
|
|
|
return $this; |
|
|
|
|
177
|
|
|
} |
178
|
|
|
|
179
|
|
|
/** |
180
|
|
|
* @return DateTime|null |
181
|
|
|
*/ |
182
|
|
|
public function newsDate() |
183
|
|
|
{ |
184
|
|
|
return $this->newsDate; |
185
|
|
|
} |
186
|
|
|
|
187
|
|
|
/** |
188
|
|
|
* @param mixed $url The info URL (news source or where to find more information; localized). |
189
|
|
|
* @return NewsInterface Chainable |
190
|
|
|
*/ |
191
|
|
|
public function setInfoUrl($url) |
192
|
|
|
{ |
193
|
|
|
$this->infoUrl = new TranslationString($url); |
194
|
|
|
return $this; |
195
|
|
|
} |
196
|
|
|
|
197
|
|
|
/** |
198
|
|
|
* @return TranslationString |
199
|
|
|
*/ |
200
|
|
|
public function infoUrl() |
201
|
|
|
{ |
202
|
|
|
return $this->infoUrl; |
203
|
|
|
} |
204
|
|
|
|
205
|
|
|
/** |
206
|
|
|
* MetatagTrait > canonical_url |
207
|
|
|
* |
208
|
|
|
* @return string |
209
|
|
|
* @todo |
210
|
|
|
*/ |
211
|
|
|
public function canonicalUrl() |
212
|
|
|
{ |
213
|
|
|
return ''; |
214
|
|
|
} |
215
|
|
|
|
216
|
|
|
/** |
217
|
|
|
* RoutableInterface > handle_route() |
218
|
|
|
* |
219
|
|
|
* @param string $path The request path. |
220
|
|
|
* @param RequestInterface $request PSR-7 (http) request. |
221
|
|
|
* @param ResponseInterface $response PSR-7 (http) response. |
222
|
|
|
* @throws InvalidArgumentException If the path is not a string. |
223
|
|
|
* @return callable|null Route dispatcher |
224
|
|
|
*/ |
225
|
|
View Code Duplication |
public function routeHandler($path, RequestInterface $request, ResponseInterface $response) |
|
|
|
|
226
|
|
|
{ |
227
|
|
|
if (!is_string($path)) { |
228
|
|
|
throw new InvalidArgumentException( |
229
|
|
|
'Route path must be a string' |
230
|
|
|
); |
231
|
|
|
} |
232
|
|
|
$match_path = $path == 'xxx'; |
233
|
|
|
|
234
|
|
|
// Insert logic here... |
235
|
|
|
if ($match_path) { |
236
|
|
|
return function(RequestInterface $request, ResponseInterface $response) use ($path) { |
237
|
|
|
$response->write($path); |
238
|
|
|
}; |
239
|
|
|
} else { |
240
|
|
|
return null; |
241
|
|
|
} |
242
|
|
|
} |
243
|
|
|
|
244
|
|
|
/** |
245
|
|
|
* @return TranslationString |
246
|
|
|
*/ |
247
|
|
|
public function defaultMetaTitle() |
248
|
|
|
{ |
249
|
|
|
return $this->title(); |
|
|
|
|
250
|
|
|
} |
251
|
|
|
|
252
|
|
|
/** |
253
|
|
|
* @return TranslationString |
254
|
|
|
*/ |
255
|
|
|
public function defaultMetaDescription() |
256
|
|
|
{ |
257
|
|
|
return $this->content(); |
|
|
|
|
258
|
|
|
} |
259
|
|
|
|
260
|
|
|
/** |
261
|
|
|
* @return TranslationString |
262
|
|
|
*/ |
263
|
|
|
public function defaultMetaImage() |
264
|
|
|
{ |
265
|
|
|
return $this->image(); |
|
|
|
|
266
|
|
|
} |
267
|
|
|
} |
268
|
|
|
|
If you return a value from a function or method, it should be a sub-type of the type that is given by the parent type f.e. an interface, or abstract method. This is more formally defined by the Lizkov substitution principle, and guarantees that classes that depend on the parent type can use any instance of a child type interchangably. This principle also belongs to the SOLID principles for object oriented design.
Let’s take a look at an example:
Our function
my_function
expects aPost
object, and outputs the author of the post. The base classPost
returns a simple string and outputting a simple string will work just fine. However, the child classBlogPost
which is a sub-type ofPost
instead decided to return anobject
, and is therefore violating the SOLID principles. If aBlogPost
were passed tomy_function
, PHP would not complain, but ultimately fail when executing thestrtoupper
call in its body.