Skip to content

Commit dd9a9ac

Browse files
committed
create whatsapp template message
1 parent e979549 commit dd9a9ac

File tree

3 files changed

+184
-10
lines changed

3 files changed

+184
-10
lines changed

src/WhatsAppMessage.php

Lines changed: 0 additions & 10 deletions
This file was deleted.

src/WhatsAppTemplate.php

Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
<?php
2+
3+
namespace NotificationChannels\WhatsApp;
4+
5+
use Netflie\WhatsAppCloudApi\Message\Template\Component as CloudApiComponent;
6+
use NotificationChannels\WhatsApp\Component\Component;
7+
8+
class WhatsAppTemplate
9+
{
10+
private string $to;
11+
12+
private string $name;
13+
14+
private string $language;
15+
16+
private array $components;
17+
18+
private function __construct($to = '', $name = '', $language = 'en_US')
19+
{
20+
$this->to = $to;
21+
$this->name = $name;
22+
$this->language = $language;
23+
$this->components = [
24+
'header' => [],
25+
'body' => [],
26+
];
27+
}
28+
29+
public static function create($to = '', $name = '', $language = 'en_US'): self
30+
{
31+
return new self($to, $name, $language);
32+
}
33+
34+
public function to(string $to): self
35+
{
36+
$this->to = $to;
37+
38+
return $this;
39+
}
40+
41+
public function name(string $name): self
42+
{
43+
$this->name = $name;
44+
45+
return $this;
46+
}
47+
48+
public function language(string $language): self
49+
{
50+
$this->language = $language;
51+
52+
return $this;
53+
}
54+
55+
public function header(Component $component): self
56+
{
57+
$this->components['header'][] = $component->toArray();
58+
59+
return $this;
60+
}
61+
62+
public function body(Component $component): self
63+
{
64+
$this->components['body'][] = $component->toArray();
65+
66+
return $this;
67+
}
68+
69+
public function recipient(): ?string
70+
{
71+
return $this->to;
72+
}
73+
74+
public function configuredName(): ?string
75+
{
76+
return $this->name;
77+
}
78+
79+
public function configuredLanguage(): string
80+
{
81+
return $this->language;
82+
}
83+
84+
public function components(): CloudApiComponent
85+
{
86+
return new CloudApiComponent(
87+
$this->components['header'],
88+
$this->components['body']
89+
);
90+
}
91+
92+
public function hasRecipient(): bool
93+
{
94+
return !empty($this->to);
95+
}
96+
}

tests/WhatsAppTemplateTest.php

Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
<?php
2+
3+
namespace NotificationChannels\WhatsApp\Test;
4+
5+
use NotificationChannels\WhatsApp\Component\Currency;
6+
use NotificationChannels\WhatsApp\Component\Document;
7+
use NotificationChannels\WhatsApp\Component\Image;
8+
use NotificationChannels\WhatsApp\Component\Text;
9+
use NotificationChannels\WhatsApp\Component\Video;
10+
use NotificationChannels\WhatsApp\WhatsAppTemplate;
11+
use PHPUnit\Framework\TestCase;
12+
13+
final class WhatsAppTemplateTest extends TestCase
14+
{
15+
/** @test */
16+
public function the_notification_recipient_can_be_set()
17+
{
18+
$message = WhatsAppTemplate::create()
19+
->to('346762014584');
20+
21+
$this->assertEquals('346762014584', $message->recipient());
22+
}
23+
24+
/** @test */
25+
public function the_notification_name_can_be_set()
26+
{
27+
$message = WhatsAppTemplate::create()
28+
->name('invoice_created');
29+
30+
$this->assertEquals('invoice_created', $message->configuredName());
31+
}
32+
33+
/** @test */
34+
public function the_notification_language_can_be_set()
35+
{
36+
$message = WhatsAppTemplate::create()
37+
->language('es_fake');
38+
39+
$this->assertEquals('es_fake', $message->configuredLanguage());
40+
}
41+
42+
/** @test */
43+
public function the_notification_component_header_can_be_set()
44+
{
45+
$message = WhatsAppTemplate::create()
46+
->header(new Currency(10, 'USD'))
47+
->header(new Document('https://netflie.es/document.pdf'))
48+
->header(new Video('https://netflie.es/video.webm'));
49+
50+
$expectedHeader = [
51+
[
52+
'type' => 'currency',
53+
'currency' => ['amount_1000' => 10000, 'code' => 'USD'],
54+
],
55+
[
56+
'type' => 'document',
57+
'document' => ['link' => 'https://netflie.es/document.pdf'],
58+
],
59+
[
60+
'type' => 'video',
61+
'video' => ['link' => 'https://netflie.es/video.webm'],
62+
],
63+
];
64+
65+
$this->assertEquals($expectedHeader, $message->components()->header());
66+
}
67+
68+
/** @test */
69+
public function the_notification_component_body_can_be_set()
70+
{
71+
$message = WhatsAppTemplate::create()
72+
->body(new Text('Mr Jones'))
73+
->body(new Image('https://netflie.es/image.png'));
74+
75+
$expectedHeader = [
76+
[
77+
'type' => 'text',
78+
'text' => 'Mr Jones',
79+
],
80+
[
81+
'type' => 'image',
82+
'image' => ['link' => 'https://netflie.es/image.png'],
83+
],
84+
];
85+
86+
$this->assertEquals($expectedHeader, $message->components()->body());
87+
}
88+
}

0 commit comments

Comments
 (0)