Skip to content

Commit 9b1b072

Browse files
committed
Validating Mobile Number
1 parent d347580 commit 9b1b072

3 files changed

Lines changed: 40 additions & 3 deletions

File tree

README.md

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,8 @@ Add your Utalk token on `app/services.php` file. You can get your API Token [her
3434
],
3535
```
3636

37+
**You need to create a `UTALK_TOKEN` on your `.env` file.**
38+
3739
## Usage
3840

3941
Now you can use the channel in your `via()` method inside the notification:
@@ -45,11 +47,24 @@ use Illuminate\Notifications\Notification;
4547

4648
class TeamCreated extends Notification
4749
{
50+
51+
/**
52+
* Get the notification's delivery channels.
53+
*
54+
* @param mixed $notifiable
55+
* @return array
56+
*/
4857
public function via($notifiable)
4958
{
5059
return [UtalkChannel::class];
5160
}
5261

62+
/**
63+
* Get the UTalk representation of the notification.
64+
*
65+
* @param mixed $notifiable
66+
* @return \Powertic\Utalk\UtalkMessage
67+
*/
5368
public function toUtalk($notifiable)
5469
{
5570
return UtalkMessage::create()
@@ -60,9 +75,14 @@ class TeamCreated extends Notification
6075

6176
In order to let your Notification know which number should receive the message, add the `routeNotificationForUtalk` method to your Notifiable model.
6277

63-
This method needs to return the mobile number where the notification will be sent.
78+
**This method expects a [valid E.164 mobile number](https://en.wikipedia.org/wiki/E.164) where the notification will be sent.**
6479

6580
```php
81+
/**
82+
* Route notifications for the Utalk channel.
83+
*
84+
* @return string
85+
*/
6686
public function routeNotificationForUtalk()
6787
{
6888
return $this->mobile;

composer.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,8 @@
1919
"php": "^7.2.5 || ^8.0",
2020
"guzzlehttp/guzzle": "^6.2 || ^7.0",
2121
"illuminate/notifications": "^6.0 || ^7.0 || ^8.0",
22-
"illuminate/support": "^6.0 || ^7.0 || ^8.0"
22+
"illuminate/support": "^6.0 || ^7.0 || ^8.0",
23+
"propaganistas/laravel-phone": "^4.2"
2324
},
2425
"require-dev": {
2526
"mockery/mockery": "^1.3",

src/UtalkChannel.php

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
use Illuminate\Notifications\Notification;
88
use Powertic\Utalk\Exceptions\CouldNotSendNotification;
99
use Powertic\Utalk\Exceptions\InvalidConfiguration;
10+
use Propaganistas\LaravelPhone\PhoneNumber;
1011

1112
class UtalkChannel
1213
{
@@ -21,6 +22,19 @@ public function __construct(Utalk $utalk)
2122
$this->utalk = $utalk;
2223
}
2324

25+
/**
26+
* Transform Mobile Number to Utalk Format
27+
*
28+
* @param [string] $mobile
29+
* @return string
30+
*/
31+
private function transformMobileNumber($mobile)
32+
{
33+
$transformed = PhoneNumber::make($mobile)->formatE164();
34+
$transformed = str_replace('+', '', $transformed);
35+
return "{$transformed}@c.us";
36+
}
37+
2438
/**
2539
* Send the given notification.
2640
*
@@ -35,10 +49,12 @@ public function __construct(Utalk $utalk)
3549
public function send($notifiable, Notification $notification)
3650
{
3751
// Exit if $notifiable doesn't have a route
38-
if (!$to = $notifiable->routeNotificationFor('utalk')) {
52+
if (!$mobile = $notifiable->routeNotificationFor('utalk')) {
3953
return;
4054
}
4155

56+
$to = $this->transformMobileNumber($mobile);
57+
4258
// Exit if token not found
4359
if (!$token = $this->utalk->getToken()) {
4460
throw new InvalidConfiguration();

0 commit comments

Comments
 (0)