Skip to content

Commit e093d00

Browse files
committed
Add support for encoding
1 parent 984997b commit e093d00

3 files changed

Lines changed: 79 additions & 4 deletions

File tree

CHANGELOG.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,10 @@
11
# Changelog
22

3+
## 4.1.0 - 2025-03-19
4+
* Add support for `encoding` via `SMSMessage::ENCODING_UTF8` and `SMSMessage::Encoding_UCS2`. Default is `null`, which
5+
uses `UTF8`. Note that `UTF8` means that the GSM-7 character set is used. `UCS2` adds the ability to use emojis, but
6+
increases the length (and hence cost) of your messages. See https://gatewayapi.com/docs/apis/rest/#basic-use.
7+
38
## 4.0.0 - 2025-02-05
49
* Require at least PHP 7.3.
510
* Test against PHP 8.4.

src/Entities/Request/SMSMessage.php

Lines changed: 44 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
* @property int|null $sendtime
1919
* @property string|null $userref
2020
* @property string|null $callback_url
21+
* @property string|null $encoding
2122
* @package nickdnk\GatewayAPI
2223
*/
2324
class SMSMessage implements JsonSerializable
@@ -29,7 +30,20 @@ class SMSMessage implements JsonSerializable
2930
const CLASS_PREMIUM = 'premium';
3031
const CLASS_SECRET = 'secret';
3132

32-
private $message, $sender, $recipients, $tags, $sendtime, $class, $userref, $callbackUrl;
33+
/**
34+
* Passing `UTF8` as encoding means the message will use the GSM-7 character set. To use emojis etc., use
35+
* `SMSMessage::ENCODING_UCS2`.
36+
* @link https://gatewayapi.com/docs/glossary/#gsm-7
37+
*/
38+
const ENCODING_UTF8 = 'UTF8';
39+
/**
40+
* Passing `UCS2` as encoding means the message will use the UCS2 character set, which allows for emojis but
41+
* increases message length.
42+
* @link https://gatewayapi.com/docs/glossary/#ucs-2
43+
*/
44+
const ENCODING_UCS2 = 'UCS2';
45+
46+
private $message, $sender, $recipients, $tags, $sendtime, $class, $userref, $callbackUrl, $encoding;
3347

3448
public static function constructFromArray(array $array): SMSMessage
3549
{
@@ -61,7 +75,8 @@ public static function constructFromArray(array $array): SMSMessage
6175
$array['tags'],
6276
array_key_exists('sendtime', $array) ? $array['sendtime'] : null,
6377
$array['class'],
64-
array_key_exists('callback_url', $array) ? $array['callback_url'] : null
78+
array_key_exists('callback_url', $array) ? $array['callback_url'] : null,
79+
array_key_exists('encoding', $array) ? $array['encoding'] : null
6580
);
6681

6782
}
@@ -81,10 +96,11 @@ public static function constructFromArray(array $array): SMSMessage
8196
* @param int|null $sendTime
8297
* @param string $class
8398
* @param string|null $callbackUrl
99+
* @param string|null $encoding
84100
*/
85101
public function __construct(string $message, string $senderName, array $recipients = [],
86102
?string $userReference = null, array $tags = [], ?int $sendTime = null, string $class = self::CLASS_STANDARD,
87-
?string $callbackUrl = null
103+
?string $callbackUrl = null, ?string $encoding = null
88104
)
89105
{
90106

@@ -95,6 +111,7 @@ public function __construct(string $message, string $senderName, array $recipien
95111
$this->tags = $tags;
96112
$this->sendtime = $sendTime;
97113
$this->callbackUrl = $callbackUrl;
114+
$this->setEncoding($encoding);
98115
$this->setClass($class);
99116

100117
}
@@ -130,6 +147,21 @@ public function setClass(string $class): void
130147

131148
}
132149

150+
public function setEncoding(?string $encoding): void
151+
{
152+
153+
if ($encoding !== self::ENCODING_UTF8
154+
&& $encoding !== self::ENCODING_UCS2
155+
&& $encoding !== null) {
156+
throw new InvalidArgumentException(
157+
'SMS encoding must be one of the provided constants or null. Received value: ' . $encoding
158+
);
159+
}
160+
161+
$this->encoding = $encoding;
162+
163+
}
164+
133165
public function getClass(): string
134166
{
135167

@@ -174,6 +206,11 @@ public function getCallbackUrl(): ?string
174206
return $this->callbackUrl;
175207
}
176208

209+
public function getEncoding(): ?string
210+
{
211+
return $this->encoding;
212+
}
213+
177214
public function setSendTime(int $sendTime): void
178215
{
179216

@@ -257,6 +294,10 @@ public function jsonSerialize(): array
257294
$json['callback_url'] = $this->callbackUrl;
258295
}
259296

297+
if ($this->encoding !== null) {
298+
$json['encoding'] = $this->encoding;
299+
}
300+
260301
return $json;
261302
}
262303

tests/SMSMessageTest.php

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ public function testConstructFromJSON()
2121
new Recipient(4561232323, ['Joe', '22']),
2222
new Recipient(4577364722, ['Mark', '23'])
2323
], 'reference text', ['%NAME%', '%AGE%'], 1585835858, SMSMessage::CLASS_SECRET,
24-
'https://example.com/callback'
24+
'https://example.com/callback', SMSMessage::ENCODING_UCS2
2525
)
2626
)
2727
);
@@ -39,6 +39,7 @@ public function testConstructFromJSON()
3939
$this->assertEquals(1585835858, $self->getSendtime());
4040

4141
$this->assertEquals('https://example.com/callback', $self->getCallbackUrl());
42+
$this->assertEquals('UCS2', $self->getEncoding());
4243

4344
}
4445

@@ -168,4 +169,32 @@ public function testCallbackUrl()
168169

169170
$this->assertEquals('https://example.com/callback', $message->getCallbackUrl());
170171
}
172+
173+
public function testEncodingUCS2()
174+
{
175+
176+
$message = new SMSMessage('test', 'sender');
177+
$message->setEncoding(SMSMessage::ENCODING_UCS2);
178+
179+
$this->assertEquals('UCS2', $message->getEncoding());
180+
}
181+
182+
public function testEncodingUTF8()
183+
{
184+
185+
$message = new SMSMessage('test', 'sender');
186+
$message->setEncoding(SMSMessage::ENCODING_UTF8);
187+
188+
$this->assertEquals('UTF8', $message->getEncoding());
189+
}
190+
191+
public function testEncodingNull()
192+
{
193+
194+
$message = new SMSMessage('test', 'sender');
195+
$message->setEncoding(SMSMessage::ENCODING_UTF8);
196+
$message->setEncoding(null);
197+
198+
$this->assertNull($message->getEncoding());
199+
}
171200
}

0 commit comments

Comments
 (0)