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\Provider\en_SG;
4
 
5
use Faker\Provider\DateTime;
6
 
7
class Person extends \Faker\Provider\Person
8
{
9
    /**
10
     * National Registration Identity Card number
11
     *
12
     * @param \DateTime|null $birthDate birth date
13
     *
14
     * @return string in format S1234567D or T1234567J
15
     */
16
    public static function nric(?\DateTime $birthDate = null): string
17
    {
18
        return self::singaporeId($birthDate, false);
19
    }
20
 
21
    /**
22
     * Foreign Identification Number
23
     *
24
     * @param \DateTime|null $issueDate issue date
25
     *
26
     * @return string in format F1234567N or G1234567X
27
     */
28
    public static function fin(?\DateTime $issueDate = null): string
29
    {
30
        return self::singaporeId($issueDate, true);
31
    }
32
 
33
    /**
34
     * Singapore NRIC (citizens) or FIN (foreigners) number
35
     *
36
     * @param \DateTime|null $issueDate birth/issue date
37
     * @param bool           $foreigner whether a person is foreigner or citizen
38
     *
39
     * @return string in format S1234567D, T1234567J, F1234567N or G1234567X
40
     */
41
    public static function singaporeId(?\DateTime $issueDate = null, bool $foreigner = false): string
42
    {
43
        if ($issueDate === null) {
44
            $issueDate = DateTime::dateTimeThisCentury();
45
        }
46
 
47
        $weights = [2, 7, 6, 5, 4, 3, 2];
48
        $result = '';
49
 
50
        if ($foreigner) {
51
            $prefix = ($issueDate < new \DateTime('2000-01-01')) ? 'F' : 'G';
52
            $checksumArr = ['X', 'W', 'U', 'T', 'R', 'Q', 'P', 'N', 'M', 'L', 'K'];
53
        } else {
54
            $prefix = ($issueDate < new \DateTime('2000-01-01')) ? 'S' : 'T';
55
            // NRICs before 1968 did not contain YOB
56
            $result .= ($issueDate < new \DateTime('1968-01-01')) ? static::randomElement(['00', '01']) : $issueDate->format('y');
57
            $checksumArr = ['J', 'Z', 'I', 'H', 'G', 'F', 'E', 'D', 'C', 'B', 'A'];
58
        }
59
 
60
        $length = count($weights);
61
 
62
        for ($i = strlen($result); $i < $length; ++$i) {
63
            $result .= static::randomDigit();
64
        }
65
 
66
        $checksum = in_array($prefix, ['G', 'T'], true) ? 4 : 0;
67
 
68
        for ($i = 0; $i < $length; ++$i) {
69
            $checksum += (int) $result[$i] * $weights[$i];
70
        }
71
 
72
        return $prefix . $result . $checksumArr[$checksum % 11];
73
    }
74
}