|
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
|
|
|
|