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 */
2324class 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
0 commit comments