Passed
Push — master ( 2e2cc3...b09f9f )
by Sebastian
04:26
created

getValidations()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 5
c 1
b 0
f 0
nc 1
nop 0
dl 0
loc 7
rs 10
1
<?php
2
/**
3
 * File containing the {@see Mailcode_Commands_Command_ShowPhone} class.
4
 *
5
 * @package Mailcode
6
 * @subpackage Commands
7
 * @see Mailcode_Commands_Command_ShowPhone
8
 */
9
10
declare(strict_types=1);
11
12
namespace Mailcode;
13
14
/**
15
 * Mailcode command: show a phone number variable value, in E164 format.
16
 *
17
 * @package Mailcode
18
 * @subpackage Commands
19
 * @author Sebastian Mordziol <[email protected]>
20
 */
21
class Mailcode_Commands_Command_ShowPhone extends Mailcode_Commands_ShowBase
22
{
23
    const VALIDATION_SOURCE_FORMAT_MISSING = 84001;
24
    const VALIDATION_INVALID_COUNTRY = 84002;
25
26
    /**
27
     * Two-letter country code, uppercase.
28
     * @var string
29
     */
30
    protected $sourceFormat = '';
31
32
    /**
33
     * List of supported countries in the libphonenumber package.
34
     * NOTE: This can be extracted automatically using the provided PHP script.
35
     *
36
     * @var array<string,string>
37
     *
38
     * @see https://github.com/google/libphonenumber/tree/master/resources/geocoding
39
     * @see /tools/extractPhoneCountries.php
40
     */
41
    protected static $supportedCountries = array(
42
        'AD' => 'Andorra',
43
        'AF' => 'Afghanistan',
44
        'AI' => 'Anguilla',
45
        'AL' => 'Albania',
46
        'AM' => 'Armenia',
47
        'AO' => 'Angola',
48
        'AR' => 'Argentina',
49
        'AT' => 'Austria',
50
        'AU' => 'Australia',
51
        'AW' => 'Aruba',
52
        'AZ' => 'Azerbaijan',
53
        'BB' => 'Barbados',
54
        'BD' => 'Bangladesh',
55
        'BE' => 'Belgium',
56
        'BG' => 'Bulgaria',
57
        'BH' => 'Bahrain',
58
        'BI' => 'Burundi',
59
        'BJ' => 'Benin',
60
        'BM' => 'Bermuda',
61
        'BN' => 'Brunei',
62
        'BO' => 'Bolivia',
63
        'BR' => 'Brazil',
64
        'BS' => 'Bahamas',
65
        'BT' => 'Bhutan',
66
        'BW' => 'Botswana',
67
        'BY' => 'Belarus',
68
        'BZ' => 'Belize',
69
        'CA' => 'Canada',
70
        'CH' => 'Switzerland',
71
        'CL' => 'Chile',
72
        'CM' => 'Cameroon',
73
        'CN' => 'China',
74
        'CO' => 'Colombia',
75
        'CU' => 'Cuba',
76
        'CY' => 'Cyprus',
77
        'CZ' => 'Czechia',
78
        'DE' => 'Germany',
79
        'DJ' => 'Djibouti',
80
        'DK' => 'Denmark',
81
        'DM' => 'Dominica',
82
        'DZ' => 'Algeria',
83
        'EC' => 'Ecuador',
84
        'EE' => 'Estonia',
85
        'EG' => 'Egypt',
86
        'ER' => 'Eritrea',
87
        'ES' => 'Spain',
88
        'ET' => 'Ethiopia',
89
        'FI' => 'Finland',
90
        'FJ' => 'Fiji',
91
        'FM' => 'Micronesia',
92
        'FR' => 'France',
93
        'GA' => 'Gabon',
94
        'GD' => 'Grenada',
95
        'GE' => 'Georgia',
96
        'GG' => 'Guernsey',
97
        'GH' => 'Ghana',
98
        'GI' => 'Gibraltar',
99
        'GL' => 'Greenland',
100
        'GM' => 'Gambia',
101
        'GN' => 'Guinea',
102
        'GP' => 'Guadeloupe',
103
        'GR' => 'Greece',
104
        'GT' => 'Guatemala',
105
        'GU' => 'Guam',
106
        'GY' => 'Guyana',
107
        'HN' => 'Honduras',
108
        'HR' => 'Croatia',
109
        'HT' => 'Haiti',
110
        'HU' => 'Hungary',
111
        'ID' => 'Indonesia',
112
        'IE' => 'Ireland',
113
        'IL' => 'Israel',
114
        'IN' => 'India',
115
        'IQ' => 'Iraq',
116
        'IR' => 'Iran',
117
        'IS' => 'Iceland',
118
        'IT' => 'Italy',
119
        'JE' => 'Jersey',
120
        'JM' => 'Jamaica',
121
        'JO' => 'Jordan',
122
        'JP' => 'Japan',
123
        'KE' => 'Kenya',
124
        'KG' => 'Kyrgyzstan',
125
        'KH' => 'Cambodia',
126
        'KI' => 'Kiribati',
127
        'KM' => 'Comoros',
128
        'KW' => 'Kuwait',
129
        'KZ' => 'Kazakhstan',
130
        'LA' => 'Laos',
131
        'LB' => 'Lebanon',
132
        'LI' => 'Liechtenstein',
133
        'LR' => 'Liberia',
134
        'LS' => 'Lesotho',
135
        'LT' => 'Lithuania',
136
        'LU' => 'Luxembourg',
137
        'LV' => 'Latvia',
138
        'LY' => 'Libya',
139
        'MA' => 'Morocco',
140
        'MC' => 'Monaco',
141
        'MD' => 'Moldova',
142
        'ME' => 'Montenegro',
143
        'MG' => 'Madagascar',
144
        'ML' => 'Mali',
145
        'MN' => 'Mongolia',
146
        'MO' => 'Macao',
147
        'MQ' => 'Martinique',
148
        'MR' => 'Mauritania',
149
        'MS' => 'Montserrat',
150
        'MT' => 'Malta',
151
        'MU' => 'Mauritius',
152
        'MV' => 'Maldives',
153
        'MW' => 'Malawi',
154
        'MX' => 'Mexico',
155
        'MY' => 'Malaysia',
156
        'MZ' => 'Mozambique',
157
        'NA' => 'Namibia',
158
        'NE' => 'Niger',
159
        'NG' => 'Nigeria',
160
        'NI' => 'Nicaragua',
161
        'NL' => 'Netherlands',
162
        'NO' => 'Norway',
163
        'NP' => 'Nepal',
164
        'NR' => 'Nauru',
165
        'NU' => 'Niue',
166
        'OM' => 'Oman',
167
        'PA' => 'Panama',
168
        'PE' => 'Peru',
169
        'PH' => 'Philippines',
170
        'PK' => 'Pakistan',
171
        'PL' => 'Poland',
172
        'PS' => 'Palestine',
173
        'PT' => 'Portugal',
174
        'PW' => 'Palau',
175
        'PY' => 'Paraguay',
176
        'QA' => 'Qatar',
177
        'RO' => 'Romania',
178
        'RS' => 'Serbia',
179
        'RU' => 'Russia',
180
        'RW' => 'Rwanda',
181
        'SC' => 'Seychelles',
182
        'SD' => 'Sudan',
183
        'SE' => 'Sweden',
184
        'SG' => 'Singapore',
185
        'SI' => 'Slovenia',
186
        'SK' => 'Slovakia',
187
        'SN' => 'Senegal',
188
        'SO' => 'Somalia',
189
        'SR' => 'Suriname',
190
        'SY' => 'Syria',
191
        'SZ' => 'Eswatini',
192
        'TD' => 'Chad',
193
        'TG' => 'Togo',
194
        'TH' => 'Thailand',
195
        'TJ' => 'Tajikistan',
196
        'TK' => 'Tokelau',
197
        'TM' => 'Turkmenistan',
198
        'TN' => 'Tunisia',
199
        'TO' => 'Tonga',
200
        'TR' => 'Turkey',
201
        'TV' => 'Tuvalu',
202
        'TW' => 'Taiwan',
203
        'TZ' => 'Tanzania',
204
        'UA' => 'Ukraine',
205
        'UG' => 'Uganda',
206
        'UY' => 'Uruguay',
207
        'UZ' => 'Uzbekistan',
208
        'VE' => 'Venezuela',
209
        'VN' => 'Vietnam',
210
        'VU' => 'Vanuatu',
211
        'WS' => 'Samoa',
212
        'XK' => 'Kosovo',
213
        'YE' => 'Yemen',
214
        'YT' => 'Mayotte',
215
        'ZM' => 'Zambia',
216
        'ZW' => 'Zimbabwe'
217
    );
218
219
    public function getName() : string
220
    {
221
        return 'showphone';
222
    }
223
224
    public function getLabel() : string
225
    {
226
        return t('Show phone number variable');
227
    }
228
229
    protected function getValidations() : array
230
    {
231
        return array(
232
            'variable',
233
            'source_format',
234
            'country_code',
235
            'urlencode'
236
        );
237
    }
238
239
    protected function validateSyntax_source_format(): void
240
    {
241
        $val = $this->validator->createStringLiteral();
242
243
        if($val->isValid())
244
        {
245
            $this->sourceFormat = strtoupper($val->getToken()->getText());
246
            return;
247
        }
248
249
        $this->validationResult->makeError(
250
            t('No country code for the source phone format specified.'),
251
            self::VALIDATION_SOURCE_FORMAT_MISSING
252
        );
253
    }
254
255
    /**
256
     * Validates the specified country code to ensure it is one of the
257
     * supported countries.
258
     */
259
    protected function validateSyntax_country_code(): void
260
    {
261
        if(isset(self::$supportedCountries[$this->sourceFormat])) {
262
            return;
263
        }
264
265
        $this->validationResult->makeError(
266
            t('The country code %1$s is not supported for phone number conversion.', '<code>'.$this->sourceFormat.'</code>'),
267
            self::VALIDATION_INVALID_COUNTRY
268
        );
269
    }
270
271
    /**
272
     * Retrieves the list of countries supported for phone number conversions,
273
     * as an associative array with uppercase country code => country name pairs.
274
     *
275
     * @return array<string,string>
276
     */
277
    public static function getSupportedCountries() : array
278
    {
279
        return self::$supportedCountries;
280
    }
281
282
    /**
283
     * @return string Two-letter country code, uppercase.
284
     */
285
    public function getSourceFormat() : string
286
    {
287
        return $this->sourceFormat;
288
    }
289
}
290
291