|
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
|
|
|
const TYPE_SHARE = 'element_share'; |
|
11
|
|
|
|
|
12
|
|
|
/** |
|
13
|
|
|
* @var string |
|
14
|
|
|
*/ |
|
15
|
|
|
private $title; |
|
16
|
|
|
|
|
17
|
|
|
/** |
|
18
|
|
|
* @var string |
|
19
|
|
|
*/ |
|
20
|
|
|
private $type; |
|
21
|
|
|
|
|
22
|
|
|
/** |
|
23
|
|
|
* Url or payload |
|
24
|
|
|
* |
|
25
|
|
|
* @var string |
|
26
|
|
|
*/ |
|
27
|
|
|
protected $data; |
|
28
|
|
|
|
|
29
|
|
|
/** |
|
30
|
|
|
* @param string $type |
|
31
|
|
|
* @param string|null $title |
|
32
|
|
|
* @param string|null $data Url, phone number or payload value |
|
33
|
|
|
*/ |
|
34
|
30 |
|
public function __construct($type, $title = null, $data = null) |
|
35
|
|
|
{ |
|
36
|
30 |
|
$this->type = $type; |
|
37
|
|
|
|
|
38
|
30 |
|
self::validateTitleSize($title); |
|
39
|
29 |
|
$this->title = $title; |
|
40
|
|
|
|
|
41
|
29 |
|
if ($this->type === self::TYPE_POSTBACK) { |
|
42
|
5 |
|
self::validatePayload($data); |
|
43
|
28 |
|
} elseif ($this->type === self::TYPE_PHONE_NUMBER) { |
|
44
|
4 |
|
self::validatePhoneNumber($data); |
|
45
|
3 |
|
} |
|
46
|
|
|
|
|
47
|
27 |
|
$this->data = $data; |
|
48
|
27 |
|
} |
|
49
|
|
|
|
|
50
|
|
|
/** |
|
51
|
|
|
* @return string |
|
52
|
|
|
*/ |
|
53
|
1 |
|
public function getType() |
|
54
|
|
|
{ |
|
55
|
1 |
|
return $this->type; |
|
56
|
|
|
} |
|
57
|
|
|
|
|
58
|
|
|
/** |
|
59
|
|
|
* @return string|null |
|
60
|
|
|
*/ |
|
61
|
1 |
|
public function getTitle() |
|
62
|
|
|
{ |
|
63
|
1 |
|
return $this->title; |
|
64
|
|
|
} |
|
65
|
|
|
|
|
66
|
|
|
/** |
|
67
|
|
|
* @return string|null |
|
68
|
|
|
*/ |
|
69
|
1 |
|
public function getData() |
|
70
|
|
|
{ |
|
71
|
1 |
|
return $this->data; |
|
72
|
|
|
} |
|
73
|
|
|
|
|
74
|
|
|
/** |
|
75
|
|
|
* @inheritdoc |
|
76
|
|
|
*/ |
|
77
|
7 |
|
public function jsonSerialize() |
|
78
|
|
|
{ |
|
79
|
|
|
$data = [ |
|
80
|
7 |
|
'type' => $this->type, |
|
81
|
7 |
|
]; |
|
82
|
|
|
|
|
83
|
7 |
|
if (null !== $this->title) { |
|
84
|
6 |
|
$data['title'] = $this->title; |
|
85
|
6 |
|
} |
|
86
|
|
|
|
|
87
|
7 |
|
if (null === $this->data) { |
|
88
|
1 |
|
return $data; |
|
89
|
|
|
} |
|
90
|
|
|
|
|
91
|
6 |
View Code Duplication |
if ($this->type === self::TYPE_WEB_URL) { |
|
|
|
|
|
|
92
|
3 |
|
$data['url'] = $this->data; |
|
93
|
3 |
|
} else { |
|
94
|
3 |
|
$data['payload'] = $this->data; |
|
95
|
|
|
} |
|
96
|
|
|
|
|
97
|
6 |
|
return $data; |
|
98
|
|
|
} |
|
99
|
|
|
|
|
100
|
|
|
/** |
|
101
|
|
|
* @param string $title |
|
102
|
|
|
* |
|
103
|
|
|
* @throws \InvalidArgumentException |
|
104
|
|
|
*/ |
|
105
|
30 |
|
public static function validateTitleSize($title) |
|
106
|
|
|
{ |
|
107
|
30 |
|
if (mb_strlen($title) > 20) { |
|
108
|
1 |
|
throw new \InvalidArgumentException('The button title field should not exceed 20 characters.'); |
|
109
|
|
|
} |
|
110
|
29 |
|
} |
|
111
|
|
|
|
|
112
|
|
|
/** |
|
113
|
|
|
* @param $payload |
|
114
|
|
|
* |
|
115
|
|
|
* @throws \InvalidArgumentException |
|
116
|
|
|
*/ |
|
117
|
5 |
|
public static function validatePayload($payload) |
|
118
|
|
|
{ |
|
119
|
5 |
|
if (mb_strlen($payload) > 1000) { |
|
120
|
1 |
|
throw new \InvalidArgumentException(sprintf( |
|
121
|
1 |
|
'Payload should not exceed 1000 characters.', $payload) |
|
122
|
1 |
|
); |
|
123
|
|
|
} |
|
124
|
4 |
|
} |
|
125
|
|
|
|
|
126
|
|
|
/** |
|
127
|
|
|
* @param $phoneNumber |
|
128
|
|
|
* |
|
129
|
|
|
* @throws \InvalidArgumentException |
|
130
|
|
|
*/ |
|
131
|
4 |
|
public static function validatePhoneNumber($phoneNumber) |
|
132
|
|
|
{ |
|
133
|
|
|
// Dummy phone number check |
|
134
|
4 |
|
if (strpos($phoneNumber, '+') !== 0) { |
|
135
|
1 |
|
throw new \InvalidArgumentException(sprintf( |
|
136
|
1 |
|
'The phone number "%s" seem to be invalid. Please check the documentation to format the phone number.', $phoneNumber) |
|
137
|
1 |
|
); |
|
138
|
|
|
} |
|
139
|
3 |
|
} |
|
140
|
|
|
} |
|
141
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.