Completed
Push — upstream-8.3.0 ( 6d22b3...36447c )
by Joshua
29:18 queued 18:23
created

PhonePrefixMap::lookup()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 6
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 3
nc 1
nop 1
ccs 3
cts 3
cp 1
crap 1
1
<?php
2
3
namespace libphonenumber\prefixmapper;
4
5
use libphonenumber\PhoneNumber;
6
use libphonenumber\PhoneNumberUtil;
7
8
/**
9
 * A utility that maps phone number prefixes to a description string,
10
 * which may be, for example, the geographical area the prefix covers.
11
 *
12
 * Class PhonePrefixMap
13
 * @package libphonenumber\prefixmapper
14
 */
15
class PhonePrefixMap
16
{
17
    protected $phonePrefixMapStorage = array();
18
    /**
19
     * @var PhoneNumberUtil
20
     */
21
    protected $phoneUtil;
22
23 19
    public function __construct($map)
24
    {
25 19
        $this->phonePrefixMapStorage = $map;
26 19
        $this->phoneUtil = PhoneNumberUtil::getInstance();
27 19
    }
28
29
    /**
30
     * Returns the description of the {@code $number}. This method distinguishes the case of an invalid
31
     * prefix and a prefix for which the name is not available in the current language. If the
32
     * description is not available in the current language an empty string is returned. If no
33
     * description was found for the provided number, null is returned.
34
     *
35
     * @param PhoneNumber $number The phone number to look up
36
     * @return string|null the description of the number
37
     */
38 21
    public function lookup(PhoneNumber $number)
39
    {
40 21
        $phonePrefix = $number->getCountryCode() . $this->phoneUtil->getNationalSignificantNumber($number);
41
42 21
        return $this->lookupKey($phonePrefix);
43
    }
44
45 29
    public function lookupKey($key)
46
    {
47 29
        if (count($this->phonePrefixMapStorage) == 0) {
48
            return null;
49
        }
50
51 29
        while (strlen($key) > 0) {
52 29
            if (array_key_exists($key, $this->phonePrefixMapStorage)) {
53 23
                return $this->phonePrefixMapStorage[$key];
54
            }
55
56 28
            $key = substr($key, 0, -1);
57 28
        }
58
59 10
        return null;
60
    }
61
}
62