|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Charcoal\Cms; |
|
4
|
|
|
|
|
5
|
|
|
// Dependencies from `charcoal-translation` |
|
6
|
|
|
use \Charcoal\Translation\TranslationString; |
|
7
|
|
|
|
|
8
|
|
|
// Module `charcoal-base` dependencies |
|
9
|
|
|
use \Charcoal\Object\Content; |
|
10
|
|
|
use \Charcoal\Object\CategorizableInterface; |
|
11
|
|
|
use \Charcoal\Object\CategorizableTrait; |
|
12
|
|
|
use \Charcoal\Object\PublishableInterface; |
|
13
|
|
|
use \Charcoal\Object\PublishableTrait; |
|
14
|
|
|
|
|
15
|
|
|
// Intra-module (`charcoal-cms`) dependencies |
|
16
|
|
|
use \Charcoal\Cms\SearchableInterface; |
|
17
|
|
|
use \Charcoal\Cms\SearchableTrait; |
|
18
|
|
|
use \Charcoal\Cms\TextInterface; |
|
19
|
|
|
|
|
20
|
|
|
/** |
|
21
|
|
|
* Text |
|
22
|
|
|
*/ |
|
23
|
|
|
abstract class AbstractText extends Content implements |
|
24
|
|
|
CategorizableInterface, |
|
25
|
|
|
PublishableInterface, |
|
26
|
|
|
SearchableInterface, |
|
27
|
|
|
TextInterface |
|
28
|
|
|
{ |
|
29
|
|
|
use CategorizableTrait; |
|
30
|
|
|
use PublishableTrait; |
|
31
|
|
|
use SearchableTrait; |
|
32
|
|
|
|
|
33
|
|
|
/** |
|
34
|
|
|
* @var TranslationString $title |
|
35
|
|
|
*/ |
|
36
|
|
|
private $title; |
|
37
|
|
|
|
|
38
|
|
|
/** |
|
39
|
|
|
* @var TranslationString $title |
|
40
|
|
|
*/ |
|
41
|
|
|
private $subtitle; |
|
42
|
|
|
|
|
43
|
|
|
/** |
|
44
|
|
|
* @var TranslationString $content |
|
45
|
|
|
*/ |
|
46
|
|
|
private $content; |
|
47
|
|
|
|
|
48
|
|
|
/** |
|
49
|
|
|
* @param mixed $title The news title (localized). |
|
50
|
|
|
* @return TranslationString |
|
51
|
|
|
*/ |
|
52
|
|
|
public function setTitle($title) |
|
53
|
|
|
{ |
|
54
|
|
|
$this->title = new TranslationString($title); |
|
55
|
|
|
return $this; |
|
|
|
|
|
|
56
|
|
|
} |
|
57
|
|
|
|
|
58
|
|
|
/** |
|
59
|
|
|
* @return TranslationString |
|
60
|
|
|
*/ |
|
61
|
|
|
public function title() |
|
62
|
|
|
{ |
|
63
|
|
|
return $this->title; |
|
|
|
|
|
|
64
|
|
|
} |
|
65
|
|
|
|
|
66
|
|
|
/** |
|
67
|
|
|
* @param mixed $subtitle The news subtitle (localized). |
|
68
|
|
|
* @return Event Chainable |
|
69
|
|
|
*/ |
|
70
|
|
|
public function setSubtitle($subtitle) |
|
71
|
|
|
{ |
|
72
|
|
|
$this->subtitle = new TranslationString($subtitle); |
|
73
|
|
|
return $this; |
|
|
|
|
|
|
74
|
|
|
} |
|
75
|
|
|
|
|
76
|
|
|
/** |
|
77
|
|
|
* @return TranslationString |
|
78
|
|
|
*/ |
|
79
|
|
|
public function subtitle() |
|
80
|
|
|
{ |
|
81
|
|
|
return $this->subtitle; |
|
|
|
|
|
|
82
|
|
|
} |
|
83
|
|
|
|
|
84
|
|
|
/** |
|
85
|
|
|
* @param mixed $content The news content (localized). |
|
86
|
|
|
* @return Event Chainable |
|
87
|
|
|
*/ |
|
88
|
|
|
public function setContent($content) |
|
89
|
|
|
{ |
|
90
|
|
|
$this->content = new TranslationString($content); |
|
91
|
|
|
return $this; |
|
|
|
|
|
|
92
|
|
|
} |
|
93
|
|
|
|
|
94
|
|
|
/** |
|
95
|
|
|
* @return TranslationString |
|
96
|
|
|
*/ |
|
97
|
|
|
public function content() |
|
98
|
|
|
{ |
|
99
|
|
|
return $this->content; |
|
|
|
|
|
|
100
|
|
|
} |
|
101
|
|
|
} |
|
102
|
|
|
|
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_functionexpects aPostobject, and outputs the author of the post. The base classPostreturns a simple string and outputting a simple string will work just fine. However, the child classBlogPostwhich is a sub-type ofPostinstead decided to return anobject, and is therefore violating the SOLID principles. If aBlogPostwere passed tomy_function, PHP would not complain, but ultimately fail when executing thestrtouppercall in its body.