Completed
Push — master ( 1554a9...07f312 )
by Ronan
34s queued 11s
created

Uid::validate()   B

Complexity

Conditions 7
Paths 12

Size

Total Lines 31

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 31
rs 8.4906
c 0
b 0
f 0
cc 7
nc 12
nop 1
1
<?php
2
3
namespace IsoCodes;
4
5
/**
6
 * Class Uid:
7
 * Switzerland Business Orgs VAT Number (UID) Validation
8
 * Business Identification Number (BID) or Unternehmens-Identifikationsnummer (UID).
9
 *
10
 * @see https://vatstack.com/articles/switzerland-vat-number-validation
11
 * @see https://www.estv.admin.ch/estv/fr/home/mehrwertsteuer/fachinformationen/steuerpflicht/unternehmens-identifikationsnummer--uid-.html
12
 * @see https://www.ech.ch/de/dokument/57be808d-9a03-4e9e-a2c5-65f08ca78e44
13
 */
14
class Uid implements IsoCodeInterface
15
{
16
    /**
17
     * @param mixed $uid - Swiss Business Identification Number
18
     *
19
     * @return bool
20
     */
21
    public static function validate($uid)
22
    {
23
        $multipliers = [5, 4, 3, 2, 7, 6, 5, 4];
24
        $result = 0;
25
        $uid = Utils::unDecorate(strval($uid), [' ', '.', '-', '—']);
26
27
        if (false !== strpos($uid, 'CHE', 0) || false !== strpos($uid, 'ADM', 0)) {
28
            $uid = substr(strval($uid), 3, 9);
29
        }
30
31
        $uid = substr($uid, 0, 9);
32
33
        if (9 != strlen($uid)) {
34
            return false;
35
        }
36
37
        foreach ($multipliers as $key => $multiplier) {
38
            if (!is_numeric($uid[$key])) {
39
                return false;
40
            }
41
            $result += $uid[$key] * $multiplier;
42
        }
43
44
        $rest = $result % 11;
45
46
        if (0 === $rest) {
47
            return true;
48
        }
49
50
        return intval($uid[8]) === 11 - $rest;
51
    }
52
}
53