Proyectos de Subversion Moodle

Rev

| Ultima modificación | Ver Log |

Rev Autor Línea Nro. Línea
1 efrain 1
<?php
2
// This file is part of Moodle - http://moodle.org/
3
//
4
// Moodle is free software: you can redistribute it and/or modify
5
// it under the terms of the GNU General Public License as published by
6
// the Free Software Foundation, either version 3 of the License, or
7
// (at your option) any later version.
8
//
9
// Moodle is distributed in the hope that it will be useful,
10
// but WITHOUT ANY WARRANTY; without even the implied warranty of
11
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12
// GNU General Public License for more details.
13
//
14
// You should have received a copy of the GNU General Public License
15
// along with Moodle.  If not, see <http://www.gnu.org/licenses/>.
16
 
17
namespace factor_sms;
18
 
19
/**
20
 * Helper class for shared sms gateway functions
21
 *
22
 * @package     factor_sms
23
 * @author      Alex Morris <alex.morris@catalyst.net.nz>
24
 * @copyright   Catalyst IT
25
 * @license     http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
26
 */
27
class helper {
28
 
29
    /**
30
     * This function internationalises a number to E.164 standard.
31
     * https://46elks.com/kb/e164
32
     *
33
     * @param string $phonenumber the phone number to format.
34
     * @return string the formatted phone number.
35
     */
36
    public static function format_number(string $phonenumber): string {
37
        // Remove all whitespace, dashes and brackets.
38
        $phonenumber = preg_replace('/[ \(\)-]/', '', $phonenumber);
39
 
40
        // Number is already in international format. Do nothing.
41
        if (str_starts_with ($phonenumber, '+')) {
42
            return $phonenumber;
43
        }
44
 
45
        // Strip leading 0 if found.
46
        if (str_starts_with ($phonenumber, '0')) {
47
            $phonenumber = substr($phonenumber, 1);
48
        }
49
 
50
        // Prepend country code.
51
        $countrycode = get_config('factor_sms', 'countrycode');
52
        $phonenumber = !empty($countrycode) ? '+' . $countrycode . $phonenumber : $phonenumber;
53
 
54
        return $phonenumber;
55
    }
56
 
57
    /**
58
     * Validate phone number with E.164 format. https://en.wikipedia.org/wiki/E.164
59
     *
60
     * @param string $phonenumber from the given user input
61
     * @return bool
62
     */
63
    public static function is_valid_phonenumber(string $phonenumber): bool {
64
        $phonenumber = self::format_number($phonenumber);
65
        return (preg_match("/^\+[1-9]\d{1,14}$/", $phonenumber)) ? true : false;
66
    }
67
}