Completed
Push — locale ( b2b284...e3784a )
by Joshua
41:04 queued 24:21
created

PhonePrefixMap::lookupKey()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 16
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 8
CRAP Score 4.0218

Importance

Changes 1
Bugs 0 Features 0
Metric Value
dl 0
loc 16
rs 9.2
c 1
b 0
f 0
ccs 8
cts 9
cp 0.8889
cc 4
eloc 8
nc 4
nop 1
crap 4.0218
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 20
    public function __construct($map)
24
    {
25 20
        $this->phonePrefixMapStorage = $map;
26 20
        $this->phoneUtil = PhoneNumberUtil::getInstance();
27 20
    }
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 22
    public function lookup(PhoneNumber $number)
39
    {
40 22
        $phonePrefix = $number->getCountryCode() . $this->phoneUtil->getNationalSignificantNumber($number);
41
42 22
        return $this->lookupKey($phonePrefix);
43
    }
44
45 30
    public function lookupKey($key)
46
    {
47 30
        if (count($this->phonePrefixMapStorage) == 0) {
48
            return null;
49
        }
50
51 30
        while (strlen($key) > 0) {
52 30
            if (array_key_exists($key, $this->phonePrefixMapStorage)) {
53 23
                return $this->phonePrefixMapStorage[$key];
54
            }
55
56 29
            $key = substr($key, 0, -1);
57 29
        }
58
59 10
        return null;
60
    }
61
}
62