-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathWhatsAppInteractiveContent.php
More file actions
74 lines (61 loc) · 2.03 KB
/
WhatsAppInteractiveContent.php
File metadata and controls
74 lines (61 loc) · 2.03 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
<?php
namespace CMText\RichContent\Messages\WhatsApp;
class WhatsAppInteractiveContent implements \JsonSerializable
{
public $type;
public $header;
public $body;
public $footer;
public $action;
/**
* @param string $type
* @param WhatsAppInteractiveHeader|null $header
* @param WhatsAppInteractiveBody|null $body
* @param IWhatsAppInteractiveAction|null $action
* @param WhatsAppInteractiveFooter|null $footer
* @throws \Exception
*/
public function __construct(
string $type,
?WhatsAppInteractiveHeader $header = null,
?WhatsAppInteractiveBody $body = null,
?IWhatsAppInteractiveAction $action = null,
?WhatsAppInteractiveFooter $footer = null
)
{
if( !in_array(
$type,
(new \ReflectionClass(WhatsAppInteractiveContentTypes::class))->getConstants())
){
throw new \Exception("Unsupported WhatsApp-InteractiveContent-type $type");
}
// action is always required.
if($action == null){
throw new \Exception("Action is always Required.");
}
// header is required for type Product-list
if($type == WhatsAppInteractiveContentTypes::PRODUCT_LIST && $header == null){
throw new \Exception("Header is required for type $type");
}
// body is optional for type Product, otherwise required.
if($type != WhatsAppInteractiveContentTypes::PRODUCT && $body == null){
throw new \Exception("Body is required for type $type");
}
$this->type = $type;
$this->header = $header;
$this->body = $body;
$this->footer = $footer;
$this->action = $action;
}
#[\ReturnTypeWillChange]
public function jsonSerialize()
{
return (object)array_filter([
'type' => $this->type,
'header' => $this->header,
'body' => $this->body,
'footer' => $this->footer,
'action' => $this->action
]);
}
}