PhoneFormatter::format()   B
last analyzed

Complexity

Conditions 7
Paths 5

Size

Total Lines 14
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 9
CRAP Score 7

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 8
c 1
b 0
f 0
dl 0
loc 14
ccs 9
cts 9
cp 1
rs 8.8333
cc 7
nc 5
nop 2
crap 7
1
<?php
2
3
4
namespace floor12\phone;
5
6
use yii\helpers\Html;
7
8
class PhoneFormatter
9
{
10
11
    /**
12
     * @param $phone
13
     * @return string
14
     */
15
    public static function run($phone)
16
    {
17
        return self::a($phone);
18
    }
19
20
    /**
21
     * @param $phone
22
     * @param array $options
23
     * @return string
24
     */
25 3
    public static function a($phone, array $options = [], $countCodeLength = 1)
26
    {
27 3
        return Html::a(self::format($phone, $countCodeLength), "tel:+{$phone}", $options);
28
    }
29
30
    /**
31
     * @param $phone
32
     * @return string
33
     */
34 7
    public static function format($phone, $countCodeLength = 1)
35
    {
36
37 7
        if (preg_match('/^(\d{1})(\d{3})(\d{3})(\d{2})(\d{2})$/', $phone, $matches) && $countCodeLength == 1)
38 2
            return "+" . $matches[1] . ' (' . $matches[2] . ') ' . $matches[3] . "-" . $matches[4] . '-' . $matches[5];
39
40 5
        if (preg_match('/^(\d{2})(\d{3})(\d{3})(\d{2})(\d{2})$/', $phone, $matches) && $countCodeLength == 1)
41 2
            return "+" . $matches[1] . ' (' . $matches[2] . ') ' . $matches[3] . "-" . $matches[4] . '-' . $matches[5];
42
43 3
        if ($countCodeLength == 2)
44 2
            if (preg_match('/^(\d{2})(\d{3})(\d{2})(\d{2})(\d{2})$/', $phone, $matches))
45 2
                return "+" . $matches[1] . ' (' . $matches[2] . ') ' . $matches[3] . "-" . $matches[4] . '-' . $matches[5];
46
47 1
        return $phone;
48
    }
49
}