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

PhoneFormatter::run()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
cc 1
eloc 1
c 2
b 0
f 0
nc 1
nop 1
dl 0
loc 3
rs 10
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
}