These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more
1 | <?php |
||
2 | |||
3 | namespace NotificationChannels\HipChat; |
||
4 | |||
5 | class Card |
||
6 | { |
||
7 | /** |
||
8 | * An id that will help HipChat recognise the same card when it is sent multiple times. |
||
9 | * |
||
10 | * @var string |
||
11 | */ |
||
12 | public $id; |
||
13 | |||
14 | /** |
||
15 | * The title of the card. |
||
16 | * Valid length range: 1 - 500. |
||
17 | * |
||
18 | * @var string |
||
19 | */ |
||
20 | public $title = ''; |
||
21 | |||
22 | /** |
||
23 | * Style of the card. |
||
24 | * Valid values: file, image, application, link, media. |
||
25 | * |
||
26 | * @var string |
||
27 | */ |
||
28 | public $style = CardStyles::APPLICATION; |
||
29 | |||
30 | /** |
||
31 | * The description in the specific format. |
||
32 | * Valid length range: 1 - 1000. |
||
33 | * |
||
34 | * @var string |
||
35 | */ |
||
36 | public $content = ''; |
||
37 | |||
38 | /** |
||
39 | * The format that can be html or text. |
||
40 | * |
||
41 | * @var string |
||
42 | */ |
||
43 | public $format = 'text'; |
||
44 | |||
45 | /** |
||
46 | * Application cards can be compact (1 to 2 lines) or medium (1 to 5 lines). |
||
47 | * |
||
48 | * @var string |
||
49 | */ |
||
50 | public $cardFormat; |
||
51 | |||
52 | /** |
||
53 | * The url where the card will open. |
||
54 | * |
||
55 | * @var string |
||
56 | */ |
||
57 | public $url = ''; |
||
58 | |||
59 | /** |
||
60 | * The thumbnail url. Valid length range: 1 - 250. |
||
61 | * |
||
62 | * @var string |
||
63 | */ |
||
64 | public $thumbnail; |
||
65 | |||
66 | /** |
||
67 | * The thumbnail url in retina. Valid length range: 1 - 250. |
||
68 | * |
||
69 | * @var string |
||
70 | */ |
||
71 | public $thumbnail2; |
||
72 | |||
73 | /** |
||
74 | * The original width of the image. |
||
75 | * |
||
76 | * @var int |
||
77 | */ |
||
78 | public $thumbnailWidth; |
||
79 | |||
80 | /** |
||
81 | * The original height of the image. |
||
82 | * |
||
83 | * @var int |
||
84 | */ |
||
85 | public $thumbnailHeight; |
||
86 | |||
87 | /** |
||
88 | * Html for the activity to show in one line a summary of the action that happened. |
||
89 | * |
||
90 | * @var string |
||
91 | */ |
||
92 | public $activity; |
||
93 | |||
94 | /** |
||
95 | * The activity icon url. |
||
96 | * |
||
97 | * @var string |
||
98 | */ |
||
99 | public $activityIcon; |
||
100 | |||
101 | /** |
||
102 | * The activity icon url in retina. |
||
103 | * |
||
104 | * @var string |
||
105 | */ |
||
106 | public $activityIcon2; |
||
107 | |||
108 | /** |
||
109 | * The icon url. |
||
110 | * |
||
111 | * @var string |
||
112 | */ |
||
113 | public $icon; |
||
114 | |||
115 | /** |
||
116 | * The icon url in retina. |
||
117 | * |
||
118 | * @var string |
||
119 | */ |
||
120 | public $icon2; |
||
121 | |||
122 | /** |
||
123 | * List of attributes to show below the card. Sample {label}:{value.icon} {value.label}. |
||
124 | * |
||
125 | * @var CardAttribute[] |
||
126 | */ |
||
127 | public $attributes = []; |
||
128 | |||
129 | /** |
||
130 | * Create a new Card instance. |
||
131 | * |
||
132 | * @param string $title |
||
133 | * @param string $id |
||
134 | */ |
||
135 | public function __construct($title = '', $id = '') |
||
136 | { |
||
137 | if (! empty($title)) { |
||
138 | $this->title = $title; |
||
139 | } |
||
140 | |||
141 | $this->id = $id ?: str_random(); |
||
142 | } |
||
143 | |||
144 | /** |
||
145 | * Create a new Card instance. |
||
146 | * |
||
147 | * @param string $title |
||
148 | * @param string $id |
||
149 | * @return static |
||
150 | */ |
||
151 | public static function create($title = '', $id = '') |
||
152 | { |
||
153 | return new static($title, $id); |
||
154 | } |
||
155 | |||
156 | /** |
||
157 | * Sets the title of the card. |
||
158 | * |
||
159 | * @param string $title |
||
160 | * @return $this |
||
161 | */ |
||
162 | public function title($title) |
||
163 | { |
||
164 | $this->title = trim($title); |
||
165 | return $this; |
||
166 | } |
||
167 | |||
168 | /** |
||
169 | * Sets the id of the card. |
||
170 | * |
||
171 | * @param $id |
||
172 | * @return $this |
||
173 | */ |
||
174 | public function id($id) |
||
175 | { |
||
176 | $this->id = trim($id); |
||
177 | return $this; |
||
178 | } |
||
179 | |||
180 | /** |
||
181 | * Sets the style of the card. |
||
182 | * |
||
183 | * @param $style |
||
184 | * @return $this |
||
185 | */ |
||
186 | public function style($style) |
||
187 | { |
||
188 | $this->style = $style; |
||
189 | return $this; |
||
190 | } |
||
191 | |||
192 | /** |
||
193 | * Sets the content of the card. |
||
194 | * |
||
195 | * @param $content |
||
196 | * @return $this |
||
197 | */ |
||
198 | public function content($content) |
||
199 | { |
||
200 | $this->content = trim($content); |
||
201 | return $this; |
||
202 | } |
||
203 | |||
204 | /** |
||
205 | * Sets the format to plain text and optionally the content. |
||
206 | * |
||
207 | * @param string $content |
||
208 | * @return $this |
||
209 | */ |
||
210 | public function text($content = '') |
||
211 | { |
||
212 | $this->format = 'text'; |
||
213 | |||
214 | if (! empty($content)) { |
||
215 | $this->content($content); |
||
216 | } |
||
217 | |||
218 | return $this; |
||
219 | } |
||
220 | |||
221 | /** |
||
222 | * Sets the format to html and optionally the content. |
||
223 | * |
||
224 | * @param string $content |
||
225 | * @return $this |
||
226 | */ |
||
227 | public function html($content = '') |
||
228 | { |
||
229 | $this->format = 'html'; |
||
230 | |||
231 | if (! empty($content)) { |
||
232 | $this->content($content); |
||
233 | } |
||
234 | |||
235 | return $this; |
||
236 | } |
||
237 | |||
238 | /** |
||
239 | * Sets the format of the card. |
||
240 | * |
||
241 | * @param $cardFormat |
||
242 | * @return $this |
||
243 | */ |
||
244 | public function cardFormat($cardFormat) |
||
245 | { |
||
246 | $this->cardFormat = trim($cardFormat); |
||
247 | return $this; |
||
248 | } |
||
249 | |||
250 | /** |
||
251 | * Sets the url of the card. |
||
252 | * |
||
253 | * @param $url |
||
254 | * @return $this |
||
255 | */ |
||
256 | public function url($url) |
||
257 | { |
||
258 | $this->url = trim($url); |
||
259 | return $this; |
||
260 | } |
||
261 | |||
262 | /** |
||
263 | * Sets the thumbnail of the card. |
||
264 | * |
||
265 | * @param string $icon |
||
266 | * @param string|null $icon2 |
||
267 | * @param string|null $width |
||
268 | * @param string|null $height |
||
269 | * @return $this |
||
270 | */ |
||
271 | public function thumbnail($icon, $icon2 = null, $width = null, $height = null) |
||
272 | { |
||
273 | $this->thumbnail = trim($icon); |
||
274 | |||
275 | if (! empty($icon2)) { |
||
276 | $this->thumbnail2 = trim($icon2); |
||
277 | } |
||
278 | |||
279 | if (! empty($width)) { |
||
280 | $this->thumbnailWidth = $width; |
||
0 ignored issues
–
show
|
|||
281 | } |
||
282 | |||
283 | if (! empty($height)) { |
||
284 | $this->thumbnailHeight = $height; |
||
0 ignored issues
–
show
The property
$thumbnailHeight was declared of type integer , but $height is of type string . Maybe add a type cast?
This check looks for assignments to scalar types that may be of the wrong type. To ensure the code behaves as expected, it may be a good idea to add an explicit type cast. $answer = 42;
$correct = false;
$correct = (bool) $answer;
Loading history...
|
|||
285 | } |
||
286 | |||
287 | return $this; |
||
288 | } |
||
289 | |||
290 | /** |
||
291 | * Sets the activity of the card. |
||
292 | * |
||
293 | * @param string $html |
||
294 | * @param string|null $icon |
||
295 | * @param string|null $icon2 |
||
296 | * @return $this |
||
297 | */ |
||
298 | public function activity($html, $icon = null, $icon2 = null) |
||
299 | { |
||
300 | $this->activity = trim($html); |
||
301 | |||
302 | if (! empty($icon)) { |
||
303 | $this->activityIcon = trim($icon); |
||
304 | } |
||
305 | |||
306 | if (! empty($icon2)) { |
||
307 | $this->activityIcon2 = trim($icon2); |
||
308 | } |
||
309 | |||
310 | return $this; |
||
311 | } |
||
312 | |||
313 | /** |
||
314 | * Sets the icon of the card. |
||
315 | * |
||
316 | * @param string $icon |
||
317 | * @param string|null $icon2 |
||
318 | * @return $this |
||
319 | */ |
||
320 | View Code Duplication | public function icon($icon, $icon2 = null) |
|
321 | { |
||
322 | $this->icon = trim($icon); |
||
323 | |||
324 | if (! empty($icon2)) { |
||
325 | $this->icon2 = trim($icon2); |
||
326 | } |
||
327 | |||
328 | return $this; |
||
329 | } |
||
330 | |||
331 | /** |
||
332 | * Adds a CardAttribute to the card. |
||
333 | * |
||
334 | * @param CardAttribute|\Closure $attribute |
||
335 | * @return $this |
||
336 | */ |
||
337 | View Code Duplication | public function addAttribute($attribute) |
|
338 | { |
||
339 | if ($attribute instanceof CardAttribute) { |
||
340 | $this->attributes[] = $attribute; |
||
341 | return $this; |
||
342 | } |
||
343 | |||
344 | if ($attribute instanceof \Closure) { |
||
345 | $attribute($new = new CardAttribute()); |
||
346 | $this->attributes[] = $new; |
||
347 | return $this; |
||
348 | } |
||
349 | |||
350 | throw new \InvalidArgumentException('Invalid attribute type. Expected '.CardAttribute::class.' or '.\Closure::class.'.'); |
||
351 | } |
||
352 | |||
353 | /** |
||
354 | * Get an array representation of the Card. |
||
355 | * |
||
356 | * @return array |
||
357 | */ |
||
358 | public function toArray() |
||
359 | { |
||
360 | $card = array_filter([ |
||
361 | 'id' => $this->id, |
||
362 | 'style' => $this->style, |
||
363 | 'format' => $this->cardFormat, |
||
364 | 'title' => $this->title, |
||
365 | 'url' => $this->url, |
||
366 | ]); |
||
367 | |||
368 | if (! empty($this->content)) { |
||
369 | $card['description'] = [ |
||
370 | 'value' => $this->content, |
||
371 | 'format' => $this->format, |
||
372 | ]; |
||
373 | } |
||
374 | |||
375 | if (! empty($this->thumbnail)) { |
||
376 | $card['thumbnail'] = array_filter([ |
||
377 | 'url' => $this->thumbnail, |
||
378 | 'url@2x' => $this->thumbnail2, |
||
379 | 'width' => $this->thumbnailWidth, |
||
380 | 'height' => $this->thumbnailHeight, |
||
381 | ]); |
||
382 | } |
||
383 | |||
384 | if (! empty($this->activity)) { |
||
385 | $card['activity'] = array_filter([ |
||
386 | 'html' => $this->activity, |
||
387 | 'icon' => array_filter([ |
||
388 | 'url' => $this->activityIcon, |
||
389 | 'url@2x' => $this->activityIcon2, |
||
390 | ]), |
||
391 | ]); |
||
392 | } |
||
393 | |||
394 | if (! empty($this->icon)) { |
||
395 | $card['icon'] = array_filter([ |
||
396 | 'url' => $this->icon, |
||
397 | 'url@2x' => $this->icon2, |
||
398 | ]); |
||
399 | } |
||
400 | |||
401 | if (! empty($this->attributes)) { |
||
402 | $card['attributes'] = array_map(function ($attribute) { |
||
403 | return $attribute->toArray(); |
||
404 | }, $this->attributes); |
||
405 | } |
||
406 | |||
407 | return $card; |
||
408 | } |
||
409 | } |
||
410 |
This check looks for assignments to scalar types that may be of the wrong type.
To ensure the code behaves as expected, it may be a good idea to add an explicit type cast.