1 | <?php |
||
9 | class AttachmentBuilder implements BuilderInterface |
||
10 | { |
||
11 | private $data = []; |
||
12 | private $fields; |
||
13 | private $parent; |
||
14 | |||
15 | /** |
||
16 | * Constructor. |
||
17 | */ |
||
18 | 11 | public function __construct(MessageBuilder $parent = null) |
|
19 | { |
||
20 | 11 | $this->parent = $parent; |
|
21 | 11 | $this->fields = new FieldCollection(); |
|
22 | 11 | } |
|
23 | |||
24 | /** |
||
25 | * @throws \LogicException When called before setFallback($fallback) |
||
26 | * |
||
27 | * @return Attachment |
||
28 | */ |
||
29 | 6 | public function create() |
|
30 | { |
||
31 | 6 | $attachment = new Attachment($this->data + ['fields' => clone $this->fields]); |
|
32 | 6 | $this->refresh(); |
|
33 | |||
34 | 6 | return $attachment; |
|
35 | } |
||
36 | |||
37 | /** |
||
38 | * @param $fallback |
||
39 | * |
||
40 | * @return $this |
||
41 | */ |
||
42 | 7 | public function setFallback($fallback) |
|
46 | |||
47 | /** |
||
48 | * @param $text |
||
49 | * |
||
50 | * @return $this |
||
51 | */ |
||
52 | 1 | public function setText($text) |
|
53 | { |
||
54 | 1 | return $this->setParameter('text', $text); |
|
55 | } |
||
56 | |||
57 | /** |
||
58 | * @param $pretext |
||
59 | * |
||
60 | * @return $this |
||
61 | */ |
||
62 | 1 | public function setPretext($pretext) |
|
63 | { |
||
64 | 1 | return $this->setParameter('pretext', $pretext); |
|
65 | } |
||
66 | |||
67 | /** |
||
68 | * @param $color |
||
69 | * |
||
70 | * @return $this |
||
71 | */ |
||
72 | 1 | public function setColor($color) |
|
73 | { |
||
74 | 1 | return $this->setParameter('color', $color); |
|
75 | } |
||
76 | |||
77 | /** |
||
78 | * @param $author_name |
||
79 | * |
||
80 | * @return $this |
||
81 | */ |
||
82 | 1 | public function setAuthorName($author_name) |
|
86 | |||
87 | /** |
||
88 | * @param $author_link |
||
89 | * |
||
90 | * @return $this |
||
91 | */ |
||
92 | 1 | public function setAuthorLink($author_link) |
|
96 | |||
97 | /** |
||
98 | * @param $author_icon |
||
99 | * |
||
100 | * @return $this |
||
101 | */ |
||
102 | 1 | public function setAuthorIcon($author_icon) |
|
106 | |||
107 | /** |
||
108 | * @param $title |
||
109 | * |
||
110 | * @return $this |
||
111 | */ |
||
112 | 1 | public function setTitle($title) |
|
116 | |||
117 | /** |
||
118 | * @param $title_link |
||
119 | * |
||
120 | * @return $this |
||
121 | */ |
||
122 | 1 | public function setTitleLink($title_link) |
|
126 | |||
127 | /** |
||
128 | * @param $image_url |
||
129 | * |
||
130 | * @return $this |
||
131 | */ |
||
132 | 1 | public function setImageUrl($image_url) |
|
136 | |||
137 | /** |
||
138 | * @param $thumb_url |
||
139 | * |
||
140 | * @return $this |
||
141 | */ |
||
142 | 1 | public function setThumbUrl($thumb_url) |
|
146 | |||
147 | /** |
||
148 | * @param $mrkdwn_in |
||
149 | * |
||
150 | * @return $this |
||
151 | */ |
||
152 | 1 | public function setMrkdwnIn($mrkdwn_in) |
|
156 | |||
157 | /** |
||
158 | * Sets values on non-empty parameters. |
||
159 | * Set $value to null to remove the custom value. |
||
160 | * |
||
161 | * @param string $name |
||
162 | * @param $value |
||
163 | * |
||
164 | * @return $this |
||
165 | */ |
||
166 | 7 | private function setParameter($name, $value) |
|
176 | |||
177 | /** |
||
178 | * @param string $title |
||
179 | * @param string $value |
||
180 | * @param bool $isShort |
||
181 | * |
||
182 | * @return $this |
||
183 | */ |
||
184 | 3 | public function addField($title, $value, $isShort) |
|
185 | { |
||
186 | 3 | $this->fields->add(new Field($title, $value, $isShort)); |
|
187 | |||
188 | 3 | return $this; |
|
189 | } |
||
190 | |||
191 | /** |
||
192 | * Reset the attachment data and the fields collection. |
||
193 | */ |
||
194 | 6 | protected function refresh() |
|
199 | |||
200 | /** |
||
201 | * @return MessageBuilder |
||
202 | */ |
||
203 | 3 | public function end() |
|
213 | } |
||
214 |
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.