Subversion-Projekte lars-tiefland.laravel_shop

Revision

Details | Letzte Änderung | Log anzeigen | RSS feed

Revision Autor Zeilennr. Zeile
148 lars 1
<?php
2
 
3
namespace Faker\Calculator;
4
 
5
class Iban
6
{
7
    /**
8
     * Generates IBAN Checksum
9
     *
10
     * @param string $iban
11
     *
12
     * @return string Checksum (numeric string)
13
     */
14
    public static function checksum($iban)
15
    {
16
        // Move first four digits to end and set checksum to '00'
17
        $checkString = substr($iban, 4) . substr($iban, 0, 2) . '00';
18
 
19
        // Replace all letters with their number equivalents
20
        $checkString = preg_replace_callback(
21
            '/[A-Z]/',
22
            static function (array $matches): string {
23
                return (string) self::alphaToNumber($matches[0]);
24
            },
25
            $checkString,
26
        );
27
 
28
        // Perform mod 97 and subtract from 98
29
        $checksum = 98 - self::mod97($checkString);
30
 
31
        return str_pad($checksum, 2, '0', STR_PAD_LEFT);
32
    }
33
 
34
    /**
35
     * Converts letter to number
36
     *
37
     * @param string $char
38
     *
39
     * @return int
40
     */
41
    public static function alphaToNumber($char)
42
    {
43
        return ord($char) - 55;
44
    }
45
 
46
    /**
47
     * Calculates mod97 on a numeric string
48
     *
49
     * @param string $number Numeric string
50
     *
51
     * @return int
52
     */
53
    public static function mod97($number)
54
    {
55
        $checksum = (int) $number[0];
56
 
57
        for ($i = 1, $size = strlen($number); $i < $size; ++$i) {
58
            $checksum = (10 * $checksum + (int) $number[$i]) % 97;
59
        }
60
 
61
        return $checksum;
62
    }
63
 
64
    /**
65
     * Checks whether an IBAN has a valid checksum
66
     *
67
     * @param string $iban
68
     *
69
     * @return bool
70
     */
71
    public static function isValid($iban)
72
    {
73
        return self::checksum($iban) === substr($iban, 2, 2);
74
    }
75
}