Total Complexity | 7 |
Total Lines | 58 |
Duplicated Lines | 0 % |
Changes | 1 | ||
Bugs | 0 | Features | 0 |
1 | <?php |
||
15 | class LibPhoneNumberController extends Controller |
||
16 | { |
||
17 | /** |
||
18 | * @var array<string> |
||
19 | */ |
||
20 | private static $allowed_actions = [ |
||
|
|||
21 | 'validate', |
||
22 | 'format', |
||
23 | ]; |
||
24 | |||
25 | /** |
||
26 | * @param HTTPRequest $request |
||
27 | * @return int |
||
28 | */ |
||
29 | public function validate(HTTPRequest $request) |
||
30 | { |
||
31 | $fieldname = $request->getVar('field') ? $request->getVar('field') : 'number'; |
||
32 | $rawNumber = $request->getVar($fieldname); |
||
33 | // as a fallback solution, we get the first $_GET parameter |
||
34 | if (!$rawNumber) { |
||
35 | $qs = array_values($_GET); |
||
36 | array_shift($qs); //remove first that is always url |
||
37 | $rawNumber = array_shift($qs); |
||
38 | } |
||
39 | $country = $request->getVar('country'); |
||
40 | try { |
||
41 | $result = PhoneHelper::validatePhoneNumber( |
||
42 | $rawNumber, |
||
43 | $country |
||
44 | ); |
||
45 | if ($result) { |
||
46 | return 1; |
||
47 | } |
||
48 | } catch (Exception $e) { |
||
49 | // Not valid |
||
50 | } |
||
51 | $this->getResponse()->setStatusCode(400); |
||
52 | return 0; |
||
53 | } |
||
54 | |||
55 | /** |
||
56 | * @param HTTPRequest $request |
||
57 | * @return string The formatted number |
||
58 | */ |
||
59 | public function format(HTTPRequest $request) |
||
73 | } |
||
74 | } |
||
76 |