• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

nickdnk / gatewayapi-php / 13949882358

19 Mar 2025 02:58PM UTC coverage: 91.509% (-0.5%) from 91.99%
13949882358

push

github

nickdnk
Add support for encoding

10 of 13 new or added lines in 1 file covered. (76.92%)

388 of 424 relevant lines covered (91.51%)

21.99 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

96.25
/src/Entities/Request/SMSMessage.php
1
<?php
2

3

4
namespace nickdnk\GatewayAPI\Entities\Request;
5

6
use InvalidArgumentException;
7
use JsonSerializable;
8
use nickdnk\GatewayAPI\Entities\Constructable;
9

10
/**
11
 * Class SMSMessage
12
 *
13
 * @property string      $class
14
 * @property string      $message
15
 * @property string      $sender
16
 * @property Recipient[] $recipients
17
 * @property string[]    $tags
18
 * @property int|null    $sendtime
19
 * @property string|null $userref
20
 * @property string|null $callback_url
21
 * @property string|null $encoding
22
 * @package nickdnk\GatewayAPI
23
 */
24
class SMSMessage implements JsonSerializable
25
{
26

27
    use Constructable;
28

29
    const CLASS_STANDARD = 'standard';
30
    const CLASS_PREMIUM  = 'premium';
31
    const CLASS_SECRET   = 'secret';
32

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;
47

48
    public static function constructFromArray(array $array): SMSMessage
49
    {
50

51
        if (array_key_exists('class', $array)
14 ✔
52
            && array_key_exists('message', $array)
14 ✔
53
            && array_key_exists('sender', $array)
14 ✔
54
            && array_key_exists('recipients', $array)
14 ✔
55
            && array_key_exists('tags', $array)
14 ✔
56
            && is_string($array['class'])
14 ✔
57
            && is_string($array['message'])
14 ✔
58
            && is_string($array['sender'])
14 ✔
59
            && is_array($array['recipients'])
14 ✔
60
            && is_array($array['tags'])) {
14 ✔
61

62
            $recipients = [];
7 ✔
63

64
            foreach ($array['recipients'] as $recipient) {
7 ✔
65

66
                $recipients[] = Recipient::constructFromArray($recipient);
7 ✔
67

68
            }
69

70
            return new self(
7 ✔
71
                $array['message'],
7 ✔
72
                $array['sender'],
7 ✔
73
                $recipients,
7 ✔
74
                array_key_exists('userref', $array) ? $array['userref'] : null,
7 ✔
75
                $array['tags'],
7 ✔
76
                array_key_exists('sendtime', $array) ? $array['sendtime'] : null,
7 ✔
77
                $array['class'],
7 ✔
78
                array_key_exists('callback_url', $array) ? $array['callback_url'] : null,
7 ✔
79
                array_key_exists('encoding', $array) ? $array['encoding'] : null
7 ✔
80
            );
7 ✔
81

82
        }
83

84
        throw new InvalidArgumentException('Array passed to ' . self::class . ' is missing required parameters.');
7 ✔
85

86
    }
87

88
    /**
89
     * SMSMessage constructor.
90
     *
91
     * @param string      $message
92
     * @param string      $senderName
93
     * @param Recipient[] $recipients
94
     * @param string|null $userReference
95
     * @param string[]    $tags
96
     * @param int|null    $sendTime
97
     * @param string      $class
98
     * @param string|null $callbackUrl
99
     * @param string|null $encoding
100
     */
101
    public function __construct(string $message, string $senderName, array $recipients = [],
102
        ?string $userReference = null, array $tags = [], ?int $sendTime = null, string $class = self::CLASS_STANDARD,
103
        ?string $callbackUrl = null, ?string $encoding = null
104
    )
105
    {
106

107
        $this->message = $message;
91 ✔
108
        $this->sender = $senderName;
91 ✔
109
        $this->recipients = $recipients;
91 ✔
110
        $this->userref = $userReference;
91 ✔
111
        $this->tags = $tags;
91 ✔
112
        $this->sendtime = $sendTime;
91 ✔
113
        $this->callbackUrl = $callbackUrl;
91 ✔
114
        $this->setEncoding($encoding);
91 ✔
115
        $this->setClass($class);
91 ✔
116

117
    }
118

119
    /**
120
     * @param string[] $tags
121
     */
122
    public function setTags(array $tags): void
123
    {
124

125
        $this->tags = $tags;
7 ✔
126
    }
127

128

129
    /**
130
     * Must be one of the available constants; `standard`, `premium` or `secret`. Use the built-in constants provided
131
     * by this class, i.e: `SMSMessage::CLASS_STANDARD`.
132
     *
133
     * @param string $class
134
     */
135
    public function setClass(string $class): void
136
    {
137

138
        if ($class !== self::CLASS_STANDARD
91 ✔
139
            && $class !== self::CLASS_PREMIUM
91 ✔
140
            && $class !== self::CLASS_SECRET) {
91 ✔
141
            throw new InvalidArgumentException(
7 ✔
142
                'SMS class must be one of the provided constants. Received value: ' . $class
7 ✔
143
            );
7 ✔
144
        }
145

146
        $this->class = $class;
91 ✔
147

148
    }
149

150
    public function setEncoding(?string $encoding): void
151
    {
152

153
        if ($encoding !== self::ENCODING_UTF8
91 ✔
154
            && $encoding !== self::ENCODING_UCS2
91 ✔
155
            && $encoding !== null) {
91 ✔
NEW
156
            throw new InvalidArgumentException(
×
NEW
157
                'SMS encoding must be one of the provided constants or null. Received value: ' . $encoding
×
NEW
158
            );
×
159
        }
160

161
        $this->encoding = $encoding;
91 ✔
162

163
    }
164

165
    public function getClass(): string
166
    {
167

168
        return $this->class;
7 ✔
169
    }
170

171
    public function getMessage(): string
172
    {
173

174
        return $this->message;
7 ✔
175
    }
176

177
    public function getSender(): string
178
    {
179

180
        return $this->sender;
7 ✔
181
    }
182

183
    /**
184
     * @return string[]
185
     */
186
    public function getTags(): array
187
    {
188

189
        return $this->tags;
7 ✔
190
    }
191

192
    public function getSendtime(): ?int
193
    {
194

195
        return $this->sendtime;
14 ✔
196
    }
197

198
    public function getUserReference(): ?string
199
    {
200

201
        return $this->userref;
14 ✔
202
    }
203

204
    public function getCallbackUrl(): ?string
205
    {
206
        return $this->callbackUrl;
14 ✔
207
    }
208

209
    public function getEncoding(): ?string
210
    {
211
        return $this->encoding;
28 ✔
212
    }
213

214
    public function setSendTime(int $sendTime): void
215
    {
216

217
        $this->sendtime = $sendTime;
7 ✔
218
    }
219

220
    public function setUserReference(string $userReference): void
221
    {
222

223
        $this->userref = $userReference;
7 ✔
224
    }
225

226
    public function setCallbackUrl(string $callbackUrl): void
227
    {
228
        $this->callbackUrl = $callbackUrl;
7 ✔
229
    }
230

231
    /**
232
     * Sets the send-time of the message to null. Messages with no send time are sent immediately.
233
     */
234
    public function removeSendTime(): void
235
    {
236

237
        $this->sendtime = null;
7 ✔
238
    }
239

240
    /**
241
     * Returns all the recipients that have been added to the SMS message.
242
     *
243
     * @return Recipient[]
244
     */
245
    public function getRecipients(): array
246
    {
247

248
        return $this->recipients;
21 ✔
249
    }
250

251
    /**
252
     * Adds a single recipient to the message.
253
     *
254
     * @param Recipient $recipient
255
     */
256
    public function addRecipient(Recipient $recipient)
257
    {
258

259
        $this->recipients[] = $recipient;
7 ✔
260
    }
261

262
    /**
263
     * Sets the recipient array, overriding any existing recipients of the message.
264
     *
265
     * @param Recipient[] $recipients
266
     */
267
    public function setRecipients(array $recipients)
268
    {
269

270
        $this->recipients = $recipients;
7 ✔
271

272
    }
273

274
    public function jsonSerialize(): array
275
    {
276

277
        $json = [
14 ✔
278
            'class'      => $this->class,
14 ✔
279
            'message'    => $this->message,
14 ✔
280
            'sender'     => $this->sender,
14 ✔
281
            'recipients' => $this->recipients,
14 ✔
282
            'tags'       => $this->tags
14 ✔
283
        ];
14 ✔
284

285
        if ($this->userref !== null) {
14 ✔
286
            $json['userref'] = $this->userref;
7 ✔
287
        }
288

289
        if ($this->sendtime !== null) {
14 ✔
290
            $json['sendtime'] = $this->sendtime;
7 ✔
291
        }
292

293
        if ($this->callbackUrl !== null) {
14 ✔
294
            $json['callback_url'] = $this->callbackUrl;
7 ✔
295
        }
296

297
        if ($this->encoding !== null) {
14 ✔
298
            $json['encoding'] = $this->encoding;
7 ✔
299
        }
300

301
        return $json;
14 ✔
302
    }
303

304

305
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE TRIAL · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc