1 | <?php |
||
15 | final class GenericMessage implements MessageInterface |
||
16 | { |
||
17 | /** |
||
18 | * @var array |
||
19 | */ |
||
20 | private $headers = [ |
||
21 | 'return-path' => [], |
||
22 | 'received' => [], |
||
23 | 'dkim-signature' => [], |
||
24 | 'domainkey-signature' => [], |
||
25 | 'sender' => [], |
||
26 | 'message-id' => [], |
||
27 | 'date' => [], |
||
28 | 'subject' => [], |
||
29 | 'from' => [], |
||
30 | 'reply-to' => [], |
||
31 | 'to' => [], |
||
32 | 'cc' => [], |
||
33 | 'bcc' => [], |
||
34 | 'mime-version' => [], |
||
35 | 'content-type' => [], |
||
36 | 'content-transfer-encoding' => [], |
||
37 | ]; |
||
38 | /** |
||
39 | * @var StreamInterface |
||
40 | */ |
||
41 | private $body; |
||
42 | |||
43 | /** |
||
44 | * ImmutableMessage constructor. |
||
45 | */ |
||
46 | 56 | public function __construct() |
|
51 | |||
52 | /** |
||
53 | * @return iterable |
||
54 | */ |
||
55 | 5 | public function getHeaders(): iterable |
|
59 | |||
60 | /** |
||
61 | * @param string $name |
||
62 | * @return bool |
||
63 | */ |
||
64 | 16 | public function hasHeader(string $name): bool |
|
69 | |||
70 | /** |
||
71 | * @param string $name |
||
72 | * @return HeaderInterface[]|iterable |
||
73 | */ |
||
74 | 16 | public function getHeader(string $name): iterable |
|
84 | |||
85 | /** |
||
86 | * @param HeaderInterface $header |
||
87 | * @return MessageInterface |
||
88 | */ |
||
89 | 42 | public function withHeader(HeaderInterface $header): MessageInterface |
|
95 | |||
96 | /** |
||
97 | * @param HeaderInterface $header |
||
98 | * @return MessageInterface |
||
99 | */ |
||
100 | 3 | public function withAddedHeader(HeaderInterface $header): MessageInterface |
|
106 | |||
107 | /** |
||
108 | * @param string $name |
||
109 | * @return MessageInterface |
||
110 | */ |
||
111 | 5 | public function withoutHeader(string $name): MessageInterface |
|
117 | |||
118 | /** |
||
119 | * @param StreamInterface $body |
||
120 | * @return MessageInterface |
||
121 | */ |
||
122 | 27 | public function withBody(StreamInterface $body): MessageInterface |
|
128 | |||
129 | /** |
||
130 | * @return StreamInterface |
||
131 | */ |
||
132 | 3 | public function getBody(): StreamInterface |
|
136 | |||
137 | /** |
||
138 | * @return string |
||
139 | */ |
||
140 | 21 | public function __toString(): string |
|
163 | |||
164 | /** |
||
165 | * @param string $messageString |
||
166 | * @return MessageInterface |
||
167 | */ |
||
168 | 11 | public static function fromString(string $messageString): MessageInterface |
|
198 | } |
||
199 |
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.