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