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