1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Tgallice\FBMessenger\Model\Button; |
4
|
|
|
|
5
|
|
|
use Tgallice\FBMessenger\Model\Button; |
6
|
|
|
|
7
|
|
|
class WebUrl extends Button |
8
|
|
|
{ |
9
|
|
|
const HEIGHT_RATIO_FULL = 'full'; |
10
|
|
|
const HEIGHT_RATIO_COMPACT = 'compact'; |
11
|
|
|
const HEIGHT_RATIO_TALL = 'tall'; |
12
|
|
|
|
13
|
|
|
const WEBVIEW_SHARE_BUTTON_SHOW = 'show'; |
14
|
|
|
const WEBVIEW_SHARE_BUTTON_HIDE = 'hide'; |
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* @var string |
18
|
|
|
*/ |
19
|
|
|
private $title; |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* @var string |
23
|
|
|
*/ |
24
|
|
|
private $url; |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* @var string |
28
|
|
|
*/ |
29
|
|
|
private $webviewHeightRatio = self::HEIGHT_RATIO_FULL; |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* @var bool |
33
|
|
|
*/ |
34
|
|
|
private $messengerExtensions = false; |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* @var string|null |
38
|
|
|
*/ |
39
|
|
|
public $fallbackUrl; |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* @var string |
43
|
|
|
*/ |
44
|
|
|
private $webviewShareButton = self::WEBVIEW_SHARE_BUTTON_SHOW; |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* @param string $title |
48
|
|
|
* @param string $url |
49
|
|
|
*/ |
50
|
18 |
|
public function __construct($title, $url) |
51
|
|
|
{ |
52
|
18 |
|
parent::__construct(Button::TYPE_WEB_URL); |
53
|
|
|
|
54
|
18 |
|
self::validateTitleSize($title); |
55
|
17 |
|
$this->title = $title; |
56
|
17 |
|
$this->url = $url; |
57
|
17 |
|
} |
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* @return string |
61
|
|
|
*/ |
62
|
1 |
|
public function getTitle() |
63
|
|
|
{ |
64
|
1 |
|
return $this->title; |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
/** |
68
|
|
|
* @return string |
69
|
|
|
*/ |
70
|
1 |
|
public function getUrl() |
71
|
|
|
{ |
72
|
1 |
|
return $this->url; |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
/** |
76
|
|
|
* @param string $ratio |
77
|
|
|
*/ |
78
|
2 |
View Code Duplication |
public function setWebviewHeightRatio($ratio) |
79
|
|
|
{ |
80
|
2 |
|
if (!in_array($ratio, $this->getAllowedHeights())) { |
81
|
1 |
|
throw new \InvalidArgumentException(sprintf('Webview height ratio must be one of this values: [%s]', implode(', ', $this->getAllowedHeights()))); |
82
|
|
|
} |
83
|
|
|
|
84
|
1 |
|
$this->webviewHeightRatio = $ratio; |
85
|
1 |
|
} |
86
|
|
|
|
87
|
|
|
/** |
88
|
|
|
* @param bool $messengerExtensions |
89
|
|
|
*/ |
90
|
2 |
|
public function setMessengerExtensions($messengerExtensions) |
91
|
|
|
{ |
92
|
2 |
|
$this->messengerExtensions = $messengerExtensions; |
93
|
2 |
|
} |
94
|
|
|
|
95
|
|
|
/** |
96
|
|
|
* @param string $fallbackUrl |
97
|
|
|
*/ |
98
|
2 |
|
public function setFallbackUrl($fallbackUrl) |
99
|
|
|
{ |
100
|
2 |
|
$this->fallbackUrl = $fallbackUrl; |
101
|
2 |
|
} |
102
|
|
|
|
103
|
|
|
/** |
104
|
|
|
* @return string|null |
105
|
|
|
*/ |
106
|
2 |
|
public function getWebviewHeightRatio() |
107
|
|
|
{ |
108
|
2 |
|
return $this->webviewHeightRatio; |
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
/** |
112
|
|
|
* @return string|null |
113
|
|
|
*/ |
114
|
2 |
|
public function getFallbackUrl() |
115
|
|
|
{ |
116
|
2 |
|
return $this->fallbackUrl; |
117
|
|
|
} |
118
|
|
|
|
119
|
|
|
/** |
120
|
|
|
* @return bool |
121
|
|
|
*/ |
122
|
2 |
|
public function useMessengerExtensions() |
123
|
|
|
{ |
124
|
2 |
|
return $this->messengerExtensions; |
125
|
|
|
} |
126
|
|
|
|
127
|
|
|
/** |
128
|
|
|
* @param $value |
129
|
|
|
*/ |
130
|
1 |
View Code Duplication |
public function setWebviewShareButton($value) |
131
|
|
|
{ |
132
|
1 |
|
if (!in_array($value, $this->getAllowedWebviewShareButton())) { |
133
|
|
|
throw new \InvalidArgumentException(sprintf('Webview share button must be one of this values: [%s]', implode(', ', $this->getAllowedWebviewShareButton()))); |
134
|
|
|
} |
135
|
|
|
|
136
|
1 |
|
$this->webviewShareButton = $value; |
137
|
1 |
|
} |
138
|
|
|
|
139
|
|
|
/** |
140
|
|
|
* @return string|null |
141
|
|
|
*/ |
142
|
2 |
|
public function getWebviewShareButton() |
143
|
|
|
{ |
144
|
2 |
|
return $this->webviewShareButton; |
145
|
|
|
} |
146
|
|
|
|
147
|
|
|
/** |
148
|
|
|
* @inheritdoc |
149
|
|
|
*/ |
150
|
2 |
|
public function jsonSerialize() |
151
|
|
|
{ |
152
|
2 |
|
$json = parent::jsonSerialize(); |
153
|
|
|
|
154
|
2 |
|
$json['title'] = $this->title; |
155
|
2 |
|
$json['url'] = $this->url; |
156
|
|
|
|
157
|
2 |
|
if (!empty($this->webviewHeightRatio)) { |
158
|
2 |
|
$json['webview_height_ratio'] = $this->webviewHeightRatio; |
159
|
2 |
|
} |
160
|
|
|
|
161
|
2 |
|
if (!empty($this->webviewShareButton)) { |
162
|
2 |
|
$json['webview_share_button'] = $this->webviewShareButton; |
163
|
2 |
|
} |
164
|
|
|
|
165
|
2 |
|
if ($this->messengerExtensions) { |
166
|
1 |
|
$json['messenger_extensions'] = $this->messengerExtensions; |
167
|
1 |
|
} |
168
|
|
|
|
169
|
2 |
|
if ($this->messengerExtensions && !empty($this->fallbackUrl)) { |
170
|
1 |
|
$json['fallback_url'] = $this->fallbackUrl; |
171
|
1 |
|
} |
172
|
|
|
|
173
|
2 |
|
return $json; |
|
|
|
|
174
|
|
|
} |
175
|
|
|
|
176
|
|
|
/** |
177
|
|
|
* @return string[] |
178
|
|
|
*/ |
179
|
2 |
|
private function getAllowedHeights() |
180
|
|
|
{ |
181
|
|
|
return [ |
182
|
2 |
|
self::HEIGHT_RATIO_FULL, |
183
|
2 |
|
self::HEIGHT_RATIO_COMPACT, |
184
|
|
|
self::HEIGHT_RATIO_TALL |
185
|
2 |
|
]; |
186
|
|
|
} |
187
|
|
|
|
188
|
|
|
/** |
189
|
|
|
* @return string[] |
190
|
|
|
*/ |
191
|
1 |
|
private function getAllowedWebviewShareButton() |
192
|
|
|
{ |
193
|
|
|
return [ |
194
|
1 |
|
self::WEBVIEW_SHARE_BUTTON_SHOW, |
195
|
|
|
self::WEBVIEW_SHARE_BUTTON_HIDE |
196
|
1 |
|
]; |
197
|
|
|
} |
198
|
|
|
} |
199
|
|
|
|
If you return a value from a function or method, it should be a sub-type of the type that is given by the parent type f.e. an interface, or abstract method. This is more formally defined by the Lizkov substitution principle, and guarantees that classes that depend on the parent type can use any instance of a child type interchangably. This principle also belongs to the SOLID principles for object oriented design.
Let’s take a look at an example:
Our function
my_function
expects aPost
object, and outputs the author of the post. The base classPost
returns a simple string and outputting a simple string will work just fine. However, the child classBlogPost
which is a sub-type ofPost
instead decided to return anobject
, and is therefore violating the SOLID principles. If aBlogPost
were passed tomy_function
, PHP would not complain, but ultimately fail when executing thestrtoupper
call in its body.