Passed
Push — master ( 707ddf...aa8b2f )
by Sebastian
04:56
created

ConvertHelper_URLFinder_Detector_Tel   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 25
Duplicated Lines 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
wmc 4
eloc 8
c 2
b 0
f 0
dl 0
loc 25
rs 10

4 Methods

Rating   Name   Duplication   Size   Complexity  
A isValidFor() 0 3 1
A filterSubject() 0 3 1
A detect() 0 6 1
A getRunPosition() 0 3 1
1
<?php
2
/**
3
 * File containing the class {@see ConvertHelper_URLFinder_Detector_Tel}.
4
 *
5
 * @package AppUtils
6
 * @subpackage URLInfo
7
 * @see ConvertHelper_URLFinder_Detector_Tel
8
 */
9
10
declare(strict_types=1);
11
12
namespace AppUtils;
13
14
/**
15
 * Class used to detect `tel:` phone number links in a string.
16
 * Allows for the full RFC syntax, and automatically corrects
17
 * links that mistakenly use `tel://` notation.
18
 *
19
 * Matching examples:
20
 *
21
 * tel:+1(800)555-1212
22
 * tel:+18005551212,+18005553434;ext=123
23
 * tel:911;phone-context=+1
24
 * tel:123;phone-context=example.com
25
 *
26
 * @package AppUtils
27
 * @subpackage URLInfo
28
 * @author Sebastian Mordziol <[email protected]>
29
 *
30
 * @link https://www.ietf.org/rfc/rfc3966.txt
31
 * @link https://snipplr.com/view/11540/regex-for-tel-uris
32
 */
33
class ConvertHelper_URLFinder_Detector_Tel extends ConvertHelper_URLFinder_Detector
34
{
35
    const REGEX = '/^tel:((?:\+[\d().-]*\d[\d().-]*|[0-9A-F*#().-]*[0-9A-F*#][0-9A-F*#().-]*(?:;[a-z\d-]+(?:=(?:[a-z\d\[\]\/:&+$_!~*\'().-]|%[\dA-F]{2})+)?)*;phone-context=(?:\+[\d().-]*\d[\d().-]*|(?:[a-z0-9]\.|[a-z0-9][a-z0-9-]*[a-z0-9]\.)*(?:[a-z]|[a-z][a-z0-9-]*[a-z0-9])))(?:;[a-z\d-]+(?:=(?:[a-z\d\[\]\/:&+$_!~*\'().-]|%[\dA-F]{2})+)?)*(?:,(?:\+[\d().-]*\d[\d().-]*|[0-9A-F*#().-]*[0-9A-F*#][0-9A-F*#().-]*(?:;[a-z\d-]+(?:=(?:[a-z\d\[\]\/:&+$_!~*\'().-]|%[\dA-F]{2})+)?)*;phone-context=\+[\d().-]*\d[\d().-]*)(?:;[a-z\d-]+(?:=(?:[a-z\d\[\]\/:&+$_!~*\'().-]|%[\dA-F]{2})+)?)*)*)$/s';
36
37
    public function getRunPosition() : string
38
    {
39
        return self::RUN_BEFORE;
40
    }
41
42
    public function isValidFor(string $subject) : bool
43
    {
44
        return stristr($subject, 'tel:') !== false;
45
    }
46
47
    protected function filterSubject(string $subject) : string
48
    {
49
        return str_replace('tel://', 'tel:', $subject);
50
    }
51
52
    protected function detect(string $subject) : array
53
    {
54
        $matches = array();
55
        preg_match_all(self::REGEX, $subject, $matches, PREG_PATTERN_ORDER);
56
57
        return array_unique($matches[0]);
58
    }
59
}
60