Completed
Push — master ( 600bb9...10880d )
by Blake
05:18
created

PhoneHelper::buildUrl()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 6
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 3
nc 1
nop 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
        } else {
57
            return $number;
58
        }
59
    }
60
61
    /**
62
     * Creates an HTML telephone link.
63
     *
64
     * If the $url is empty, $title is used instead.
65
     *
66
     * ### Options
67
     *
68
     * - `escape` Set to false to disable escaping of title and attributes.
69
     * - `escapeTitle` Set to false to disable escaping of title. Takes precedence
70
     *   over value of `escape`)
71
     *
72
     * @param string $title The content to be wrapped by `<a>` tags.
73
     * @param string|null $url string The phone number to link to.
74
     * @param array $options Array of options and HTML attributes.
75
     * @return string An `<a />` element.
76
     */
77
    public function link($title, $url = null, array $options = [])
78
    {
79
        $escapeTitle = true;
80
        if ($url !== null) {
81
            $url = $this->buildUrl($url);
82
        } else {
83
            $url = $this->buildUrl($title);
84
            $title = htmlspecialchars_decode($title, ENT_QUOTES);
85
            $title = h(urldecode($title));
86
            $escapeTitle = false;
87
        }
88
89
        if (isset($options['escapeTitle'])) {
90
            $escapeTitle = $options['escapeTitle'];
91
            unset($options['escapeTitle']);
92
        } elseif (isset($options['escape'])) {
93
            $escapeTitle = $options['escape'];
94
        }
95
96
        if ($escapeTitle === true) {
97
            $title = h($title);
98
        } elseif (is_string($escapeTitle)) {
99
            $title = htmlentities($title, ENT_QUOTES, $escapeTitle);
100
        }
101
102
        $templater = $this->templater();
103
104
        return $templater->format('phone', [
105
            'url' => $url,
106
            'attrs' => $templater->formatAttributes($options),
107
            'content' => $title
108
        ]);
109
    }
110
111
    /**
112
     * Builds a telephone link from a passed in string
113
     *
114
     * The string passed in can be a number, formatted number, or tel url
115
     *
116
     * @param string $url The number or tel url to use in the link
117
     * @return string rfc3966 formatted tel URL
118
     */
119
    public function buildUrl($url)
120
    {
121
        $number = preg_replace("/[^0-9]+/", "", $url);
122
123
        return "tel:+" . $number;
124
    }
125
}
126