Completed
Push — master ( a7385b...36e950 )
by Ronan
23s queued 11s
created

Imei::validate()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 15

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 15
rs 9.7666
c 0
b 0
f 0
cc 2
nc 2
nop 1
1
<?php
2
3
namespace IsoCodes;
4
5
/**
6
 * Class Imei for International Mobile Equipment Identity (IMEI)
7
 * IMEI is a number, usually unique,
8
 * to identify 3GPP and iDEN mobile phones, as well as some satellite phones.
9
 *
10
 * The IMEI (15 decimal digits: 14 digits plus a check digit)
11
 * or IMEISV (16 decimal digits: 14 digits plus two software version digits)
12
 * includes information on the origin, model, and serial number of the device.
13
 *
14
 * As of 2004, the format of the IMEI is AA-BBBBBB-CCCCCC-D,
15
 * although it may not always be displayed this way.
16
 *
17
 * The IMEISV does not have the Luhn check digit
18
 * but instead has two digits for the Software Version Number (SVN), making the format AA-BBBBBB-CCCCCC-EE
19
 *
20
 * @see https://en.wikipedia.org/wiki/International_Mobile_Equipment_Identity
21
 */
22
class Imei implements IsoCodeInterface
23
{
24
    const HYPHENS = ['‐', '-', ' ']; // regular dash, authentic hyphen (rare!) and space
25
26
    /**
27
     * Basic Luhn check.
28
     *
29
     * @param mixed $imei
30
     *
31
     * @return bool
32
     */
33
    public static function validate($imei)
34
    {
35
        $imei = Utils::unDecorate($imei, self::HYPHENS);
36
        $length = 15; // for IMEI only; IMEISV = EMEI+1, and not Luhn check
37
38
        // IMEISV?
39
        if ($length + 1 === strlen($imei)) {
40
            $expr = sprintf('/\\d{%d}/i', $length + 1);
41
42
            return boolval(preg_match($expr, $imei));
43
        }
44
45
        // IMEI?
46
        return Utils::Luhn($imei, $length, 2, 10, self::HYPHENS);
47
    }
48
}
49