Completed
Push — master ( 92e541...c9fde5 )
by Blake
05:50
created

PhoneHelper::phone()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 11
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 11
rs 9.4285
cc 3
eloc 8
nc 3
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
 * @since       1.0
13
 * @version     1.0
14
 * @license     http://www.opensource.org/licenses/mit-license.php MIT License
15
 */
16
namespace PhoneHelper\View\Helper;
17
18
use Cake\View\Helper;
19
use Cake\View\Helper\NumberHelper;
20
21
/**
22
 * PhoneHelper Main Class
23
 *
24
 * This class extends the NumberHelper built into CakePHP 3.x and adds the ability
25
 * to parse and prettify a US telephone number.
26
 */
27
class PhoneHelper extends NumberHelper
28
{
29
    /**
30
     * Prettify a phone number.
31
     *
32
     * This function can take either a 10 digit or 7 digit phone number and returns
33
     * a more human readable version.
34
     *
35
     * @param int $number The phone number to be prettified
36
     * @return string The prettified phone number
37
     */
38
    public function phone($number)
39
    {
40
        $number = preg_replace("/[^0-9]/", "", $number);
41
        if (strlen($number) == 7) {
42
            return preg_replace("/([0-9]{3})([0-9]{4})/", "$1-$2", $number);
43
        } elseif (strlen($number) == 10) {
44
            return preg_replace("/([0-9]{3})([0-9]{3})([0-9]{4})/", "($1) $2-$3", $number);
45
        } else {
46
            return $number;
47
        }
48
    }
49
}
50