1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* This file is part of the Superdesk Web Publisher Template Engine Bundle. |
5
|
|
|
* |
6
|
|
|
* Copyright 2015 Sourcefabric z.u. and contributors. |
7
|
|
|
* |
8
|
|
|
* For the full copyright and license information, please see the |
9
|
|
|
* AUTHORS and LICENSE files distributed with this source code. |
10
|
|
|
* |
11
|
|
|
* @copyright 2015 Sourcefabric z.ú. |
12
|
|
|
* @license http://www.superdesk.org/license |
13
|
|
|
*/ |
14
|
|
|
namespace SWP\Bundle\TemplateEngineBundle\Model; |
15
|
|
|
|
16
|
|
|
use Doctrine\Common\Collections\ArrayCollection; |
17
|
|
|
use SWP\Component\MultiTenancy\Model\TenantAwareInterface; |
18
|
|
|
use SWP\Component\MultiTenancy\Model\TenantInterface; |
19
|
|
|
use SWP\TemplatesSystem\Gimme\Model\ContainerInterface; |
20
|
|
|
use SWP\Component\Common\Model\TimestampableInterface; |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* Container. |
24
|
|
|
*/ |
25
|
|
|
class Container implements ContainerInterface, TenantAwareInterface, TimestampableInterface |
26
|
|
|
{ |
27
|
|
|
const TYPE_SIMPLE = 1; |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* @var int |
31
|
|
|
*/ |
32
|
|
|
protected $id; |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* @var int |
36
|
|
|
*/ |
37
|
|
|
protected $type; |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* @var string |
41
|
|
|
*/ |
42
|
|
|
protected $name; |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* @var int |
46
|
|
|
*/ |
47
|
|
|
protected $width; |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* @var int |
51
|
|
|
*/ |
52
|
|
|
protected $height; |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* @var string |
56
|
|
|
*/ |
57
|
|
|
protected $styles; |
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* @var string |
61
|
|
|
*/ |
62
|
|
|
protected $cssClass; |
63
|
|
|
|
64
|
|
|
/** |
65
|
|
|
* @var bool |
66
|
|
|
*/ |
67
|
|
|
protected $visible; |
68
|
|
|
|
69
|
|
|
/** |
70
|
|
|
* @var ArrayCollection |
71
|
|
|
*/ |
72
|
|
|
protected $data; |
73
|
|
|
|
74
|
|
|
/** |
75
|
|
|
* @var ArrayCollection |
76
|
|
|
*/ |
77
|
|
|
protected $widgets; |
78
|
|
|
|
79
|
|
|
/** |
80
|
|
|
* @var TenantInterface |
81
|
|
|
*/ |
82
|
|
|
protected $tenant; |
83
|
|
|
|
84
|
|
|
/** |
85
|
|
|
* @var \DateTime |
86
|
|
|
*/ |
87
|
|
|
protected $createdAt; |
88
|
|
|
|
89
|
|
|
/** |
90
|
|
|
* @var \DateTime |
91
|
|
|
*/ |
92
|
|
|
protected $updatedAt = null; |
93
|
|
|
|
94
|
|
|
public function __construct() |
95
|
|
|
{ |
96
|
|
|
$this->createdAt = new \DateTime(); |
97
|
|
|
$this->data = new ArrayCollection(); |
98
|
|
|
$this->widgets = new ArrayCollection(); |
99
|
|
|
$this->setType(self::TYPE_SIMPLE); |
100
|
|
|
$this->setVisible(true); |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
/** |
104
|
|
|
* Get id. |
105
|
|
|
* |
106
|
|
|
* @return int |
107
|
|
|
*/ |
108
|
|
|
public function getId() |
109
|
|
|
{ |
110
|
|
|
return $this->id; |
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
/** |
114
|
|
|
* Set id. |
115
|
|
|
* |
116
|
|
|
* @param int $id |
117
|
|
|
* |
118
|
|
|
* @return self |
119
|
|
|
*/ |
120
|
|
|
public function setId($id) |
121
|
|
|
{ |
122
|
|
|
$this->id = $id; |
123
|
|
|
|
124
|
|
|
return $this; |
125
|
|
|
} |
126
|
|
|
|
127
|
|
|
/** |
128
|
|
|
* Set name. |
129
|
|
|
* |
130
|
|
|
* @param string $name |
131
|
|
|
* |
132
|
|
|
* @return self |
133
|
|
|
*/ |
134
|
|
|
public function setName($name) |
135
|
|
|
{ |
136
|
|
|
$this->name = $name; |
137
|
|
|
|
138
|
|
|
return $this; |
139
|
|
|
} |
140
|
|
|
|
141
|
|
|
/** |
142
|
|
|
* Get name. |
143
|
|
|
* |
144
|
|
|
* @return string |
145
|
|
|
*/ |
146
|
|
|
public function getName() |
147
|
|
|
{ |
148
|
|
|
return $this->name; |
149
|
|
|
} |
150
|
|
|
|
151
|
|
|
/** |
152
|
|
|
* Gets the value of width. |
153
|
|
|
* |
154
|
|
|
* @return int |
155
|
|
|
*/ |
156
|
|
|
public function getWidth() |
157
|
|
|
{ |
158
|
|
|
return $this->width; |
159
|
|
|
} |
160
|
|
|
|
161
|
|
|
/** |
162
|
|
|
* Sets the value of width. |
163
|
|
|
* |
164
|
|
|
* @param int $width the width |
165
|
|
|
* |
166
|
|
|
* @return self |
167
|
|
|
*/ |
168
|
|
|
public function setWidth($width) |
169
|
|
|
{ |
170
|
|
|
$this->width = $width; |
171
|
|
|
|
172
|
|
|
return $this; |
173
|
|
|
} |
174
|
|
|
|
175
|
|
|
/** |
176
|
|
|
* Gets the value of height. |
177
|
|
|
* |
178
|
|
|
* @return int |
179
|
|
|
*/ |
180
|
|
|
public function getHeight() |
181
|
|
|
{ |
182
|
|
|
return $this->height; |
183
|
|
|
} |
184
|
|
|
|
185
|
|
|
/** |
186
|
|
|
* Sets the value of height. |
187
|
|
|
* |
188
|
|
|
* @param int $height the height |
189
|
|
|
* |
190
|
|
|
* @return self |
191
|
|
|
*/ |
192
|
|
|
public function setHeight($height) |
193
|
|
|
{ |
194
|
|
|
$this->height = $height; |
195
|
|
|
|
196
|
|
|
return $this; |
197
|
|
|
} |
198
|
|
|
|
199
|
|
|
/** |
200
|
|
|
* Gets the value of styles. |
201
|
|
|
* |
202
|
|
|
* @return string |
203
|
|
|
*/ |
204
|
|
|
public function getStyles() |
205
|
|
|
{ |
206
|
|
|
return $this->styles; |
207
|
|
|
} |
208
|
|
|
|
209
|
|
|
/** |
210
|
|
|
* Sets the value of styles. |
211
|
|
|
* |
212
|
|
|
* @param string $styles the styles |
213
|
|
|
* |
214
|
|
|
* @return self |
215
|
|
|
*/ |
216
|
|
|
public function setStyles($styles) |
217
|
|
|
{ |
218
|
|
|
$this->styles = $styles; |
219
|
|
|
|
220
|
|
|
return $this; |
221
|
|
|
} |
222
|
|
|
|
223
|
|
|
/** |
224
|
|
|
* Gets the value of cssClass. |
225
|
|
|
* |
226
|
|
|
* @return string |
227
|
|
|
*/ |
228
|
|
|
public function getCssClass() |
229
|
|
|
{ |
230
|
|
|
return $this->cssClass; |
231
|
|
|
} |
232
|
|
|
|
233
|
|
|
/** |
234
|
|
|
* Sets the value of cssClass. |
235
|
|
|
* |
236
|
|
|
* @param string $cssClass the css class |
237
|
|
|
* |
238
|
|
|
* @return self |
239
|
|
|
*/ |
240
|
|
|
public function setCssClass($cssClass) |
241
|
|
|
{ |
242
|
|
|
$this->cssClass = $cssClass; |
243
|
|
|
|
244
|
|
|
return $this; |
245
|
|
|
} |
246
|
|
|
|
247
|
|
|
/** |
248
|
|
|
* Gets the value of visible. |
249
|
|
|
* |
250
|
|
|
* @return bool |
251
|
|
|
*/ |
252
|
|
|
public function getVisible() |
253
|
|
|
{ |
254
|
|
|
return $this->visible; |
255
|
|
|
} |
256
|
|
|
|
257
|
|
|
/** |
258
|
|
|
* Sets the value of visible. |
259
|
|
|
* |
260
|
|
|
* @param bool $visible the visible |
261
|
|
|
* |
262
|
|
|
* @return self |
263
|
|
|
*/ |
264
|
|
|
public function setVisible($visible) |
265
|
|
|
{ |
266
|
|
|
$this->visible = $visible; |
267
|
|
|
|
268
|
|
|
return $this; |
269
|
|
|
} |
270
|
|
|
|
271
|
|
|
/** |
272
|
|
|
* Gets the value of data. |
273
|
|
|
* |
274
|
|
|
* @return ArrayCollection |
275
|
|
|
*/ |
276
|
|
|
public function getData() |
277
|
|
|
{ |
278
|
|
|
return $this->data; |
|
|
|
|
279
|
|
|
} |
280
|
|
|
|
281
|
|
|
/** |
282
|
|
|
* Sets the value of data. |
283
|
|
|
* |
284
|
|
|
* @param ArrayCollection $data the data |
285
|
|
|
* |
286
|
|
|
* @return self |
287
|
|
|
*/ |
288
|
|
|
public function setData(ArrayCollection $data) |
289
|
|
|
{ |
290
|
|
|
$this->data = $data; |
291
|
|
|
|
292
|
|
|
return $this; |
293
|
|
|
} |
294
|
|
|
|
295
|
|
|
/** |
296
|
|
|
* Add ContainerData to container. |
297
|
|
|
* |
298
|
|
|
* @param $containerData |
299
|
|
|
*/ |
300
|
|
|
public function addData($containerData) |
301
|
|
|
{ |
302
|
|
|
$this->data->add($containerData); |
303
|
|
|
|
304
|
|
|
return $this; |
305
|
|
|
} |
306
|
|
|
|
307
|
|
|
/** |
308
|
|
|
* Gets the value of type. |
309
|
|
|
* |
310
|
|
|
* @return int |
311
|
|
|
*/ |
312
|
|
|
public function getType() |
313
|
|
|
{ |
314
|
|
|
return $this->type; |
315
|
|
|
} |
316
|
|
|
|
317
|
|
|
/** |
318
|
|
|
* Sets the value of type. |
319
|
|
|
* |
320
|
|
|
* @param int $type the type |
321
|
|
|
* |
322
|
|
|
* @return self |
323
|
|
|
*/ |
324
|
|
|
public function setType($type) |
325
|
|
|
{ |
326
|
|
|
$this->type = $type; |
327
|
|
|
|
328
|
|
|
return $this; |
329
|
|
|
} |
330
|
|
|
|
331
|
|
|
/** |
332
|
|
|
* Gets the value of widgets. |
333
|
|
|
* |
334
|
|
|
* @return ArrayCollection |
335
|
|
|
*/ |
336
|
|
|
public function getWidgets() |
337
|
|
|
{ |
338
|
|
|
return $this->widgets; |
339
|
|
|
} |
340
|
|
|
|
341
|
|
|
/** |
342
|
|
|
* Sets the value of widgets. |
343
|
|
|
* |
344
|
|
|
* @param ArrayCollection $widgets the widgets |
345
|
|
|
* |
346
|
|
|
* @return self |
347
|
|
|
*/ |
348
|
|
|
public function setWidgets(ArrayCollection $widgets) |
349
|
|
|
{ |
350
|
|
|
$this->widgets = $widgets; |
351
|
|
|
|
352
|
|
|
return $this; |
353
|
|
|
} |
354
|
|
|
|
355
|
|
|
/** |
356
|
|
|
* Add widget to container. |
357
|
|
|
* |
358
|
|
|
* @param $widget |
359
|
|
|
*/ |
360
|
|
|
public function addWidget($widget) |
361
|
|
|
{ |
362
|
|
|
$this->widgets->add($widget); |
363
|
|
|
|
364
|
|
|
return $this; |
365
|
|
|
} |
366
|
|
|
|
367
|
|
|
/** |
368
|
|
|
* Remove widget to container. |
369
|
|
|
* |
370
|
|
|
* @param $widget |
371
|
|
|
*/ |
372
|
|
|
public function removeWidget($widget) |
373
|
|
|
{ |
374
|
|
|
$this->widgets->removeElement($widget); |
375
|
|
|
|
376
|
|
|
return $this; |
377
|
|
|
} |
378
|
|
|
|
379
|
|
|
/** |
380
|
|
|
* {@inheritdoc} |
381
|
|
|
*/ |
382
|
|
|
public function getTenant() |
383
|
|
|
{ |
384
|
|
|
return $this->tenant; |
385
|
|
|
} |
386
|
|
|
|
387
|
|
|
/** |
388
|
|
|
* {@inheritdoc} |
389
|
|
|
*/ |
390
|
|
|
public function setTenant(TenantInterface $tenant) |
391
|
|
|
{ |
392
|
|
|
$this->tenant = $tenant; |
393
|
|
|
} |
394
|
|
|
|
395
|
|
|
/** |
396
|
|
|
* {@inheritdoc} |
397
|
|
|
*/ |
398
|
|
|
public function getCreatedAt() |
399
|
|
|
{ |
400
|
|
|
return $this->createdAt; |
401
|
|
|
} |
402
|
|
|
|
403
|
|
|
/** |
404
|
|
|
* {@inheritdoc} |
405
|
|
|
*/ |
406
|
|
|
public function setCreatedAt(\DateTime $createdAt) |
407
|
|
|
{ |
408
|
|
|
$this->createdAt = $createdAt; |
409
|
|
|
} |
410
|
|
|
|
411
|
|
|
/** |
412
|
|
|
* {@inheritdoc} |
413
|
|
|
*/ |
414
|
|
|
public function getUpdatedAt() |
415
|
|
|
{ |
416
|
|
|
if (is_null($this->updatedAt)) { |
417
|
|
|
return $this->createdAt; |
418
|
|
|
} |
419
|
|
|
|
420
|
|
|
return $this->updatedAt; |
421
|
|
|
} |
422
|
|
|
|
423
|
|
|
/** |
424
|
|
|
* {@inheritdoc} |
425
|
|
|
*/ |
426
|
|
|
public function setUpdatedAt(\DateTime $updatedAt) |
427
|
|
|
{ |
428
|
|
|
$this->updatedAt = $updatedAt; |
429
|
|
|
} |
430
|
|
|
} |
431
|
|
|
|
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.