PhoneHelper   A
last analyzed

Complexity

Total Complexity 11

Size/Duplication

Total Lines 103
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 3

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 11
c 1
b 0
f 0
lcom 0
cbo 3
dl 0
loc 103
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
A format() 0 13 4
B link() 0 33 6
A buildUrl() 0 6 1
1
<?php
2
/**
3
 * Phone Helper (https://github.com/PotatoPowered/phone-helper)
4
 *
5
 * Licensed under The MIT License
6
 * For full copyrgiht and license information, please see the LICENSE
7
 * Redistributions of files must retain the above copyright notice.
8
 *
9
 * @author      Blake Sutton <[email protected]>
10
 * @copyright   Copyright (c) Potato Powered Software
11
 * @link        http://potatopowered.net
12
 * @license     http://www.opensource.org/licenses/mit-license.php MIT License
13
 */
14
namespace PhoneHelper\View\Helper;
15
16
use Cake\View\Helper;
17
use Cake\View\StringTemplateTrait;
18
19
/**
20
 * PhoneHelper Main Class
21
 *
22
 * This class extends the NumberHelper built into CakePHP 3.x and adds the ability
23
 * to parse and prettify a US telephone number.
24
 */
25
class PhoneHelper extends Helper
26
{
27
    use StringTemplateTrait;
28
29
    /**
30
     * Default config for this class
31
     *
32
     * @var array
33
     */
34
    protected $_defaultConfig = [
35
        'templates' => [
36
            'phone' => '<a href="{{url}}"{{attrs}}>{{content}}</a>'
37
        ]
38
    ];
39
40
    /**
41
     * Prettify a phone number.
42
     *
43
     * This function can take either a 10 digit or 7 digit phone number and returns
44
     * a more human readable version.
45
     *
46
     * @param int $number The phone number to be prettified
47
     * @return string The prettified phone number
48
     */
49
    public function format($number)
50
    {
51
        $number = preg_replace("/[^0-9]/", "", $number);
52
        if (strlen($number) == 7) {
53
            return preg_replace("/([0-9]{3})([0-9]{4})/", "$1-$2", $number);
54
        } elseif (strlen($number) == 10) {
55
            return preg_replace("/([0-9]{3})([0-9]{3})([0-9]{4})/", "($1) $2-$3", $number);
56
        } elseif (strlen($number) == 11) {
57
            return preg_replace("/([0-9]{1})([0-9]{3})([0-9]{3})([0-9]{4})/", "$1 ($2) $3 $4", $number);
58
        } else {
59
            return $number;
60
        }
61
    }
62
63
    /**
64
     * Creates an HTML telephone link.
65
     *
66
     * If the $url is empty, $title is used instead.
67
     *
68
     * ### Options
69
     *
70
     * - `escape` Set to false to disable escaping of title and attributes.
71
     * - `escapeTitle` Set to false to disable escaping of title. Takes precedence
72
     *   over value of `escape`)
73
     *
74
     * @param string $title The content to be wrapped by `<a>` tags.
75
     * @param string|null $url string The phone number to link to.
76
     * @param array $options Array of options and HTML attributes.
77
     * @return string An `<a />` element.
78
     */
79
    public function link($title, $url = null, array $options = [])
80
    {
81
        $escapeTitle = true;
82
        if ($url !== null) {
83
            $url = $this->buildUrl($url);
84
        } else {
85
            $url = $this->buildUrl($title);
86
            $title = htmlspecialchars_decode($title, ENT_QUOTES);
87
            $title = h(urldecode($title));
88
            $escapeTitle = false;
89
        }
90
91
        if (isset($options['escapeTitle'])) {
92
            $escapeTitle = $options['escapeTitle'];
93
            unset($options['escapeTitle']);
94
        } elseif (isset($options['escape'])) {
95
            $escapeTitle = $options['escape'];
96
        }
97
98
        if ($escapeTitle === true) {
99
            $title = h($title);
100
        } elseif (is_string($escapeTitle)) {
101
            $title = htmlentities($title, ENT_QUOTES, $escapeTitle);
102
        }
103
104
        $templater = $this->templater();
105
106
        return $templater->format('phone', [
107
            'url' => $url,
108
            'attrs' => $templater->formatAttributes($options),
109
            'content' => $title
110
        ]);
111
    }
112
113
    /**
114
     * Builds a telephone link from a passed in string
115
     *
116
     * The string passed in can be a number, formatted number, or tel url
117
     *
118
     * @param string $url The number or tel url to use in the link
119
     * @return string rfc3966 formatted tel URL
120
     */
121
    public function buildUrl($url)
122
    {
123
        $number = preg_replace("/[^0-9]+/", "", $url);
124
125
        return "tel:+" . $number;
126
    }
127
}
128