Нашел для себя следующее решение:
protected $mask = [
7 => [
'country' => 'ru',
'mask' => "/
(7)?\D* # optional country code
(\d{3})?\D* # optional area code
(\d{3})\D* # first three
(\d{2}) # first two
(\d{2}) # second two
/x"
],
355 => [
'country' => 'al',
'mask' => "/
(355)?\D* # optional country code
(\d{3})?\D* # optional area code
(\d{3})\D* # first three
(\d{3}) # second three
/x"
],
//.....
];
public function __construct($phone = null, $code = null)
{
if(! $phone){
throw new Exception('Not set phone');
}
$this->phone = $phone;
if($code){
$this->code = $code;
}
}
public function getMaskPhone() {
$code = empty($this->code) ? '' : $this->code;
$phone = empty($this->phone) ? '' : $this->phone;
if (empty($code)) {
return $phone;
}
$mask = empty(self::$mask[$code]['mask']) ? '' : self::$mask[$code]['mask'];
if (empty($mask)) {
return "+".$code." ".$phone;
}
$phone = $code.$phone;
preg_match($mask, $phone, $matches);
if(!isset($matches[0])){
return false;
}
$country = $matches[1];
$area = $matches[2];
$firstPart = empty($matches[3]) ? '' : $matches[3];
$secondPart = empty($matches[4]) ? '' : $matches[4];
$thirdPart = empty($matches[5]) ? '' : $matches[5];
$fourthPart = empty($matches[6]) ? '' : $matches[6];
$fifthPart = empty($matches[7]) ? '' : $matches[7];
$out = $firstPart;
if (!empty($secondPart)) {
$out .= "-".$secondPart;
}
if (!empty($thirdPart)) {
$out .= "-".$thirdPart;
}
if (!empty($fourthPart)) {
$out .= "-".$fourthPart;
}
if (!empty($fifthPart)) {
$out .= "-".$fifthPart;
}
if (!empty($area)) {
$out = "(".$area.")".$out;
} else {
$out = $area."-".$out;
}
if (!empty($country)) {
$out = "+".$country.$out;
}
if(!empty($ext)) {
$out .= "x".$ext;
}
return $out;
}