Skip to content

Commit

Permalink
Tel: Add support for extensions denoted by auto-dialer pause characte…
Browse files Browse the repository at this point in the history
…r (comma)
  • Loading branch information
alexrsagen committed Aug 13, 2024
1 parent f80fe38 commit e55335b
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 10 deletions.
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "alexrsagen/obie",
"version": "1.6.9",
"version": "1.6.10",
"type": "framework",
"description": "Obie is a simple PHP framework. It aims to provide basic services needed for any web app.",
"keywords": ["framework", "php", "http", "template", "view", "router", "routing", "model", "models", "session", "sessions"],
Expand Down
48 changes: 39 additions & 9 deletions src/tel.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,10 @@ class Tel {
const TYP_UAN = 'uan';
const TYP_VMN = 'voicemail';

const NON_DIGIT_REGEX = '/[^\d\x{FF10}-\x{FF19}\x{0660}-\x{0669}\x{06F0}-\x{06F9}a-zA-Z\s()\.\-]/u';
const NON_INT_PREFIX_OR_DIGIT_REGEX = '/[^\d\x{FF10}-\x{FF19}\x{0660}-\x{0669}\x{06F0}-\x{06F9}a-zA-Z\s()\.\-\+\x{FF0B}]/u';
const NON_DIGIT_REGEX = '/[^\\d\\x{FF10}-\\x{FF19}\\x{0660}-\\x{0669}\\x{06F0}-\\x{06F9}a-zA-Z\\s()\\.\\-]/u';
const NON_INT_PREFIX_OR_DIGIT_REGEX = '/[^\\d\\x{FF10}-\\x{FF19}\\x{0660}-\\x{0669}\\x{06F0}-\\x{06F9}a-zA-Z\\s()\\.\\-\\+\\x{FF0B}]/u';
const NON_PAUSE_PARAMS_OR_EXTENSION_DIGIT_REGEX = '/[^\\d\\x{FF10}-\\x{FF19}\\x{0660}-\\x{0669}\\x{06F0}-\\x{06F9}a-zA-Z\\s()\\.\\-\\*#,~;]/u';
const NON_PAUSE_OR_EXTENSION_DIGIT_REGEX = '/[^\\d\\x{FF10}-\\x{FF19}\\x{0660}-\\x{0669}\\x{06F0}-\\x{06F9}a-zA-Z\\s()\\.\\-\\*#,]/u';

protected string $fmt = '';
protected string $int = '';
Expand Down Expand Up @@ -52,6 +54,9 @@ public function getNumber(): string {
public function getExt(): string {
return $this->ext;
}
public function getExtDigits(): string {
return static::normalize($this->ext);
}
public function getParams(): array {
return $this->params;
}
Expand Down Expand Up @@ -85,7 +90,7 @@ public function setNum(string $num): static {
return $this;
}
public function setExt(string $ext): static {
$this->ext = static::normalize($ext);
$this->ext = static::normalize($ext, true);
return $this;
}
public function setParam(string $key, string $value): static {
Expand All @@ -99,7 +104,7 @@ public function setParam(string $key, string $value): static {
public function setParams(array $params): static {
if (array_key_exists('ext', $params)) {
if (is_string($params['ext'])) {
$this->ext = static::normalize($params['ext']);
$this->ext = static::normalize($params['ext'], true);
} else {
$this->ext = '';
}
Expand Down Expand Up @@ -442,8 +447,29 @@ public static function parse(string $number, ?string $fallback_cc = null, bool $
$res->num = static::normalize($res->num);
$offset += $num_len;

// parse extension, if present
// skip anything that isn't a digit, comma, tilde or semicolon
for (; preg_match(self::NON_PAUSE_PARAMS_OR_EXTENSION_DIGIT_REGEX, substr($number, $offset, 1)) === 1 && $offset < strlen($number); $offset++) {}
if ($offset >= strlen($number)) return $res;

// parse extension denoted by comma (auto-dialer pause character), if present
if (substr($number, $offset, 1) === ',') {
// find extension length by finding first non-digit char
$ext_len = strlen($number) - $offset;
for ($i = $offset; $i < strlen($number); $i++) {
if (preg_match(self::NON_PAUSE_OR_EXTENSION_DIGIT_REGEX, $number[$i]) === 1) {
$ext_len = $i - $offset;
break;
}
}

// set extension
$res->ext = static::normalize(substr($number, $offset, $ext_len), true);
$offset += $ext_len;
}

// parse extension denoted by tilde, if present
if (substr($number, $offset, 1) === '~') {
// skip tilde character
$offset++;

// find extension length by finding first non-digit char
Expand All @@ -456,7 +482,7 @@ public static function parse(string $number, ?string $fallback_cc = null, bool $
}

// set extension
$res->ext = static::normalize(substr($number, $offset, $ext_len));
$res->ext = static::normalize(substr($number, $offset, $ext_len), true);
$offset += $ext_len;
}

Expand All @@ -475,7 +501,7 @@ public static function parse(string $number, ?string $fallback_cc = null, bool $

// extract extension from params
if (array_key_exists('ext', $res->params)) {
$res->ext = static::normalize($res->params['ext']);
$res->ext = static::normalize($res->params['ext'], true);
unset($res->params['ext']);
}
}
Expand Down Expand Up @@ -587,7 +613,7 @@ public function format(?string $fmt = null): string {
return $res;
}

protected static function normalize(string $number): string {
protected static function normalize(string $number, bool $extension = false): string {
// replace lowercase letters with uppercase letters
$number = strtoupper($number);

Expand Down Expand Up @@ -673,7 +699,11 @@ protected static function normalize(string $number): string {
], $number);

// remove non-digits from number
$number = preg_replace('/[^\d]/', '', $number);
if ($extension) {
$number = preg_replace('/[^\\d\\*#,]/', '', $number);
} else {
$number = preg_replace('/[^\\d]/', '', $number);
}

return $number;
}
Expand Down

0 comments on commit e55335b

Please sign in to comment.