1 | <?php declare(strict_types=1); |
||
16 | class Node implements NodeInterface, ElementsAwareInterface, ArrayableInterface |
||
17 | { |
||
18 | use ElementsAwareTrait; |
||
19 | |||
20 | /** |
||
21 | * @var \ArrayIterator |
||
22 | */ |
||
23 | protected $categories; |
||
24 | |||
25 | /** |
||
26 | * @var string |
||
27 | */ |
||
28 | protected $title; |
||
29 | |||
30 | /** |
||
31 | * @var string |
||
32 | */ |
||
33 | protected $publicId; |
||
34 | |||
35 | /** |
||
36 | * @var string |
||
37 | */ |
||
38 | protected $description; |
||
39 | |||
40 | /** |
||
41 | * @var \DateTime |
||
42 | */ |
||
43 | protected $lastModified; |
||
44 | |||
45 | /** |
||
46 | * @var string |
||
47 | */ |
||
48 | protected $link; |
||
49 | |||
50 | /** |
||
51 | * @var string |
||
52 | */ |
||
53 | protected $host; |
||
54 | |||
55 | 120 | public function __construct() |
|
60 | |||
61 | /** |
||
62 | * @param string $name element name |
||
63 | * @param string $value element value |
||
64 | * @return NodeInterface |
||
65 | */ |
||
66 | 13 | public function set(string $name, string $value = null) : NodeInterface |
|
77 | |||
78 | /** |
||
79 | * returns node's categories |
||
80 | * |
||
81 | * @return iterable |
||
82 | */ |
||
83 | 12 | public function getCategories() : iterable |
|
87 | |||
88 | /** |
||
89 | * @return \Generator |
||
90 | */ |
||
91 | 5 | public function getCategoriesGenerator() : \Generator |
|
97 | |||
98 | /** |
||
99 | * adds a category to the node |
||
100 | * |
||
101 | * @param \FeedIo\Feed\Node\CategoryInterface $category |
||
102 | * @return NodeInterface |
||
103 | */ |
||
104 | 16 | public function addCategory(CategoryInterface $category) : NodeInterface |
|
110 | |||
111 | /** |
||
112 | * returns a new CategoryInterface |
||
113 | * |
||
114 | * @return \FeedIo\Feed\Node\CategoryInterface |
||
115 | */ |
||
116 | 9 | public function newCategory() : CategoryInterface |
|
120 | |||
121 | /** |
||
122 | * @return string |
||
123 | */ |
||
124 | 24 | public function getTitle() : ? string |
|
128 | |||
129 | /** |
||
130 | * @param string $title |
||
131 | * @return NodeInterface |
||
132 | */ |
||
133 | 29 | public function setTitle(string $title = null) : NodeInterface |
|
139 | |||
140 | /** |
||
141 | * @return string |
||
142 | */ |
||
143 | 14 | public function getPublicId() : ? string |
|
147 | |||
148 | /** |
||
149 | * @param string $publicId |
||
150 | * @return NodeInterface |
||
151 | */ |
||
152 | 16 | public function setPublicId(string $publicId = null) : NodeInterface |
|
158 | |||
159 | /** |
||
160 | * @return string |
||
161 | */ |
||
162 | 15 | public function getDescription() : ? string |
|
166 | |||
167 | /** |
||
168 | * @param string $description |
||
169 | * @return NodeInterface |
||
170 | */ |
||
171 | 23 | public function setDescription(string $description = null) : NodeInterface |
|
177 | |||
178 | /** |
||
179 | * @return \DateTime |
||
180 | */ |
||
181 | 27 | public function getLastModified() : ? \DateTime |
|
185 | |||
186 | /** |
||
187 | * @param \DateTime $lastModified |
||
188 | * @return NodeInterface |
||
189 | */ |
||
190 | 34 | public function setLastModified(\DateTime $lastModified = null) : NodeInterface |
|
196 | |||
197 | /** |
||
198 | * @return string |
||
199 | */ |
||
200 | 16 | public function getHost(): ? string |
|
204 | |||
205 | /** |
||
206 | * @return string |
||
207 | */ |
||
208 | 19 | public function getLink() : ? string |
|
212 | |||
213 | /** |
||
214 | * @param string $link |
||
215 | * @return NodeInterface |
||
216 | */ |
||
217 | 27 | public function setLink(string $link = null) : NodeInterface |
|
224 | |||
225 | /** |
||
226 | * @param string|null $link |
||
227 | */ |
||
228 | 27 | protected function setHost(string $link = null): void |
|
234 | |||
235 | /** |
||
236 | * @param string $name element name |
||
237 | * @return null|string |
||
238 | */ |
||
239 | 4 | public function getValue(string $name) : ? string |
|
247 | |||
248 | /** |
||
249 | * @return array |
||
250 | */ |
||
251 | 4 | public function toArray() : array |
|
275 | } |
||
276 |
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.