Skip to content

Commit

Permalink
return remaining chars in last part of UTF16 messages
Browse files Browse the repository at this point in the history
  • Loading branch information
juliangut committed Jun 5, 2019
1 parent c475537 commit c5d7489
Show file tree
Hide file tree
Showing 3 changed files with 71 additions and 5 deletions.
6 changes: 6 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,12 @@ stdClass Object
)
```

##### UTF16 notice

When using unicode chars over U+10000 (mainly emoticons 😎) on messages larger than 70 chars the _remaining_ value will actually be the **remaining chars in last message part only**, this is due to how those chars are encoded using two 16bit chars and max part length being an odd number (67)

#### Sanitization

You can sanitize your text to be a valid strict GSM 03.38 charset

```php
Expand Down
18 changes: 17 additions & 1 deletion SMSCounter.php
Original file line number Diff line number Diff line change
Expand Up @@ -259,7 +259,23 @@ function ($carry, $char) {
}

$messages = (int) ceil($length / $perMessage);
$remaining = ($perMessage * $messages) - $length;

if ($encoding === self::UTF16 && $length > $perMessage) {
$count = 0;
foreach ($unicodeArray as $char) {
if ($count === $perMessage) {
$count = 0;
} elseif ($count > $perMessage) {
$count = 2;
}

$count += $char >= 65536 ? 2 : 1;
}

$remaining = $perMessage - ($count > $perMessage ? 2 : $count);
} else {
$remaining = ($perMessage * $messages) - $length;
}

$returnset = new \stdClass();

Expand Down
52 changes: 48 additions & 4 deletions Tests/SMSCounterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -217,18 +217,62 @@ public function testUnicode()
$this->assertEquals($expected, $count);
}

public function testUnicodeEmoji()
public function testUnicodeEmojiSingleMessage()
{
$text = '😎😎😎😎😎😎😎😎😎😎😎😎😎😎😎😎😎😎😎😎😎😎😎😎😎😎😎😎😎😎😎😎😎😎😎😎😎😎😎😎😎😎😎😎😎😎😎😎😎😎😎😎😎😎😎😎😎😎😎😎😎😎😎😎😎😎';
$text = '😎😎😎😎😎😎😎😎😎😎😎😎😎😎😎😎😎😎😎😎😎😎😎😎😎😎😎😎😎😎😎😎😎😎😎';
$smsCounter = new SMSCounter();
$count = $smsCounter->count($text);

$expected = new \stdClass();
$expected->encoding = SMSCounter::UTF16;
$expected->length = 70;
$expected->per_message = 70;
$expected->remaining = 0;
$expected->messages = 1;

$this->assertEquals($expected, $count);
}

public function testUnicodeEmojiMultiPartMessage()
{
// A char is lost at the end of first part
$text = '😎😎😎😎😎😎😎😎😎😎😎😎😎😎😎😎😎😎😎😎😎😎😎😎😎😎😎😎😎😎😎😎😎😎😎😎';
$smsCounter = new SMSCounter();
$count = $smsCounter->count($text);

$expected = new \stdClass();
$expected->encoding = SMSCounter::UTF16;
$expected->length = 72;
$expected->per_message = 67;
$expected->remaining = 61;
$expected->messages = 2;

$this->assertEquals($expected, $count);

// First part is completed with a dash char (-)
$text = '😎😎😎😎😎😎😎😎😎😎😎😎😎😎😎😎😎😎😎😎😎😎😎😎😎😎😎😎😎😎😎😎😎-😎😎😎';
$smsCounter = new SMSCounter();
$count = $smsCounter->count($text);

$expected = new \stdClass();
$expected->encoding = SMSCounter::UTF16;
$expected->length = 73;
$expected->per_message = 67;
$expected->remaining = 61;
$expected->messages = 2;

$this->assertEquals($expected, $count);

// Both parts are completed with dash chars (-)
$text = '😎😎😎😎😎😎😎😎😎😎😎😎😎😎😎😎😎😎😎😎😎😎😎😎😎😎😎😎😎😎😎😎😎-😎😎😎😎😎😎😎😎😎😎😎😎😎😎😎😎😎😎😎😎😎😎😎😎😎😎😎😎😎😎😎😎😎-';
$smsCounter = new SMSCounter();
$count = $smsCounter->count($text);

$expected = new \stdClass();
$expected->encoding = SMSCounter::UTF16;
$expected->length = 132;
$expected->length = 134;
$expected->per_message = 67;
$expected->remaining = 2;
$expected->remaining = 0;
$expected->messages = 2;

$this->assertEquals($expected, $count);
Expand Down

0 comments on commit c5d7489

Please sign in to comment.