Completed
Push — master ( 4751d8...f983ef )
by Evgenii
68:04 queued 27:49
created

PhoneFormatter   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 35
Duplicated Lines 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
eloc 8
c 2
b 0
f 0
dl 0
loc 35
rs 10
wmc 5

3 Methods

Rating   Name   Duplication   Size   Complexity  
A run() 0 3 1
A format() 0 9 3
A a() 0 3 1
1
<?php
2
3
/**
4
 * Created by PhpStorm.
5
 * User: floor12
6
 * Date: 09.03.2017
7
 * Time: 9:48
8
 */
9
10
namespace floor12\phone;
11
12
use yii\helpers\Html;
13
14
class PhoneFormatter
15
{
16
17
    /**
18
     * @param $phone
19
     * @return string
20
     */
21
    public static function run($phone)
22
    {
23
        return self::a($phone);
24
    }
25
26
    /**
27
     * @param $phone
28
     * @param array $options
29
     * @return string
30
     */
31
    public static function a($phone, array $options = [])
32
    {
33
        return Html::a(self::format($phone), "tel:+{$phone}", $options);
34
    }
35
36
    /**
37
     * @param $phone
38
     * @return string
39
     */
40
    public static function format($phone)
41
    {
42
        if (preg_match('/^(\d{1})(\d{3})(\d{3})(\d{2})(\d{2})$/', $phone, $matches))
43
            return "+" . $matches[1] . ' (' . $matches[2] . ') ' . $matches[3] . "-" . $matches[4] . '-' . $matches[5];
44
45
        if (preg_match('/^(\d{2})(\d{3})(\d{3})(\d{2})(\d{2})$/', $phone, $matches))
46
            return "+" . $matches[1] . ' (' . $matches[2] . ') ' . $matches[3] . "-" . $matches[4] . '-' . $matches[5];
47
48
        return $phone;
49
    }
50
}