1
|
|
|
<?php |
2
|
|
|
declare(strict_types = 1); |
3
|
|
|
|
4
|
|
|
namespace SLWDC\NICParser; |
5
|
|
|
|
6
|
|
|
|
7
|
|
|
use SLWDC\NICParser\Exception\InvalidArgumentException; |
8
|
|
|
|
9
|
|
|
class Builder { |
10
|
|
|
|
11
|
|
|
/** |
12
|
|
|
* @var \DateTime |
13
|
|
|
*/ |
14
|
|
|
private $birthday; |
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* @var string |
18
|
|
|
*/ |
19
|
|
|
private $gender; |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* @var int |
23
|
|
|
*/ |
24
|
|
|
private $serial_number; |
25
|
|
|
|
26
|
|
|
public function setParser(Parser $parser) { |
27
|
|
|
$this->birthday = $parser->getBirthday(); |
28
|
|
|
$this->gender = $parser->getGender(); |
29
|
|
|
$this->serial_number = $parser->getSerialNumber(); |
30
|
|
|
} |
31
|
|
|
|
32
|
|
|
public function setBirthday(\DateTime $date) { |
33
|
|
|
$this->birthday = clone $date; |
34
|
|
|
return $this; |
35
|
|
|
} |
36
|
|
|
|
37
|
|
|
public function setGender(string $gender = 'M') { |
38
|
|
|
if ($gender === 'M' || $gender === 'F') { |
39
|
|
|
$this->gender = $gender; |
40
|
|
|
return $this; |
41
|
|
|
} |
42
|
|
|
throw new InvalidArgumentException('Unknown gender. Allowed values are: "M" and "F'); |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
public function setSerialNumber(int $serial_number) { |
46
|
|
|
$this->serial_number = $serial_number; |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
public function getNumber(): string { |
50
|
|
|
$this->checkBuilderFields(); |
51
|
|
|
|
52
|
|
|
$year = $this->birthday->format('Y'); |
53
|
|
|
$start_date = (new \DateTime())->setDate((int) $year, 1, 1)->setTime(0, 0); |
54
|
|
|
$birth_date_count = (int) $this->birthday->diff($start_date)->format('%a'); |
55
|
|
|
|
56
|
|
|
++$birth_date_count; |
57
|
|
|
|
58
|
|
|
if ($this->gender === 'F') { |
59
|
|
|
$birth_date_count += 500; |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
$serial = $this->serial_number; |
63
|
|
|
return "{$year}{$birth_date_count}{$serial}"; |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
public function getParser(): Parser { |
67
|
|
|
$number = $this->getNumber(); |
68
|
|
|
return new Parser($number); |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
public function checkBuilderFields() { |
72
|
|
|
if (!$this->birthday) { |
73
|
|
|
throw new \BadMethodCallException('Attempting to build ID number without a valid birthday set.'); |
74
|
|
|
} |
75
|
|
|
if (!$this->gender) { |
76
|
|
|
throw new \BadMethodCallException('Attempting to build ID number without a valid gender set.'); |
77
|
|
|
} |
78
|
|
|
if (!$this->serial_number) { |
79
|
|
|
throw new \BadMethodCallException('Attempting to build ID number without a valid serial number set.'); |
80
|
|
|
} |
81
|
|
|
} |
82
|
|
|
} |
83
|
|
|
|