Completed
Push — master ( f6ddbe...88b3ce )
by Ronan
06:26 queued 10s
created

Siren::validate()   B

Complexity

Conditions 7
Paths 7

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 7
nop 2
1
<?php
2
3
namespace IsoCodes;
4
5
/**
6
 * Siren
7
 * En France, le SIREN (Système d’Identification du Répertoire des Entreprises)
8
 * est un code Insee unique qui sert à identifier une entreprise française.
9
 * Il existe au sein d'un répertoire géré par l'Insee : SIRENE.
10
 */
11
class Siren implements IsoCodeInterface
12
{
13
    /**
14
     * SIREN validator.
15
     *
16
     * @param string $insee
17
     * @param int    $length
18
     *
19
     * @author ronan.guilloux
20
     *
21
     * @link   http://fr.wikipedia.org/wiki/SIREN
22
     *
23
     * @return bool
24
     */
25
    public static function validate($insee, $length = 9)
26
    {
27
        if (!is_numeric($insee)) {
28
            return false;
29
        }
30
31
        if (strlen($insee) != $length) {
32
            return false;
33
        }
34
35
        /**
36
         * La poste support (French mail company)
37
         * @link https://fr.wikipedia.org/wiki/SIRET#Calcul_et_validit%C3%A9_d'un_num%C3%A9ro_SIRET
38
         * @link https://blog.pagesd.info/2012/09/05/verifier-numero-siret-poste/
39
         */
40
        $laPosteSiren = '356000000';
41
        if(strpos($insee, $laPosteSiren) === 0){
42
            return $laPosteSiren === (string) $insee ? true : array_sum(str_split($insee)) % 5 === 0;
43
        }
44
45
        $sum = 0;
46
        for ($i = 0; $i < $length; ++$i) {
47
            $indice = ($length - $i);
48
            $tmp = (2 - ($indice % 2)) * $insee[$i];
49
            if ($tmp >= 10) {
50
                $tmp -= 9;
51
            }
52
            $sum += $tmp;
53
        }
54
        return ($sum % 10) == 0;
55
    }
56
}
57