1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Pageon\SlackWebhookMonolog\Slack\Attachment; |
4
|
|
|
|
5
|
|
|
use Pageon\SlackWebhookMonolog\General\SerializeToString; |
6
|
|
|
use Pageon\SlackWebhookMonolog\Slack\Exceptions\InvalidColourException; |
7
|
|
|
|
8
|
|
|
/** |
9
|
|
|
* An optional value that can either be one of good, warning, danger, or any hex color code (eg. #439FE0). |
10
|
|
|
* This value is used to color the border along the left side of the message attachment. |
11
|
|
|
* |
12
|
|
|
* @author Jelmer Prins <[email protected]> |
13
|
|
|
* |
14
|
|
|
* @since 0.4.0 |
15
|
|
|
*/ |
16
|
|
|
final class Colour extends SerializeToString |
17
|
|
|
{ |
18
|
|
|
const COLOUR_GOOD = 'good'; |
19
|
|
|
const COLOUR_WARNING = 'warning'; |
20
|
|
|
const COLOUR_DANGER = 'danger'; |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* @var string |
24
|
|
|
*/ |
25
|
|
|
private $colour; |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* @param string $colour |
29
|
|
|
*/ |
30
|
17 |
|
public function __construct($colour) |
31
|
|
|
{ |
32
|
17 |
|
$this->setColour($colour); |
33
|
16 |
|
} |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* Set the colour if it is a valid colour. |
37
|
|
|
* |
38
|
|
|
* @param $colour |
39
|
|
|
*/ |
40
|
17 |
|
private function setColour($colour) |
41
|
|
|
{ |
42
|
17 |
|
if (!in_array($colour, $this->getDefaultColours()) && |
43
|
3 |
|
!preg_match( |
44
|
3 |
|
'_^#([A-Fa-f0-9]{6}|[A-Fa-f0-9]{3})$_', |
45
|
|
|
$colour |
46
|
3 |
|
) |
47
|
17 |
|
) { |
48
|
1 |
|
throw new InvalidColourException( |
49
|
1 |
|
sprintf( |
50
|
1 |
|
'The colour "%s" is not a valid colour. Possible options are "%s" or a valid hex colour', |
51
|
1 |
|
$colour, |
52
|
1 |
|
implode('", "', $this->getDefaultColours()) |
53
|
1 |
|
) |
54
|
1 |
|
); |
55
|
|
|
} |
56
|
|
|
|
57
|
16 |
|
$this->colour = $colour; |
58
|
16 |
|
} |
59
|
|
|
|
60
|
|
|
/** |
61
|
|
|
* @return string |
62
|
|
|
*/ |
63
|
13 |
|
public function getColour() |
64
|
|
|
{ |
65
|
13 |
|
return $this->colour; |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
/** |
69
|
|
|
* {@inheritdoc} |
70
|
|
|
*/ |
71
|
12 |
|
public function __toString() |
72
|
|
|
{ |
73
|
12 |
|
return $this->getColour(); |
74
|
|
|
} |
75
|
|
|
/** |
76
|
|
|
* Get the possible default colours. |
77
|
|
|
* |
78
|
|
|
* @return array |
79
|
|
|
*/ |
80
|
17 |
|
private function getDefaultColours() |
81
|
|
|
{ |
82
|
|
|
return [ |
83
|
17 |
|
self::COLOUR_GOOD, |
84
|
17 |
|
self::COLOUR_WARNING, |
85
|
17 |
|
self::COLOUR_DANGER, |
86
|
17 |
|
]; |
87
|
|
|
} |
88
|
|
|
} |
89
|
|
|
|