|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Tgallice\FBMessenger\Model; |
|
4
|
|
|
|
|
5
|
|
|
class Button implements \JsonSerializable |
|
6
|
|
|
{ |
|
7
|
|
|
const TYPE_POSTBACK = 'postback'; |
|
8
|
|
|
const TYPE_PHONE_NUMBER = 'phone_number'; |
|
9
|
|
|
const TYPE_WEB_URL = 'web_url'; |
|
10
|
|
|
|
|
11
|
|
|
/** |
|
12
|
|
|
* @var string |
|
13
|
|
|
*/ |
|
14
|
|
|
private $title; |
|
15
|
|
|
|
|
16
|
|
|
/** |
|
17
|
|
|
* @var string |
|
18
|
|
|
*/ |
|
19
|
|
|
private $type; |
|
20
|
|
|
|
|
21
|
|
|
/** |
|
22
|
|
|
* Url or payload |
|
23
|
|
|
* |
|
24
|
|
|
* @var string |
|
25
|
|
|
*/ |
|
26
|
|
|
protected $data; |
|
27
|
|
|
|
|
28
|
|
|
/** |
|
29
|
|
|
* @param string $type |
|
30
|
|
|
* @param string $title |
|
31
|
|
|
* @param string $data Url, phone number or payload value |
|
32
|
|
|
*/ |
|
33
|
16 |
|
public function __construct($type, $title, $data) |
|
34
|
|
|
{ |
|
35
|
16 |
|
$this->type = $type; |
|
36
|
|
|
|
|
37
|
16 |
|
$this->validateTitleSize($title); |
|
38
|
15 |
|
$this->title = $title; |
|
39
|
|
|
|
|
40
|
15 |
|
if ($this->type === self::TYPE_POSTBACK) { |
|
41
|
4 |
|
$this->validatePayload($data); |
|
42
|
14 |
|
} elseif ($this->type === self::TYPE_PHONE_NUMBER) { |
|
43
|
3 |
|
$this->validatePhoneNumber($data); |
|
44
|
2 |
|
} |
|
45
|
|
|
|
|
46
|
13 |
|
$this->data = $data; |
|
47
|
13 |
|
} |
|
48
|
|
|
|
|
49
|
|
|
/** |
|
50
|
|
|
* @return string |
|
51
|
|
|
*/ |
|
52
|
1 |
|
public function getType() |
|
53
|
|
|
{ |
|
54
|
1 |
|
return $this->type; |
|
55
|
|
|
} |
|
56
|
|
|
|
|
57
|
|
|
/** |
|
58
|
|
|
* @return string |
|
59
|
|
|
*/ |
|
60
|
1 |
|
public function getTitle() |
|
61
|
|
|
{ |
|
62
|
1 |
|
return $this->title; |
|
63
|
|
|
} |
|
64
|
|
|
|
|
65
|
|
|
/** |
|
66
|
|
|
* @return string |
|
67
|
|
|
*/ |
|
68
|
1 |
|
public function getData() |
|
69
|
|
|
{ |
|
70
|
1 |
|
return $this->data; |
|
71
|
|
|
} |
|
72
|
|
|
|
|
73
|
|
|
/** |
|
74
|
|
|
* @inheritdoc |
|
75
|
|
|
*/ |
|
76
|
2 |
View Code Duplication |
public function jsonSerialize() |
|
77
|
|
|
{ |
|
78
|
|
|
$data = [ |
|
79
|
2 |
|
'type' => $this->type, |
|
80
|
2 |
|
'title' => $this->title, |
|
81
|
2 |
|
]; |
|
82
|
|
|
|
|
83
|
2 |
|
if ($this->type === self::TYPE_WEB_URL) { |
|
84
|
1 |
|
$data['url'] = $this->data; |
|
85
|
1 |
|
} else { |
|
86
|
1 |
|
$data['payload'] = $this->data; |
|
87
|
|
|
} |
|
88
|
|
|
|
|
89
|
2 |
|
return $data; |
|
90
|
|
|
} |
|
91
|
|
|
|
|
92
|
|
|
/** |
|
93
|
|
|
* @param string $title |
|
94
|
|
|
* |
|
95
|
|
|
* @throws \InvalidArgumentException |
|
96
|
|
|
*/ |
|
97
|
16 |
|
private function validateTitleSize($title) |
|
98
|
|
|
{ |
|
99
|
16 |
|
if (mb_strlen($title) > 20) { |
|
100
|
1 |
|
throw new \InvalidArgumentException('The button title field should not exceed 20 characters.'); |
|
101
|
|
|
} |
|
102
|
15 |
|
} |
|
103
|
|
|
|
|
104
|
|
|
/** |
|
105
|
|
|
* @param $payload |
|
106
|
|
|
* |
|
107
|
|
|
* @throws \InvalidArgumentException |
|
108
|
|
|
*/ |
|
109
|
4 |
|
private function validatePayload($payload) |
|
110
|
|
|
{ |
|
111
|
4 |
|
if (mb_strlen($payload) > 1000) { |
|
112
|
1 |
|
throw new \InvalidArgumentException(sprintf( |
|
113
|
1 |
|
'Payload should not exceed 1000 characters.', $payload) |
|
114
|
1 |
|
); |
|
115
|
|
|
} |
|
116
|
3 |
|
} |
|
117
|
|
|
|
|
118
|
|
|
/** |
|
119
|
|
|
* @param $phoneNumber |
|
120
|
|
|
* |
|
121
|
|
|
* @throws \InvalidArgumentException |
|
122
|
|
|
*/ |
|
123
|
3 |
|
private function validatePhoneNumber($phoneNumber) |
|
124
|
|
|
{ |
|
125
|
|
|
// Dummy phone number check |
|
126
|
3 |
|
if (strpos($phoneNumber, '+') !== 0) { |
|
127
|
1 |
|
throw new \InvalidArgumentException(sprintf( |
|
128
|
1 |
|
'The phone number "%s" seem to be invalid. Please check the documentation to format the phone number.', $phoneNumber) |
|
129
|
1 |
|
); |
|
130
|
|
|
} |
|
131
|
2 |
|
} |
|
132
|
|
|
} |
|
133
|
|
|
|