Skip to content

Commit 1324971

Browse files
committed
add template components
1 parent dd9a9ac commit 1324971

14 files changed

+343
-9
lines changed

src/Component/Component.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
<?php
2+
3+
namespace NotificationChannels\WhatsApp\Component;
4+
5+
abstract class Component
6+
{
7+
abstract public function toArray(): array;
8+
}

src/Component/Currency.php

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
<?php
2+
3+
namespace NotificationChannels\WhatsApp\Component;
4+
5+
class Currency extends Component
6+
{
7+
protected float $amount;
8+
9+
/**
10+
* Currency code as defined in ISO 4217.
11+
*/
12+
protected string $code;
13+
14+
public function __construct(float $amount, string $code = 'EUR')
15+
{
16+
$this->amount = $amount;
17+
$this->code = $code;
18+
}
19+
20+
public function toArray(): array
21+
{
22+
return [
23+
'type' => 'currency',
24+
'currency' => [
25+
'code' => $this->code,
26+
'amount_1000' => (int) ($this->amount * 1000),
27+
],
28+
];
29+
}
30+
}

src/Component/DateTime.php

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
<?php
2+
3+
namespace NotificationChannels\WhatsApp\Component;
4+
5+
class DateTime extends Component
6+
{
7+
protected \DateTimeImmutable $dateTime;
8+
9+
protected string $format;
10+
11+
public function __construct(\DateTimeImmutable $dateTime, string $format = 'Y-m-d H:i:s')
12+
{
13+
$this->dateTime = $dateTime;
14+
$this->format = $format;
15+
}
16+
17+
public function toArray(): array
18+
{
19+
return [
20+
'type' => 'date_time',
21+
'date_time' => [
22+
'fallback_value' => $this->dateTime->format($this->format),
23+
],
24+
];
25+
}
26+
}

src/Component/Document.php

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
<?php
2+
3+
namespace NotificationChannels\WhatsApp\Component;
4+
5+
use NotificationChannels\WhatsApp\Exceptions\UnsupportedMediaValue;
6+
7+
class Document extends Component
8+
{
9+
protected const SUPPORTED_EXTENSIONS = ['pdf'];
10+
11+
/**
12+
* Link to the document; e.g. https://URL
13+
* Only PDF documents are supported
14+
*/
15+
protected string $link;
16+
17+
public function __construct(string $link)
18+
{
19+
if (filter_var($link, FILTER_VALIDATE_URL) === false) {
20+
throw new UnsupportedMediaValue($link, 'document', 'Link is not a valid URL');
21+
}
22+
23+
$extension = pathinfo($link, PATHINFO_EXTENSION);
24+
25+
if (!in_array($extension, static::SUPPORTED_EXTENSIONS)) {
26+
throw new UnsupportedMediaValue($link, 'document', 'Only PDF documents are supported.');
27+
}
28+
29+
$this->link = $link;
30+
}
31+
32+
public function toArray(): array
33+
{
34+
return [
35+
'type' => 'document',
36+
'document' => [
37+
'link' => $this->link,
38+
],
39+
];
40+
}
41+
}

src/Component/Image.php

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
<?php
2+
3+
namespace NotificationChannels\WhatsApp\Component;
4+
5+
class Image extends Component
6+
{
7+
/**
8+
* Link to the image; e.g. https://URL
9+
*/
10+
protected string $link;
11+
12+
public function __construct(string $link)
13+
{
14+
$this->link = $link;
15+
}
16+
17+
public function toArray(): array
18+
{
19+
return [
20+
'type' => 'image',
21+
'image' => [
22+
'link' => $this->link,
23+
],
24+
];
25+
}
26+
}

src/Component/Text.php

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
<?php
2+
3+
namespace NotificationChannels\WhatsApp\Component;
4+
5+
class Text extends Component
6+
{
7+
protected string $text;
8+
9+
public function __construct(string $text)
10+
{
11+
$this->text = $text;
12+
}
13+
14+
public function toArray(): array
15+
{
16+
return [
17+
'type' => 'text',
18+
'text' => $this->text,
19+
];
20+
}
21+
}

src/Component/Video.php

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
<?php
2+
3+
namespace NotificationChannels\WhatsApp\Component;
4+
5+
class Video extends Component
6+
{
7+
/**
8+
* Link to the video; e.g. https://URL
9+
*/
10+
protected string $link;
11+
12+
public function __construct(string $link)
13+
{
14+
$this->link = $link;
15+
}
16+
17+
public function toArray(): array
18+
{
19+
return [
20+
'type' => 'video',
21+
'video' => [
22+
'link' => $this->link,
23+
],
24+
];
25+
}
26+
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
<?php
2+
3+
namespace NotificationChannels\WhatsApp\Exceptions;
4+
5+
class UnsupportedMediaValue extends \Exception
6+
{
7+
public function __construct($value, string $mediaType, string $extendedMessage = '')
8+
{
9+
$message = "The $value value for $mediaType is unsupported.";
10+
$message .= $extendedMessage ? " Message: $extendedMessage" : '';
11+
12+
parent::__construct($message);
13+
}
14+
}

src/WhatsAppTemplate.php

Lines changed: 25 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -7,15 +7,31 @@
77

88
class WhatsAppTemplate
99
{
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')
10+
/**
11+
* WhatsApp ID or phone number for the person you want to send a message to.
12+
*
13+
*/
14+
protected string $to;
15+
16+
/**
17+
* Name of the template.
18+
* @link https://business.facebook.com/wa/manage/message-templates/ Dashboard to manage (create, edit and delete) templates.
19+
*/
20+
protected string $name;
21+
22+
/**
23+
* @link https://developers.facebook.com/docs/whatsapp/api/messages/message-templates#supported-languages See supported language codes.
24+
*/
25+
protected string $language;
26+
27+
/**
28+
* Template header, body and buttons can be personalized with custom variable values.
29+
*
30+
* @link https://developers.facebook.com/docs/whatsapp/cloud-api/guides/send-message-templates See how you can personalized your templates.
31+
*/
32+
protected array $components;
33+
34+
protected function __construct($to = '', $name = '', $language = 'en_US')
1935
{
2036
$this->to = $to;
2137
$this->name = $name;

tests/Component/CurrencyTest.php

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
<?php
2+
3+
namespace NotificationChannels\WhatsApp\Test;
4+
5+
use NotificationChannels\WhatsApp\Component\Currency;
6+
use PHPUnit\Framework\TestCase;
7+
8+
final class CurrencyTest extends TestCase
9+
{
10+
/** @test */
11+
public function currency_is_valid()
12+
{
13+
$amount = 10.25; // 10,25 €
14+
$currency = new Currency($amount);
15+
$expectedValue = [
16+
'type' => 'currency',
17+
'currency' => [
18+
'amount_1000' => 10250,
19+
'code' => 'EUR',
20+
],
21+
];
22+
23+
$this->assertEquals($expectedValue, $currency->toArray());
24+
}
25+
}

0 commit comments

Comments
 (0)