1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace NotificationChannels\Facebook\Components; |
4
|
|
|
|
5
|
|
|
use NotificationChannels\Facebook\Exceptions\CouldNotCreateCard; |
6
|
|
|
use NotificationChannels\Facebook\Exceptions\CouldNotCreateMessage; |
7
|
|
|
use NotificationChannels\Facebook\Exceptions\CouldNotSendNotification; |
8
|
|
|
|
9
|
|
|
|
10
|
|
|
class Card implements \JsonSerializable |
11
|
|
|
{ |
12
|
|
|
/** @var string Card Title */ |
13
|
|
|
protected $title; |
14
|
|
|
|
15
|
|
|
/** @var string Item Url */ |
16
|
|
|
protected $image_url; |
17
|
|
|
|
18
|
|
|
/** @var string Image Url */ |
19
|
|
|
protected $item_url; |
20
|
|
|
|
21
|
|
|
/** @var string Subtitle */ |
22
|
|
|
protected $subtitle; |
23
|
|
|
|
24
|
|
|
/** @var array Call to Action Buttons */ |
25
|
|
|
public $buttons = []; |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* Create a Card |
29
|
|
|
* |
30
|
|
|
* @return static |
31
|
|
|
*/ |
32
|
|
|
public static function create($title = '') |
33
|
|
|
{ |
34
|
|
|
return new static($title); |
35
|
|
|
} |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* Create Card constructor |
39
|
|
|
*/ |
40
|
|
|
public function __construct($title = '') |
41
|
|
|
{ |
42
|
|
|
$this->title($title); |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* Set Button Title. |
47
|
|
|
* |
48
|
|
|
* @param $title |
49
|
|
|
* @throws CouldNotCreateCard |
50
|
|
|
* @return $this |
51
|
|
|
*/ |
52
|
|
|
public function title($title) |
53
|
|
|
{ |
54
|
|
|
if (mb_strlen($title) > 80) { |
55
|
|
|
throw CouldNotCreateCard::titleLimitExceeded($this->title); |
56
|
|
|
} |
57
|
|
|
$this->title = $title; |
58
|
|
|
return $this; |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
/** |
62
|
|
|
* Add up to 3 call to action buttons. |
63
|
|
|
* |
64
|
|
|
* @param array $buttons |
65
|
|
|
* |
66
|
|
|
* @return $this |
67
|
|
|
* @throws CouldNotSendNotification |
68
|
|
|
*/ |
69
|
|
View Code Duplication |
public function buttons(array $buttons = []) |
|
|
|
|
70
|
|
|
{ |
71
|
|
|
if (count($buttons) > 3) { |
72
|
|
|
throw CouldNotCreateMessage::messageButtonsLimitExceeded(); |
73
|
|
|
} |
74
|
|
|
$this->buttons = $buttons; |
75
|
|
|
return $this; |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
/** |
79
|
|
|
* Set Card Item Url |
80
|
|
|
* |
81
|
|
|
* @param $item_url |
82
|
|
|
* @return $this |
83
|
|
|
*/ |
84
|
|
|
public function url($item_url) |
85
|
|
|
{ |
86
|
|
|
$this->item_url = $item_url; |
87
|
|
|
return $this; |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
/** |
91
|
|
|
* Set Card Image Url |
92
|
|
|
* |
93
|
|
|
* @param $image_url |
94
|
|
|
* Image ration should be 1.91:1 |
95
|
|
|
* @return $this |
96
|
|
|
*/ |
97
|
|
|
public function image($image_url) |
98
|
|
|
{ |
99
|
|
|
$this->image_url = $image_url; |
100
|
|
|
return $this; |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
/** |
104
|
|
|
* Set Card Subtitle |
105
|
|
|
* |
106
|
|
|
* @param $subtitle |
107
|
|
|
* @throws CouldNotCreateCard |
108
|
|
|
* @return $this |
109
|
|
|
*/ |
110
|
|
|
public function subtitle($subtitle) |
111
|
|
|
{ |
112
|
|
|
if (mb_strlen($subtitle) > 80) |
113
|
|
|
throw CouldNotCreateCard::subtitleLimitExceeded($this->title); |
114
|
|
|
$this->subtitle = $subtitle; |
115
|
|
|
return $this; |
116
|
|
|
} |
117
|
|
|
|
118
|
|
|
|
119
|
|
|
/** |
120
|
|
|
* Builds payload and returns an array |
121
|
|
|
* |
122
|
|
|
* @return array |
123
|
|
|
* @throws CouldNotCreateCard |
124
|
|
|
*/ |
125
|
|
|
public function toArray() |
126
|
|
|
{ |
127
|
|
|
$payload = []; |
128
|
|
|
|
129
|
|
|
if (!isset($this->title)) |
130
|
|
|
throw CouldNotCreateCard::titleNotProvided(); |
131
|
|
|
$payload['title'] = $this->title; |
132
|
|
|
|
133
|
|
|
if (isset($this->item_url)) |
134
|
|
|
$payload['item_url'] = $this->item_url; |
135
|
|
|
|
136
|
|
|
if (isset($this->image_url)) |
137
|
|
|
$payload['image_url'] = $this->image_url; |
138
|
|
|
|
139
|
|
|
if (isset($this->subtitle)) |
140
|
|
|
$payload['subtitle'] = $this->subtitle; |
141
|
|
|
|
142
|
|
|
if (count($this->buttons) > 0) |
143
|
|
|
$payload['buttons'] = $this->buttons; |
144
|
|
|
|
145
|
|
|
return $payload; |
146
|
|
|
} |
147
|
|
|
|
148
|
|
|
/** |
149
|
|
|
* Convert the object into something JSON serializable. |
150
|
|
|
* |
151
|
|
|
* @return array |
152
|
|
|
*/ |
153
|
|
|
public function jsonSerialize() |
154
|
|
|
{ |
155
|
|
|
return $this->toArray(); |
156
|
|
|
} |
157
|
|
|
} |
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.