Proyectos de Subversion Moodle

Rev

| Ultima modificación | Ver Log |

Rev Autor Línea Nro. Línea
1441 ariadna 1
<?php
2
 
3
declare(strict_types=1);
4
 
5
namespace libphonenumber;
6
 
7
/**
8
 * Class RegexBasedMatcher
9
 * @package libphonenumber
10
 * @internal
11
 */
12
class RegexBasedMatcher implements MatcherAPIInterface
13
{
14
    public static function create(): static
15
    {
16
        return new static();
17
    }
18
 
19
    // Keep PHPStan happy (Unsafe usage of new static())
20
    final public function __construct() {}
21
 
22
    /**
23
     * Returns whether the given national number (a string containing only decimal digits) matches
24
     * the national number pattern defined in the given {@code PhoneNumberDesc} message.
25
     */
26
    public function matchNationalNumber(string $number, PhoneNumberDesc $numberDesc, bool $allowPrefixMatch): bool
27
    {
28
        $nationalNumberPattern = $numberDesc->getNationalNumberPattern();
29
 
30
        // We don't want to consider it a prefix match when matching non-empty input against an empty
31
        // pattern
32
 
33
        if ($nationalNumberPattern === '') {
34
            return false;
35
        }
36
 
37
        return $this->match($number, $nationalNumberPattern, $allowPrefixMatch);
38
    }
39
 
40
    private function match(string $number, string $pattern, bool $allowPrefixMatch): bool
41
    {
42
        $matcher = new Matcher($pattern, $number);
43
 
44
        if (!$matcher->lookingAt()) {
45
            return false;
46
        }
47
 
48
        return $matcher->matches() ? true : $allowPrefixMatch;
49
    }
50
}