1 | <?php |
||
14 | class EmojiIcon extends SerializeToString implements IconInterface |
||
15 | { |
||
16 | /** |
||
17 | * @var string |
||
18 | */ |
||
19 | private $emoji; |
||
20 | |||
21 | /** |
||
22 | * UrlIcon constructor. |
||
23 | * |
||
24 | * @param string $emoji |
||
25 | */ |
||
26 | 8 | public function __construct($emoji) |
|
27 | { |
||
28 | 8 | $this->setEmoji($emoji); |
|
29 | 7 | } |
|
30 | |||
31 | /** |
||
32 | * This will set the emoji if it is valid. |
||
33 | * |
||
34 | * @param string $emoji |
||
35 | * |
||
36 | * @throws InvalidEmojiException Thrown when the emoji is not valid |
||
37 | * |
||
38 | * @return $this |
||
39 | */ |
||
40 | 8 | private function setEmoji($emoji) |
|
41 | { |
||
42 | // remove the whitespace |
||
43 | 8 | $emoji = trim($emoji); |
|
44 | |||
45 | 8 | $emojiValidationRegex = '_^:[\w-+]+:$_iuS'; |
|
46 | 8 | if (!preg_match($emojiValidationRegex, $emoji)) { |
|
47 | 1 | throw new InvalidEmojiException( |
|
48 | 1 | sprintf( |
|
49 | 'The emoji: "%s" is not a valid emoji. |
||
50 | 1 | An emoji should always be a string starting and ending with ":".', |
|
51 | $emoji |
||
52 | 1 | ), |
|
53 | 400 |
||
54 | 1 | ); |
|
55 | } |
||
56 | 7 | $this->emoji = $emoji; |
|
57 | |||
58 | 7 | return $this; |
|
59 | } |
||
60 | |||
61 | /** |
||
62 | * {@inheritdoc} |
||
63 | */ |
||
64 | 5 | public function getIcon() |
|
65 | { |
||
66 | 5 | return $this->emoji; |
|
67 | } |
||
68 | |||
69 | /** |
||
70 | * {@inheritdoc} |
||
71 | */ |
||
72 | 4 | public function __toString() |
|
73 | { |
||
74 | 4 | return $this->getIcon(); |
|
75 | } |
||
76 | |||
77 | /** |
||
78 | * {@inheritdoc} |
||
79 | */ |
||
80 | 1 | public function getType() |
|
84 | } |
||
85 |