1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace Kerox\Messenger\Model\Message; |
6
|
|
|
|
7
|
|
|
use Kerox\Messenger\Exception\InvalidTypeException; |
8
|
|
|
use Kerox\Messenger\Helper\ValidatorTrait; |
9
|
|
|
|
10
|
|
|
class QuickReply implements \JsonSerializable |
11
|
|
|
{ |
12
|
|
|
use ValidatorTrait; |
13
|
|
|
|
14
|
|
|
public const CONTENT_TYPE_TEXT = 'text'; |
15
|
|
|
public const CONTENT_TYPE_PHONE = 'user_phone_number'; |
16
|
|
|
public const CONTENT_TYPE_EMAIL = 'user_email'; |
17
|
|
|
|
18
|
|
|
/** @deprecated Since version 3.2.0 and will be removed in version 4.0.0. */ |
19
|
|
|
public const CONTENT_TYPE_LOCATION = 'location'; |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* @var string |
23
|
|
|
*/ |
24
|
|
|
protected $contentType; |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* @var string|null |
28
|
|
|
*/ |
29
|
|
|
protected $title; |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* @var string|null |
33
|
|
|
*/ |
34
|
|
|
protected $payload; |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* @var string|null |
38
|
|
|
*/ |
39
|
|
|
protected $imageUrl; |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* QuickReply constructor. |
43
|
|
|
* |
44
|
|
|
* @throws \Exception |
45
|
|
|
*/ |
46
|
6 |
|
public function __construct(string $contentType = self::CONTENT_TYPE_TEXT) |
47
|
|
|
{ |
48
|
6 |
|
$this->isValidContentType($contentType); |
49
|
|
|
|
50
|
5 |
|
$this->contentType = $contentType; |
51
|
5 |
|
} |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* @throws \Exception |
55
|
|
|
* |
56
|
|
|
* @return \Kerox\Messenger\Model\Message\QuickReply |
57
|
|
|
*/ |
58
|
6 |
|
public static function create(string $contentType = self::CONTENT_TYPE_TEXT): self |
59
|
|
|
{ |
60
|
6 |
|
return new self($contentType); |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
/** |
64
|
|
|
* @throws \Exception |
65
|
|
|
* |
66
|
|
|
* @return \Kerox\Messenger\Model\Message\QuickReply |
67
|
|
|
*/ |
68
|
5 |
|
public function setTitle(string $title): self |
69
|
|
|
{ |
70
|
5 |
|
$this->checkContentType(); |
71
|
4 |
|
$this->isValidString($title, 20); |
72
|
|
|
|
73
|
4 |
|
$this->title = $title; |
74
|
|
|
|
75
|
4 |
|
return $this; |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
/** |
79
|
|
|
* @throws \Exception |
80
|
|
|
* |
81
|
|
|
* @return \Kerox\Messenger\Model\Message\QuickReply |
82
|
|
|
*/ |
83
|
4 |
|
public function setPayload(string $payload): self |
84
|
|
|
{ |
85
|
4 |
|
$this->checkContentType(); |
86
|
4 |
|
$this->isValidString($payload, 1000); |
87
|
|
|
|
88
|
4 |
|
$this->payload = $payload; |
89
|
|
|
|
90
|
4 |
|
return $this; |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
/** |
94
|
|
|
* @throws \Exception |
95
|
|
|
* |
96
|
|
|
* @return \Kerox\Messenger\Model\Message\QuickReply |
97
|
|
|
*/ |
98
|
4 |
|
public function setImageUrl(string $imageUrl): self |
99
|
|
|
{ |
100
|
4 |
|
$this->checkContentType(); |
101
|
4 |
|
$this->isValidUrl($imageUrl); |
102
|
4 |
|
$this->imageUrl = $imageUrl; |
103
|
|
|
|
104
|
4 |
|
return $this; |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
/** |
108
|
|
|
* @throws \Kerox\Messenger\Exception\InvalidTypeException |
109
|
|
|
*/ |
110
|
6 |
|
private function isValidContentType(string $contentType): void |
111
|
|
|
{ |
112
|
6 |
|
$allowedContentType = $this->getAllowedContentType(); |
113
|
6 |
|
if (!\in_array($contentType, $allowedContentType, true)) { |
114
|
1 |
|
throw new InvalidTypeException('Invalid content type.'); |
115
|
|
|
} |
116
|
5 |
|
} |
117
|
|
|
|
118
|
6 |
|
private function getAllowedContentType(): array |
119
|
|
|
{ |
120
|
|
|
return [ |
121
|
6 |
|
self::CONTENT_TYPE_TEXT, |
122
|
6 |
|
self::CONTENT_TYPE_LOCATION, |
|
|
|
|
123
|
6 |
|
self::CONTENT_TYPE_PHONE, |
124
|
6 |
|
self::CONTENT_TYPE_EMAIL, |
125
|
|
|
]; |
126
|
|
|
} |
127
|
|
|
|
128
|
|
|
/** |
129
|
|
|
* @throws \Kerox\Messenger\Exception\InvalidTypeException |
130
|
|
|
*/ |
131
|
5 |
|
private function checkContentType(): void |
132
|
|
|
{ |
133
|
5 |
|
if ($this->contentType !== self::CONTENT_TYPE_TEXT) { |
134
|
1 |
|
throw new InvalidTypeException('Content type must be set to text to use title, payload and image_url.'); |
135
|
|
|
} |
136
|
4 |
|
} |
137
|
|
|
|
138
|
2 |
|
public function toArray(): array |
139
|
|
|
{ |
140
|
|
|
$quickReply = [ |
141
|
2 |
|
'content_type' => $this->contentType, |
142
|
2 |
|
'title' => $this->title, |
143
|
2 |
|
'payload' => $this->payload, |
144
|
2 |
|
'image_url' => $this->imageUrl, |
145
|
|
|
]; |
146
|
|
|
|
147
|
2 |
|
return array_filter($quickReply); |
148
|
|
|
} |
149
|
|
|
|
150
|
2 |
|
public function jsonSerialize(): array |
151
|
|
|
{ |
152
|
2 |
|
return $this->toArray(); |
153
|
|
|
} |
154
|
|
|
} |
155
|
|
|
|
This class constant has been deprecated. The supplier of the class has supplied an explanatory message.
The explanatory message should give you some clue as to whether and when the constant will be removed from the class and what other constant to use instead.