Completed
Branch FET/reg-form-builder/extract-a... (6e8a58)
by
unknown
35:36 queued 25:38
created

Phone   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 109
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 0

Importance

Changes 0
Metric Value
dl 0
loc 109
rs 10
c 0
b 0
f 0
wmc 4
lcom 1
cbo 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 18 1
A regexPatterns() 0 4 1
A validTypeOptions() 0 6 2
1
<?php
2
3
namespace EventEspresso\core\services\form\meta\inputs;
4
5
class Phone
6
{
7
8
    /**
9
     * indicates that the HTML input type is 'tel'
10
     */
11
    public const INPUT_TYPE = 'tel';
12
13
    /**
14
     * indicates that the 'tel' input regex pattern is for a US formatted phone number, examples:
15
     *      ##########
16
     *      ###-###-####
17
     *      ### ### ####
18
     *      (###)-###-####
19
     *      (###) ###-####
20
     *
21
     * captures the intl code to the first group (+1) and the rest of the number to group 2
22
     *      +1 (###) ###-####
23
     */
24
    public const PATTERN_US = '(\+?\d{1,3})?[\ \-]?(\(?\d{3}\)?[\ \-]?\d{3}[\ \-]?\d{4})';
25
26
    /**
27
     * indicates that the 'tel' input regex pattern is for a UK formatted phone number, examples:
28
     *      (###) #### ####
29
     *      (####) ### ####
30
     *      (#####) ## ####
31
     *
32
     * captures the intl code to the first group (+44) and the rest of the number to group 2
33
     *      +44 (###) #### ####
34
     */
35
    public const PATTERN_UK = '(\+?44)?[\ ]?(\(?(?:(?:\d{3,5})|(?:\d{4} \d{2}))\)?[\-\ ]?\d{2,4}[\-\ ]?\d{2,4})';
36
37
    /**
38
     * indicates that the 'tel' input regex pattern is for a France formatted phone number, examples:
39
     *      0# ## ## ## ##
40
     *      0### ## ## ##
41
     *
42
     * captures the intl code to the first group (+33) and the rest of the number to group 2
43
     *      +33 # ## ## ## ##
44
     *      0033 # ## ## ## ##
45
     */
46
    public const PATTERN_FR = '((?:\+|00)33)?[\ \.\-]*((?:(?:\(0\)[\ \.\-]{0,3})?|0)[1-9](?:(?:[\ \.\-]?\d{2}){4}|\d{2}(?:[\ \.\-]?\d{3}){2}))';
47
48
    /**
49
     * indicates that the 'tel' input regex pattern is for a German formatted phone number, examples:
50
     *      (0##) ####-####
51
     *      (0###) ####-####
52
     *      (0####) ###-####
53
     *      (03####) ##-####
54
     *
55
     * captures the intl code to the first group (+49) and the rest of the number to group 2
56
     *      +49 (0##) ####-####
57
     */
58
    public const PATTERN_DE = '(\+?49)?[\ \.\-]?(\(?(?:[\d \-\)\–\/\(]+){6,}\)?(?:[\ \.\-–\/]?)(?:[\d]+))';
59
60
    /**
61
     * @var array
62
     */
63
    private $regex_patterns;
64
65
    /**
66
     * @var array
67
     */
68
    private $valid_type_options;
69
70
71
    /**
72
     * Phone constructor.
73
     */
74
    public function __construct()
75
    {
76
        $this->regex_patterns     = (array) apply_filters(
77
            'FHEE__EventEspresso_core_services_form_meta_inputs_Phone__regex_patterns',
78
            [
79
                'de_DE' => Phone::PATTERN_DE,
80
                'fr_FR' => Phone::PATTERN_FR,
81
                'en_UK' => Phone::PATTERN_UK,
82
                'en_US' => Phone::PATTERN_US,
83
            ]
84
        );
85
        $this->valid_type_options = apply_filters(
86
            'FHEE__EventEspresso_core_services_form_meta_inputs_Phone__valid_type_options',
87
            [
88
                Phone::INPUT_TYPE => esc_html__('Phone Number', 'event_espresso'),
89
            ]
90
        );
91
    }
92
93
94
    /**
95
     * @return array
96
     */
97
    public function regexPatterns(): array
98
    {
99
        return $this->regex_patterns;
100
    }
101
102
103
    /**
104
     * @param bool $constants_only
105
     * @return array
106
     */
107
    public function validTypeOptions(bool $constants_only = false): array
108
    {
109
        return $constants_only
110
            ? array_keys($this->valid_type_options)
111
            : $this->valid_type_options;
112
    }
113
}
114