From e55335b71b1981600200a1d896c9fc2456d1e50f Mon Sep 17 00:00:00 2001 From: Alexander Sagen Date: Tue, 13 Aug 2024 10:06:26 +0200 Subject: [PATCH] Tel: Add support for extensions denoted by auto-dialer pause character (comma) --- composer.json | 2 +- src/tel.php | 48 +++++++++++++++++++++++++++++++++++++++--------- 2 files changed, 40 insertions(+), 10 deletions(-) diff --git a/composer.json b/composer.json index aa02c05..3c9cf4d 100644 --- a/composer.json +++ b/composer.json @@ -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"], diff --git a/src/tel.php b/src/tel.php index 7c76c0c..91a23ed 100644 --- a/src/tel.php +++ b/src/tel.php @@ -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 = ''; @@ -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; } @@ -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 { @@ -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 = ''; } @@ -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 @@ -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; } @@ -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']); } } @@ -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); @@ -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; }