📦 anlutro / php-bulk-sms

📄 Message.php · 239 lines
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239<?php
/**
 * BulkSMS PHP implementation
 *
 * @author    Andreas Lutro <anlutro@gmail.com>
 * @license   http://opensource.org/licenses/MIT
 * @package   anlutro/bulk-sms
 */

namespace anlutro\BulkSms;

/**
 * Container class for a single SMS message.
 */
class Message
{
    /**
     * Phone number of the recipient.
     *
     * @var string
     */
    protected $recipient;

    /**
     * The text message to be sent.
     *
     * @var string
     */
    protected $message;

    /**
     * Whether or not the message needs to be concatenated.
     *
     * @var bool
     */
    protected $concat = false;

    /**
     * Where to start concatenating SMSes.
     *
     * @var integer
     */
    protected $concatLimit = 140;

    /**
     * @param $recipient
     * @param $text
     */
    public function __construct($recipient, $text)
    {
        $this->setRecipient($recipient);
        $this->setMessage($text);
    }

    /**
     * Get the recipient.
     *
     * @return string
     */
    public function getRecipient()
    {
        return $this->recipient;
    }

    /**
     * Set the recipient.
     *
     * @param  string|int $recipient
     *
     * @return $this
     */
    protected function setRecipient($recipient)
    {
        $this->recipient = $this->parseNumber($recipient);

        return $this;
    }

    /**
     * Get the message.
     *
     * @return string
     */
    public function getMessage()
    {
        return $this->message;
    }

    /**
     * Set the message.
     *
     * @param  string $message
     *
     * @return $this
     */
    protected function setMessage($message)
    {
        $this->message = $this->encodeMessage($message);

        if (strlen($this->message) > $this->concatLimit) {
            $this->concat = true;
        }

        return $this;
    }

    /**
     * Get how many SMSes the message may have to be concatenated into.
     *
     * @return int
     */
    public function getConcatParts()
    {
        if (!$this->concat) {
            return 1;
        } else {
            return $this->calculateConcat();
        }
    }

    /**
     * Calculate from the message string how many SMSes it may have to span.
     *
     * @return int
     */
    protected function calculateConcat()
    {
        $len = strlen($this->message);
        $i   = $this->concatLimit;
        $j   = 1;

        while ($i < $len) {
            $i += $this->concatLimit;
            $j++;
        }

        return $j;
    }

    /**
     * Parse a phone number.
     *
     * @param  string $number
     *
     * @return string
     */
    protected function parseNumber($number)
    {
        $number = (string) $number;

        // remove whitespaces
        $number = trim($number);
        $number = str_replace(' ', '', $number);

        // remove + in front if exists
        if (substr($number, 0, 1) == '+') {
            $number = substr($number, 1);
        }

        // remove 0s in front if exists
        while (substr($number, 0, 1) === '0') {
            $number = substr($number, 1);
        }

        // we should at this point have a normal number
        if (!is_numeric($number)) {
            throw new \InvalidArgumentException("Invalid SMS recipient: $number");
        }

        // is phone number is less than 9 characters, assume we need to append
        // a country code
        if (strlen($number) <= 8) {
            throw new \InvalidArgumentException(
                "Recipient number is too short. Is the country code missing?: " . $number
            );
        }

        return $number;
    }

    /**
     * Encode a message to the retarded GSM-charset.
     *
     * @param  string $message
     *
     * @return string
     */
    protected function encodeMessage($message)
    {
        $replaceCharacters = array(
            'Δ' => '0xD0',
            'Φ' => '0xDE',
            'Γ' => '0xAC',
            'Λ' => '0xC2',
            'Ω' => '0xDB',
            'Π' => '0xBA',
            'Ψ' => '0xDD',
            'Σ' => '0xCA',
            'Θ' => '0xD4',
            'Ξ' => '0xB1',
            '¡' => '0xA1',
            '£' => '0xA3',
            '¤' => '0xA4',
            '¥' => '0xA5',
            '§' => '0xA7',
            '¿' => '0xBF',
            'Ä' => '0xC4',
            'Å' => '0xC5',
            'Æ' => '0xC6',
            'Ç' => '0xC7',
            'É' => '0xC9',
            'Ñ' => '0xD1',
            'Ö' => '0xD6',
            'Ø' => '0xD8',
            'Ü' => '0xDC',
            'ß' => '0xDF',
            'à' => '0xE0',
            'ä' => '0xE4',
            'å' => '0xE5',
            'æ' => '0xE6',
            'è' => '0xE8',
            'é' => '0xE9',
            'ì' => '0xEC',
            'ñ' => '0xF1',
            'ò' => '0xF2',
            'ö' => '0xF6',
            'ø' => '0xF8',
            'ù' => '0xF9',
            'ü' => '0xFC',
        );

        $message = utf8_decode($message);
        $message = str_replace('"', '\"', $message);
        $message = strtr($message, $replaceCharacters);

        return $message;
    }
}