Skip to content
This repository was archived by the owner on Sep 27, 2019. It is now read-only.

Commit 40c3c0c

Browse files
committed
Better handle falsey string input. Relates to #20
1 parent fbdbb02 commit 40c3c0c

14 files changed

+308
-109
lines changed

src/Card.php

Lines changed: 27 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,9 @@
22

33
namespace NotificationChannels\HipChat;
44

5+
use Closure;
6+
use InvalidArgumentException;
7+
58
class Card
69
{
710
/**
@@ -134,11 +137,8 @@ class Card
134137
*/
135138
public function __construct($title = '', $id = '')
136139
{
137-
if (! empty($title)) {
138-
$this->title = $title;
139-
}
140-
141-
$this->id = $id ?: str_random();
140+
$this->title($title);
141+
$this->id(str_empty($id) ? str_random() : $id);
142142
}
143143

144144
/**
@@ -215,7 +215,7 @@ public function text($content = '')
215215
{
216216
$this->format = 'text';
217217

218-
if (! empty($content)) {
218+
if (! str_empty($content)) {
219219
$this->content($content);
220220
}
221221

@@ -232,7 +232,7 @@ public function html($content = '')
232232
{
233233
$this->format = 'html';
234234

235-
if (! empty($content)) {
235+
if (! str_empty($content)) {
236236
$this->content($content);
237237
}
238238

@@ -278,15 +278,15 @@ public function thumbnail($icon, $icon2 = null, $width = null, $height = null)
278278
{
279279
$this->thumbnail = trim($icon);
280280

281-
if (! empty($icon2)) {
281+
if (! str_empty($icon2)) {
282282
$this->thumbnail2 = trim($icon2);
283283
}
284284

285-
if (! empty($width)) {
285+
if (! is_null($width)) {
286286
$this->thumbnailWidth = $width;
287287
}
288288

289-
if (! empty($height)) {
289+
if (! is_null($height)) {
290290
$this->thumbnailHeight = $height;
291291
}
292292

@@ -305,11 +305,11 @@ public function activity($html, $icon = null, $icon2 = null)
305305
{
306306
$this->activity = trim($html);
307307

308-
if (! empty($icon)) {
308+
if (! str_empty($icon)) {
309309
$this->activityIcon = trim($icon);
310310
}
311311

312-
if (! empty($icon2)) {
312+
if (! str_empty($icon2)) {
313313
$this->activityIcon2 = trim($icon2);
314314
}
315315

@@ -327,7 +327,7 @@ public function icon($icon, $icon2 = null)
327327
{
328328
$this->icon = trim($icon);
329329

330-
if (! empty($icon2)) {
330+
if (! str_empty($icon2)) {
331331
$this->icon2 = trim($icon2);
332332
}
333333

@@ -337,7 +337,7 @@ public function icon($icon, $icon2 = null)
337337
/**
338338
* Adds a CardAttribute to the card.
339339
*
340-
* @param CardAttribute|\Closure $attribute
340+
* @param CardAttribute|Closure $attribute
341341
* @return $this
342342
*/
343343
public function addAttribute($attribute)
@@ -348,14 +348,16 @@ public function addAttribute($attribute)
348348
return $this;
349349
}
350350

351-
if ($attribute instanceof \Closure) {
351+
if ($attribute instanceof Closure) {
352352
$attribute($new = new CardAttribute());
353353
$this->attributes[] = $new;
354354

355355
return $this;
356356
}
357357

358-
throw new \InvalidArgumentException('Invalid attribute type. Expected '.CardAttribute::class.' or '.\Closure::class.'.');
358+
throw new InvalidArgumentException(
359+
'Invalid attribute type. Expected '.CardAttribute::class.' or '. Closure::class.'.'
360+
);
359361
}
360362

361363
/**
@@ -365,42 +367,42 @@ public function addAttribute($attribute)
365367
*/
366368
public function toArray()
367369
{
368-
$card = array_filter([
370+
$card = str_array_filter([
369371
'id' => $this->id,
370372
'style' => $this->style,
371373
'format' => $this->cardFormat,
372374
'title' => $this->title,
373375
'url' => $this->url,
374376
]);
375377

376-
if (! empty($this->content)) {
378+
if (! str_empty($this->content)) {
377379
$card['description'] = [
378380
'value' => $this->content,
379381
'format' => $this->format,
380382
];
381383
}
382384

383-
if (! empty($this->thumbnail)) {
384-
$card['thumbnail'] = array_filter([
385+
if (! str_empty($this->thumbnail)) {
386+
$card['thumbnail'] = str_array_filter([
385387
'url' => $this->thumbnail,
386388
'url@2x' => $this->thumbnail2,
387389
'width' => $this->thumbnailWidth,
388390
'height' => $this->thumbnailHeight,
389391
]);
390392
}
391393

392-
if (! empty($this->activity)) {
393-
$card['activity'] = array_filter([
394+
if (! str_empty($this->activity)) {
395+
$card['activity'] = str_array_filter([
394396
'html' => $this->activity,
395-
'icon' => array_filter([
397+
'icon' => str_array_filter([
396398
'url' => $this->activityIcon,
397399
'url@2x' => $this->activityIcon2,
398400
]),
399401
]);
400402
}
401403

402-
if (! empty($this->icon)) {
403-
$card['icon'] = array_filter([
404+
if (! str_empty($this->icon)) {
405+
$card['icon'] = str_array_filter([
404406
'url' => $this->icon,
405407
'url@2x' => $this->icon2,
406408
]);

src/CardAttribute.php

Lines changed: 10 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@ public function style($style)
8585
}
8686

8787
/**
88-
* Sets the icon for the atttribute.
88+
* Sets the icon for the attribute.
8989
*
9090
* @param string $icon
9191
* @param string|null $icon2
@@ -95,7 +95,7 @@ public function icon($icon, $icon2 = null)
9595
{
9696
$this->icon = trim($icon);
9797

98-
if (! empty($icon2)) {
98+
if (! str_empty($icon2)) {
9999
$this->icon2 = trim($icon2);
100100
}
101101

@@ -110,13 +110,8 @@ public function icon($icon, $icon2 = null)
110110
*/
111111
public function __construct($value = null, $label = null)
112112
{
113-
if (trim($value) !== '') {
114-
$this->value($value);
115-
}
116-
117-
if (trim($label) !== '') {
118-
$this->label($label);
119-
}
113+
$this->value($value);
114+
$this->label($label);
120115
}
121116

122117
/**
@@ -134,25 +129,21 @@ public static function create($value = null, $label = null)
134129
public function toArray()
135130
{
136131
$attribute = [
137-
'value' => array_filter([
132+
'value' => str_array_filter([
138133
'label' => $this->value,
139134
'url' => $this->url,
140135
'style' => $this->style,
141-
], function ($value) {
142-
return trim($value) !== '';
143-
}),
136+
]),
144137
];
145138

146-
if (! empty($this->icon)) {
147-
$attribute['value']['icon'] = array_filter([
139+
if (! str_empty($this->icon)) {
140+
$attribute['value']['icon'] = str_array_filter([
148141
'url' => $this->icon,
149142
'url@2x' => $this->icon2,
150-
], function ($value) {
151-
return trim($value) !== '';
152-
});
143+
]);
153144
}
154145

155-
if (! empty($this->label)) {
146+
if (! str_empty($this->label)) {
156147
$attribute['label'] = $this->label;
157148
}
158149

src/HipChat.php

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,19 +10,29 @@
1010

1111
class HipChat
1212
{
13-
/** @var string */
13+
/**
14+
* @var string
15+
*/
1416
protected $token;
1517

16-
/** @var HttpClient */
18+
/**
19+
* @var HttpClient
20+
*/
1721
protected $http;
1822

19-
/** @var string */
23+
/**
24+
* @var string
25+
*/
2026
protected $url;
2127

22-
/** @var string */
28+
/**
29+
* @var string
30+
*/
2331
protected $room;
2432

2533
/**
34+
* Create a new instance.
35+
*
2636
* @param HttpClient $http
2737
* @param string $token
2838
* @param string|null $url
@@ -90,7 +100,7 @@ public function shareFile($to, $file)
90100
'filename' => $file['filename'] ?: 'untitled',
91101
];
92102

93-
if (! empty($file['message'])) {
103+
if (! str_empty($file['message'])) {
94104
$parts[] = [
95105
'headers' => [
96106
'Content-Type' => 'application/json',

src/HipChatChannel.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
namespace NotificationChannels\HipChat;
44

5+
use Exception;
56
use GuzzleHttp\Exception\ClientException;
67
use Illuminate\Notifications\Notification;
78
use NotificationChannels\HipChat\Exceptions\CouldNotSendNotification;
@@ -54,8 +55,8 @@ public function send($notifiable, Notification $notification)
5455
$this->sendMessage($to, $message);
5556
} catch (ClientException $exception) {
5657
throw CouldNotSendNotification::hipChatRespondedWithAnError($exception);
57-
} catch (\Exception $exception) {
58-
throw CouldNotSendNotification::internalError();
58+
} catch (Exception $exception) {
59+
throw CouldNotSendNotification::internalError($exception);
5960
}
6061
}
6162

src/HipChatFile.php

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ class HipChatFile
4747
*/
4848
public function __construct($path = '')
4949
{
50-
if (! empty($path)) {
50+
if ($path !== '') {
5151
$this->path($path);
5252
}
5353
}
@@ -108,11 +108,13 @@ public function text($text)
108108
*/
109109
public function path($path)
110110
{
111-
if (empty($this->fileName)) {
111+
$path = trim($path);
112+
113+
if (str_empty($this->fileName)) {
112114
$this->fileName(basename($path));
113115
}
114116

115-
if (empty($this->fileType)) {
117+
if (str_empty($this->fileType)) {
116118
$this->fileType(mime_content_type($path));
117119
}
118120

src/HipChatMessage.php

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,9 @@
22

33
namespace NotificationChannels\HipChat;
44

5+
use Closure;
6+
use InvalidArgumentException;
7+
58
class HipChatMessage
69
{
710
/**
@@ -167,7 +170,7 @@ public function html($content = '')
167170
{
168171
$this->format = 'html';
169172

170-
if (! empty($content)) {
173+
if (! str_empty($content)) {
171174
$this->content($content);
172175
}
173176

@@ -184,7 +187,7 @@ public function text($content = '')
184187
{
185188
$this->format = 'text';
186189

187-
if (! empty($content)) {
190+
if (! str_empty($content)) {
188191
$this->content($content);
189192
}
190193

@@ -235,7 +238,7 @@ public function color($color)
235238
/**
236239
* Sets the Card.
237240
*
238-
* @param Card|\Closure|null $card
241+
* @param Card|Closure|null $card
239242
* @return $this
240243
*/
241244
public function card($card)
@@ -246,14 +249,16 @@ public function card($card)
246249
return $this;
247250
}
248251

249-
if ($card instanceof \Closure) {
252+
if ($card instanceof Closure) {
250253
$card($new = new Card());
251254
$this->card = $new;
252255

253256
return $this;
254257
}
255258

256-
throw new \InvalidArgumentException('Invalid Card type. Expected '.Card::class.' or '.\Closure::class.'.');
259+
throw new InvalidArgumentException(
260+
'Invalid Card type. Expected '.Card::class.' or '. Closure::class.'.'
261+
);
257262
}
258263

259264
/**
@@ -276,7 +281,7 @@ public function attachTo($id)
276281
*/
277282
public function toArray()
278283
{
279-
$message = array_filter([
284+
$message = str_array_filter([
280285
'from' => $this->from,
281286
'message_format' => $this->format,
282287
'color' => $this->color,
@@ -285,7 +290,7 @@ public function toArray()
285290
'attach_to' => $this->attachTo,
286291
]);
287292

288-
if (! empty($this->card)) {
293+
if (! is_null($this->card)) {
289294
$message['card'] = $this->card->toArray();
290295
}
291296

0 commit comments

Comments
 (0)