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