1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace NotificationChannels\WhatsApp; |
4
|
|
|
|
5
|
|
|
use Netflie\WhatsAppCloudApi\Message\Template\Component as CloudApiComponent; |
6
|
|
|
use NotificationChannels\WhatsApp\Component\Button; |
7
|
|
|
use NotificationChannels\WhatsApp\Component\Component; |
|
|
|
|
8
|
|
|
|
9
|
|
|
class WhatsAppTemplate |
10
|
|
|
{ |
11
|
|
|
/** |
12
|
|
|
* WhatsApp ID or phone number for the person you want to send a message to. |
13
|
|
|
*/ |
14
|
|
|
protected string $to; |
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* Name of the template. |
18
|
|
|
* |
19
|
|
|
* @link https://business.facebook.com/wa/manage/message-templates/ Dashboard to manage (create, edit and delete) templates. |
20
|
|
|
*/ |
21
|
|
|
protected string $name; |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* @link https://developers.facebook.com/docs/whatsapp/api/messages/message-templates#supported-languages See supported language codes. |
25
|
|
|
*/ |
26
|
|
|
protected string $language; |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* Template header, body and buttons can be personalized with custom variable values. |
30
|
|
|
* |
31
|
|
|
* @link https://developers.facebook.com/docs/whatsapp/cloud-api/guides/send-message-templates See how you can personalized your templates. |
32
|
|
|
*/ |
33
|
|
|
protected array $components; |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* The message type. |
37
|
|
|
*/ |
38
|
|
|
protected static string $type = 'template'; |
39
|
|
|
|
40
|
10 |
|
protected function __construct($to = '', $name = '', $language = 'en_US') |
41
|
|
|
{ |
42
|
10 |
|
$this->to = $to; |
43
|
10 |
|
$this->name = $name; |
44
|
10 |
|
$this->language = $language; |
45
|
10 |
|
$this->components = [ |
46
|
10 |
|
'header' => [], |
47
|
10 |
|
'body' => [], |
48
|
10 |
|
'buttons' => [], |
49
|
10 |
|
]; |
50
|
|
|
} |
51
|
|
|
|
52
|
10 |
|
public static function create($to = '', $name = '', $language = 'en_US'): self |
53
|
|
|
{ |
54
|
10 |
|
return new self($to, $name, $language); |
55
|
|
|
} |
56
|
|
|
|
57
|
4 |
|
public function to(string $to): self |
58
|
|
|
{ |
59
|
4 |
|
$this->to = $to; |
60
|
|
|
|
61
|
4 |
|
return $this; |
62
|
|
|
} |
63
|
|
|
|
64
|
5 |
|
public function name(string $name): self |
65
|
|
|
{ |
66
|
5 |
|
$this->name = $name; |
67
|
|
|
|
68
|
5 |
|
return $this; |
69
|
|
|
} |
70
|
|
|
|
71
|
1 |
|
public function language(string $language): self |
72
|
|
|
{ |
73
|
1 |
|
$this->language = $language; |
74
|
|
|
|
75
|
1 |
|
return $this; |
76
|
|
|
} |
77
|
|
|
|
78
|
1 |
|
public function header(Component $component): self |
79
|
|
|
{ |
80
|
1 |
|
$this->components['header'][] = $component->toArray(); |
81
|
|
|
|
82
|
1 |
|
return $this; |
83
|
|
|
} |
84
|
|
|
|
85
|
1 |
|
public function body(Component $component): self |
86
|
|
|
{ |
87
|
1 |
|
$this->components['body'][] = $component->toArray(); |
88
|
|
|
|
89
|
1 |
|
return $this; |
90
|
|
|
} |
91
|
|
|
|
92
|
4 |
|
public function recipient(): ?string |
93
|
|
|
{ |
94
|
4 |
|
return $this->to; |
95
|
|
|
} |
96
|
|
|
|
97
|
4 |
|
public function configuredName(): ?string |
98
|
|
|
{ |
99
|
4 |
|
return $this->name; |
100
|
|
|
} |
101
|
|
|
|
102
|
4 |
|
public function configuredLanguage(): string |
103
|
|
|
{ |
104
|
4 |
|
return $this->language; |
105
|
|
|
} |
106
|
|
|
|
107
|
6 |
|
public function components(): CloudApiComponent |
108
|
|
|
{ |
109
|
6 |
|
return new CloudApiComponent( |
110
|
6 |
|
$this->components['header'], |
111
|
6 |
|
$this->components['body'], |
112
|
6 |
|
$this->components['buttons'] |
113
|
6 |
|
); |
114
|
|
|
} |
115
|
|
|
|
116
|
1 |
|
public function buttons(Button $component): self |
117
|
|
|
{ |
118
|
1 |
|
$buttons = $this->components['buttons']; |
119
|
1 |
|
$component->setIndex(count($buttons)); |
120
|
1 |
|
$this->components['buttons'][] = $component->toArray(); |
121
|
|
|
|
122
|
1 |
|
return $this; |
123
|
|
|
} |
124
|
|
|
|
125
|
4 |
|
public function hasRecipient(): bool |
126
|
|
|
{ |
127
|
4 |
|
return ! empty($this->to); |
128
|
|
|
} |
129
|
|
|
|
130
|
3 |
|
public function type(): string |
131
|
|
|
{ |
132
|
3 |
|
return self::$type; |
133
|
|
|
} |
134
|
|
|
} |
135
|
|
|
|
Let?s assume that you have a directory layout like this:
and let?s assume the following content of
Bar.php
:If both files
OtherDir/Foo.php
andSomeDir/Foo.php
are loaded in the same runtime, you will see a PHP error such as the following:PHP Fatal error: Cannot use SomeDir\Foo as Foo because the name is already in use in OtherDir/Foo.php
However, as
OtherDir/Foo.php
does not necessarily have to be loaded and the error is only triggered if it is loaded beforeOtherDir/Bar.php
, this problem might go unnoticed for a while. In order to prevent this error from surfacing, you must import the namespace with a different alias: