Bitrix-D7 23.9
 
Загрузка...
Поиск...
Не найдено
shortnumberformatter.php
1<?php
2
4
10{
11 protected static $templates = [
12 3 => 'x-xx',
13 4 => 'xx-xx',
14 5 => 'x-xx-xx',
15 6 => 'xx-xx-xx',
16 7 => 'xxx-xx-xx'
17 ];
18
24 public static function format(PhoneNumber $phoneNumber)
25 {
26 $rawNumber = $phoneNumber->getNationalNumber();
27 $template = static::$templates[mb_strlen($rawNumber)];
28 if(!$template)
29 {
30 return $rawNumber;
31 }
32
33 $pattern = preg_replace("/[^x]/", "", $template);
34 $pattern = str_replace("x", "(\\d)", $pattern);
35 $pattern = "/" . $pattern . "/";
36 $i = 0;
37 $format = preg_replace_callback(
38 "/x/",
39 function () use (&$i)
40 {
41 return "$" . ++$i;
42 },
43 $template
44 );
45
46 $result = preg_replace($pattern, $format, $rawNumber);
47 if($phoneNumber->getExtensionSeparator())
48 {
49 $result .= $phoneNumber->getExtensionSeparator() . " " . $phoneNumber->getExtension();
50 }
51 return $result;
52 }
53
59 public static function isApplicable(PhoneNumber $phoneNumber)
60 {
61 if($phoneNumber->isValid() || $phoneNumber->hasPlus())
62 return false;
63
64 $rawNumber = $phoneNumber->getNationalNumber();
65 return preg_match("/^\d{3,7}$/", $rawNumber);
66 }
67}
static isApplicable(PhoneNumber $phoneNumber)