Test Failed
Push — master ( 36c5b6...af11e6 )
by Sebastian
04:51
created

getValidations()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 8
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
cc 1
eloc 6
c 2
b 0
f 0
nc 1
nop 0
dl 0
loc 8
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
use AppUtils\FileHelper;
15
use AppUtils\FileHelper_Exception;
16
17
/**
18
 * Mailcode command: show a phone number variable value, in E164 format.
19
 *
20
 * @package Mailcode
21
 * @subpackage Commands
22
 * @author Sebastian Mordziol <[email protected]>
23
 */
24
class Mailcode_Commands_Command_ShowPhone extends Mailcode_Commands_ShowBase
25
{
26
    public const VALIDATION_SOURCE_FORMAT_MISSING = 84001;
27
    public const VALIDATION_INVALID_COUNTRY = 84002;
28
29
    /**
30
     * Two-letter country code, uppercase.
31
     * @var string
32
     */
33
    protected $sourceFormat = '';
34
35
    /**
36
     * List of supported countries in the libphonenumber package.
37
     * NOTE: This can be extracted automatically using the provided PHP script.
38
     *
39
     * @var array<string,Mailcode_Commands_Command_ShowPhone_Number>
40
     *
41
     * @see /tools/extractPhoneCountries.php
42
     */
43
    protected static $supportedCountries = array();
44
45
    /**
46
     * @var bool
47
     */
48
    protected static $countriesLoaded = false;
49
50
    public function getName() : string
51
    {
52
        return 'showphone';
53
    }
54
55
    public function getLabel() : string
56
    {
57
        return t('Show phone number variable');
58
    }
59
60
    protected function getValidations() : array
61
    {
62
        return array(
63
            Mailcode_Interfaces_Commands_Validation_Variable::VALIDATION_NAME_VARIABLE,
64
            'source_format',
65
            'country_code',
66
            Mailcode_Interfaces_Commands_Validation_URLEncode::VALIDATION_NAME_URLENCODE,
67
            Mailcode_Interfaces_Commands_Validation_URLDecode::VALIDATION_NAME_URLDECODE
68
        );
69
    }
70
71
    protected function validateSyntax_source_format(): void
72
    {
73
        $val = $this->validator->createStringLiteral();
74
75
        if($val->isValid())
76
        {
77
            $this->sourceFormat = strtoupper($val->getToken()->getText());
78
            return;
79
        }
80
81
        $this->validationResult->makeError(
82
            t('No country code for the source phone format specified.'),
83
            self::VALIDATION_SOURCE_FORMAT_MISSING
84
        );
85
    }
86
87
    /**
88
     * Validates the specified country code to ensure it is one of the
89
     * supported countries.
90
     *
91
     * @throws FileHelper_Exception
92
     */
93
    protected function validateSyntax_country_code(): void
94
    {
95
        $countries = self::getSupportedCountries();
96
97
        if(isset($countries[$this->sourceFormat])) {
98
            return;
99
        }
100
101
        $this->validationResult->makeError(
102
            t('The country code %1$s is not supported for phone number conversion.', '<code>'.$this->sourceFormat.'</code>'),
103
            self::VALIDATION_INVALID_COUNTRY
104
        );
105
    }
106
107
    /**
108
     * Retrieves the list of countries supported for phone number conversions,
109
     * as an associative array with uppercase country code => number class pairs.
110
     *
111
     * @return array<string,Mailcode_Commands_Command_ShowPhone_Number>
112
     * @throws FileHelper_Exception
113
     */
114
    public static function getSupportedCountries() : array
115
    {
116
        if(self::$countriesLoaded) {
117
            return self::$supportedCountries;
118
        }
119
120
        self::$countriesLoaded = true;
121
122
        $data = self::loadData();
123
124
        foreach($data as $code => $def)
125
        {
126
            $code = (string)$code;
127
128
            self::$supportedCountries[$code] = new Mailcode_Commands_Command_ShowPhone_Number(
129
                $code,
130
                $def['label'],
131
                $def['local'],
132
                $def['international']
133
            );
134
        }
135
136
        return self::$supportedCountries;
137
    }
138
139
    /**
140
     * @return array<string,array{label:string,local:string,international:string}>
141
     * @throws FileHelper_Exception
142
     */
143
    private static function loadData() : array
144
    {
145
        $data = FileHelper::parseJSONFile(__DIR__.'/ShowPhone/numbers.json');
146
        $result = array();
147
148
        foreach($data as $idx => $def)
149
        {
150
            if(!is_array($def) || !is_string($idx))
151
            {
152
                continue;
153
            }
154
155
            $label = $def['label'] ?? '';
156
            $local = $def['local'] ?? '';
157
            $int = $def['international'] ?? '';
158
159
            $result[$idx] = array(
160
                'label' => (string)$label,
161
                'local' => (string)$local,
162
                'international' => (string)$int
163
            );
164
        }
165
166
        return $result;
167
    }
168
169
    /**
170
     * @return string Two-letter country code, uppercase.
171
     */
172
    public function getSourceFormat() : string
173
    {
174
        return $this->sourceFormat;
175
    }
176
}
177
178