NISS::getNISS()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 1
c 1
b 0
f 0
nc 1
nop 0
dl 0
loc 3
rs 10
1
<?php
2
3
namespace Helip\NISS;
4
5
use DateTime;
6
use Helip\NISS\Helpers\NISSExtractorHelper;
7
use Helip\NISS\Helpers\NISSValidatorHelper;
8
use InvalidArgumentException;
9
10
/**
11
 * NISS
12
 *
13
 * This class represents a Belgian National Identification Number (NISS).
14
 *
15
 * @version 1.0.0
16
 * @license LGPL-3.0-only
17
 * @author Pierre Hélin
18
 * @package NISS
19
 */
20
final class NISS
21
{
22
    public const TYPE_REGULAR = 'REGULAR';
23
    public const TYPE_DOB_UNKNOWN = 'UNKNOW DOB';
24
    public const TYPE_BIS = 'BIS';
25
    public const TYPE_TER = 'TER';
26
    public const TYPE_UNOFFICIAL = 'UNOFFICIAL'; // Special type for generated NISS
27
    public const TYPE_UNKNOWN = 'UNKNOWN';
28
29
    public const GENDER_MALE = 'M';
30
    public const GENDER_FEMALE = 'F';
31
    public const GENDER_UNKNOWN = '';
32
33
    /**
34
     * The NISS number.
35
     *
36
     * @var string
37
     */
38
    private $niss;
39
40
    /**
41
     * The control number.
42
     *
43
     * @var string
44
     */
45
    private string $controlNumber;
46
47
    /**
48
     * The order number.
49
     *
50
     * @var string
51
     */
52
    private string $orderNumber;
53
54
    /**
55
     * The birthdate.
56
     *
57
     * @var DateTime | null
58
     */
59
    private ?DateTime $birthdate;
60
61
    /**
62
     * The gender
63
     *
64
     * @var string | null
65
     */
66
    private ?string $gender;
67
68
    /**
69
     * The type of NISS number
70
     *
71
     * @var string
72
     */
73
    private string $type;
74
75
    /**
76
     * Constructor.
77
     *
78
     * @param string $niss
79
     * @throws InvalidArgumentException
80
     *
81
     */
82
    public function __construct(string $niss)
83
    {
84
        // clean the number, remove non numeric characters
85
        $niss = NISSValidatorHelper::clean($niss);
86
87
        // Check validity of the number
88
        try {
89
            NISSValidatorHelper::isValid($niss);
90
        } catch (InvalidArgumentException $e) {
91
            throw new InvalidArgumentException('The NISS number is not valid: ' . $niss . '. ' . $e->getMessage());
92
        }
93
94
        $this->extractDataFromNISS($niss);
95
    }
96
97
    /**
98
     * Extracts data from the NISS number and sets the properties.
99
     *
100
     * @param string $niss
101
     * @return void
102
     */
103
    private function extractDataFromNISS(string $niss): void
104
    {
105
        // niss
106
        $this->niss = $niss;
107
108
        // extracts last 2 digits
109
        $this->controlNumber = NISSExtractorHelper::getControlNumber($this->niss);
110
        $this->orderNumber = NISSExtractorHelper::getOrderNumber($this->niss);
111
        $this->type = NISSExtractorHelper::calculateType($this->niss);
112
        $this->gender = NISSExtractorHelper::calculateGender($this->niss);
113
        $this->birthdate = NISSExtractorHelper::calculateBirthdate($this->niss, $this->type, $this->gender);
114
    }
115
116
    /**
117
     * Returns the NISS number.
118
     *
119
     * @return string
120
     */
121
    public function getNISS(): string
122
    {
123
        return $this->niss;
124
    }
125
126
    /**
127
     * Returns the NISS number.
128
     *
129
     * @return string
130
     */
131
    public function getFormattedNISS(): string
132
    {
133
        return NISSExtractorHelper::format($this->niss);
134
    }
135
136
    /**
137
     * Returns the control number.
138
     *
139
     * @return string
140
     */
141
    public function getControlNumber(): string
142
    {
143
        return $this->controlNumber;
144
    }
145
146
    /**
147
     * Returns the order number.
148
     *
149
     * @return string
150
     */
151
    public function getOrderNumber(): string
152
    {
153
        return $this->orderNumber;
154
    }
155
156
    /**
157
     * Returns the birthdate or null if the birthdate is unknown.
158
     *
159
     * @return DateTime|null
160
     */
161
    public function getBirthdate(): ?DateTime
162
    {
163
        return $this->birthdate;
164
    }
165
166
    /**
167
     * Returns the gender
168
     *
169
     * @return string M, F or null
170
     */
171
    public function getGender(): ?string
172
    {
173
        return $this->gender;
174
    }
175
176
    /**
177
     * Returns the type of NISS number
178
     *
179
     * @return string default, bis or unofficial
180
     */
181
    public function getType(): string
182
    {
183
        return $this->type;
184
    }
185
}
186