Add forceSMS() and common methods to check length of messages

This commit is contained in:
Kijin Sung 2016-11-03 22:17:35 +09:00
parent 21f6e5f25c
commit 80fc8a0bd5
2 changed files with 93 additions and 34 deletions

View file

@ -7,6 +7,16 @@ namespace Rhymix\Framework\Drivers\SMS;
*/
class CoolSMS extends Base implements \Rhymix\Framework\Drivers\SMSInterface
{
/**
* Maximum length of an SMS.
*/
protected $_maxlength_sms = 90;
/**
* Maximum length of an LMS.
*/
protected $_maxlength_lms = 2000;
/**
* Get the list of configuration fields required by this mail driver.
*
@ -50,7 +60,7 @@ class CoolSMS extends Base implements \Rhymix\Framework\Drivers\SMSInterface
$options->charset = 'UTF-8';
// Determine the message type based on the length.
$options->type = $this->_isShort($options->text) ? 'SMS' : 'LMS';
$options->type = $message->checkLength($options->text, $this->_maxlength_sms) ? 'SMS' : 'LMS';
// If the message has a subject, it must be an LMS.
if ($subject = $message->getSubject())
@ -68,11 +78,11 @@ class CoolSMS extends Base implements \Rhymix\Framework\Drivers\SMSInterface
}
// If the recipient is not a Korean number, force SMS.
if ($recipient->country && $recipient->country != 82)
if ($message->isForceSMS() || ($recipient->country && $recipient->country != 82))
{
unset($options->subject);
unset($options->image);
$options->text = $this->_cutShort($options->text);
$options->text = array_first($message->splitMessage($options->text, $this->_maxlength_sms));
$options->type = 'SMS';
}
@ -88,34 +98,4 @@ class CoolSMS extends Base implements \Rhymix\Framework\Drivers\SMSInterface
return false;
}
}
/**
* Check if a message is short enough to fit in an SMS.
*
* @param string $message
* @param int $maxlength (optional)
* @return bool
*/
protected function _isShort($message, $maxlength = 90)
{
$message = @iconv('UTF-8', 'CP949//IGNORE', $message);
return strlen($message) <= $maxlength;
}
/**
* Cut a message to fit in an SMS.
*
* @param string $message
* @param int $maxlength (optional)
* @return string
*/
protected function _cutShort($message, $maxlength = 90)
{
$message = @iconv('UTF-8', 'CP949//IGNORE', $message);
while (strlen($message) > $maxlength)
{
$message = iconv_substr($message, 0, iconv_strlen($str, 'CP949') - 1, 'CP949');
}
return iconv('CP949', 'UTF-8', $message);
}
}

View file

@ -17,6 +17,7 @@ class SMS
protected $subject = '';
protected $content = '';
protected $attachments = array();
protected $force_sms = false;
public $errors = array();
protected $sent = false;
@ -47,7 +48,7 @@ class SMS
if (!self::$default_driver)
{
$default_driver = config('sms.type');
$default_driver_class = '\\Rhymix\\Framework\\Drivers\SMS\\' . $default_driver;
$default_driver_class = '\Rhymix\Framework\Drivers\SMS\\' . $default_driver;
if (class_exists($default_driver_class))
{
$default_driver_config = config('sms.' . $default_driver) ?: array();
@ -292,6 +293,26 @@ class SMS
return $this->attachments;
}
/**
* Force this message to use SMS (not LMS or MMS).
*
* @return void
*/
public function forceSMS()
{
$this->force_sms = true;
}
/**
* Check if this message is forced to use SMS.
*
* @return void
*/
public function isForceSMS()
{
return $this->force_sms;
}
/**
* Send the email.
*
@ -369,4 +390,62 @@ class SMS
{
return $this->errors;
}
/**
* Check if a message is no longer than the given length.
*
* This is useful when checking whether a message can fit into a single SMS.
*
* @param string $message
* @param int $maxlength
* @param string $measure_in_charset (optional)
* @return
*/
public function checkLength($message, $maxlength, $measure_in_charset = 'CP949')
{
$message = @iconv('UTF-8', $measure_in_charset . '//IGNORE', $message);
return strlen($message) <= $maxlength;
}
/**
* Split a message into several short messages.
*
* This is useful when sending a long message as a series of SMS.
*
* @param string $message
* @param int $maxlength
* @param string $measure_in_charset (optional)
* @return array
*/
public function splitMessage($message, $maxlength, $measure_in_charset = 'CP949')
{
$message = utf8_trim(utf8_normalize_spaces($message));
$chars = preg_split('//u', $message, -1, PREG_SPLIT_NO_EMPTY);
$result = array();
$current_entry = '';
$current_length = 0;
foreach ($chars as $char)
{
$char_length = strlen(@iconv('UTF-8', $measure_in_charset . '//IGNORE', $char));
if ($current_length + $char_length > $maxlength)
{
$result[] = $current_entry;
$current_entry = $char;
$current_length = $char_length;
}
else
{
$current_entry .= $char;
$current_length += $char_length;
}
}
if ($current_entry !== '')
{
$result[] = $current_entry;
}
return $result;
}
}