Completed
Push — master ( 435061...8e7c43 )
by Ronan
01:16
created

Abn   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 27
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 1

Importance

Changes 0
Metric Value
wmc 4
lcom 0
cbo 1
dl 0
loc 27
rs 10
c 0
b 0
f 0

1 Method

Rating   Name   Duplication   Size   Complexity  
A validate() 0 18 4
1
<?php
2
3
namespace IsoCodes;
4
5
/**
6
 * Class Abn, Australian Business Number
7
 *
8
 * @link https://en.wikipedia.org/wiki/Australian_Business_Number
9
 * @link https://abr.business.gov.au/
10
 */
11
class Abn implements IsoCodeInterface
12
{
13
    /**
14
     * Validate an Australian Business Number (ABN)
15
     * @author Paul Ferrett, 2009 (http://www.paulferrett.com)
16
     * @param string $abn
17
     * @return bool
18
     */
19
    public static function validate($abn)
20
    {
21
        $weights = [10, 1, 3, 5, 7, 9, 11, 13, 15, 17, 19];
22
        $sum = 0;
23
        $abn = preg_replace('/[^0-9]/', '', $abn);
24
        $abn = Utils::unDecorate($abn, [' ']);
25
        if (mb_strlen($abn) !== 11) {
26
            return false;
27
        }
28
        $abn[0] = ((int)$abn[0] - 1); // Subtract one from first digit
29
        foreach (str_split($abn) as $key => $digit) {
30
            $sum += ($digit * $weights[$key]);
31
        }
32
        if (($sum % 89) != 0) {
33
            return false;
34
        }
35
        return true;
36
    }
37
}
38