Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 33 additions & 2 deletions src/SmsLength.php
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,11 @@ class SmsLength
*/
private $size;

/**
* @var int
*/
private $padding;

/**
* @var int
*/
Expand Down Expand Up @@ -111,6 +116,16 @@ public function getSize()
return $this->size;
}

/**
* Get size of message padding used in the determined encoding
*
* @return int
*/
public function getPadding()
{
return $this->padding;
}

/**
* Get number of messages that would be used to send the given content size
*
Expand Down Expand Up @@ -175,12 +190,19 @@ private function inspect($messageContent)
// Any character outside the 7-bit alphabet switches the entire encoding to UCS-2
$this->encoding = '7-bit';
$this->size = 0;
$this->padding = 0;

$mbLength = mb_strlen($messageContent, 'UTF-8');
for ($i = 0; $i < $mbLength; $i++) {
$char = mb_substr($messageContent, $i, 1, 'UTF-8');
if (in_array($char, self::GSM0338_BASIC)) {
$this->size++;
} elseif (in_array($char, self::GSM0338_EXTENDED)) {
// In cases where a double counted char straddles two messages, add padding to push it to the next part
if (($this->size + 2) % self::MAXIMUM_CHARACTERS_7BIT_CONCATENATED === 1) {
$this->padding++;
}

$this->size += 2;
} else {
$this->encoding = 'ucs-2';
Expand All @@ -195,7 +217,14 @@ private function inspect($messageContent)
for ($i = 0; $i < $mbLength; $i++) {
$char = mb_substr($messageContent, $i, 1, 'UTF-8');
$utf16Hex = bin2hex(mb_convert_encoding($char, 'UTF-16', 'UTF-8'));
$this->size += strlen($utf16Hex) / 4;
$charSize = strlen($utf16Hex) / 4;

// In cases where a double counted char straddles two messages, add padding to push it to the next part
if ($charSize > 1 && ($this->size + $charSize) % self::MAXIMUM_CHARACTERS_UCS2_CONCATENATED === 1) {
$this->padding++;
}

$this->size += $charSize;
}
}

Expand All @@ -207,9 +236,11 @@ private function inspect($messageContent)
$concatSize = self::MAXIMUM_CHARACTERS_UCS2_CONCATENATED;
}

$sizeIncludingPadding = $this->size + $this->padding;

$this->messageCount = 1;
if ($this->size > $singleSize) {
$this->messageCount = (int)ceil($this->size / $concatSize);
$this->messageCount = (int)ceil($sizeIncludingPadding / $concatSize);
}
}
}
17 changes: 16 additions & 1 deletion tests/SmsLengthTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,22 @@ public function providerSize()
[str_repeat("exact•max", 67), 'ucs-2', 603, 9, 603],

// empty
['', '7-bit', 0, 1, 160]
['', '7-bit', 0, 1, 160],

[
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you add a comment explaining what the test is checking, please? Something which explains the value, like in #1 would be great:

The '[' at character 152 will become "0x1B[" (2 characters), and become part of the 2nd message, which pushes the last "." at the end of the content into a 3rd message

'The quick brown fox jumps over the lazy dog. The quick brown fox jumps over the lazy dog. The quick brown fox jumps over the lazy dog. The quick brown f[x jumps over the lazy dog. The quick brown fox jumps over the lazy dog. The quick brown fox jumps over the lazy dog. The quick brown fox jumps over the.',
'7-bit',
306,
3,
459,
],
[
str_repeat('🌐', 67),
'ucs-2',
134,
3,
201
],
];
}

Expand Down