BasicIdTrait::getBirthDate()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace byrokrat\id\Helper;
6
7
use byrokrat\id\IdInterface;
8
use byrokrat\id\Counties;
9
use byrokrat\id\LegalForms;
10
use byrokrat\id\Sexes;
11
use byrokrat\id\Exception\DateNotSupportedException;
12
use byrokrat\id\Formatter\Formatter;
13
14
/**
15
 * Basic implementation of IdInterface
16
 */
17
trait BasicIdTrait
18
{
19
    /**
20
     * @var string
21
     */
22
    protected $serialPre = '000000';
23
24
    /**
25
     * @var string
26
     */
27
    protected $serialPost = '000';
28
29
    /**
30
     * @var string Date and control string delimiter (- or +)
31
     */
32
    protected $delimiter = '-';
33
34
    /**
35
     * @var string
36
     */
37
    protected $checkDigit = '0';
38
39
    public function getId(): string
40
    {
41
        return $this->format(IdInterface::FORMAT_10_DIGITS);
42
    }
43
44
    public function __tostring(): string
45
    {
46
        return $this->getId();
47
    }
48
49
    public function format(string $format): string
50
    {
51
        return (new Formatter($format))->format($this);
52
    }
53
54
    public function getSerialPreDelimiter(): string
55
    {
56
        return $this->serialPre;
57
    }
58
59
    public function getSerialPostDelimiter(): string
60
    {
61
        return $this->serialPost;
62
    }
63
64
    public function getDelimiter(): string
65
    {
66
        return $this->delimiter;
67
    }
68
69
    public function getCheckDigit(): string
70
    {
71
        return $this->checkDigit;
72
    }
73
74
    public function getBirthDate(): \DateTimeImmutable
75
    {
76
        throw new DateNotSupportedException("Trying to access date on id type where it is not supported");
77
    }
78
79
    public function getAge(\DateTimeInterface $atDate = null): int
80
    {
81
        $interval = $this->getBirthDate()->diff($atDate ?: new \DateTime());
82
83
        if (!$interval) {
84
            throw new \LogicException('Unable to create age interval');
85
        }
86
87
        return (int)$interval->format('%y');
88
    }
89
90
    public function getCentury(): string
91
    {
92
        return substr($this->getBirthDate()->format('Y'), 0, 2);
93
    }
94
95
    public function getSex(): string
96
    {
97
        return Sexes::SEX_UNDEFINED;
98
    }
99
100
    public function isMale(): bool
101
    {
102
        return $this->getSex() == Sexes::SEX_MALE;
103
    }
104
105
    public function isFemale(): bool
106
    {
107
        return $this->getSex() == Sexes::SEX_FEMALE;
108
    }
109
110
    public function isSexOther(): bool
111
    {
112
        return $this->getSex() == Sexes::SEX_OTHER;
113
    }
114
115
    public function isSexUndefined(): bool
116
    {
117
        return $this->getSex() == Sexes::SEX_UNDEFINED;
118
    }
119
120
    public function getBirthCounty(): string
121
    {
122
        return Counties::COUNTY_UNDEFINED;
123
    }
124
125
    public function getLegalForm(): string
126
    {
127
        return LegalForms::LEGAL_FORM_UNDEFINED;
128
    }
129
130
    public function isLegalFormUndefined(): bool
131
    {
132
        return $this->getLegalForm() == LegalForms::LEGAL_FORM_UNDEFINED;
133
    }
134
135
    public function isStateOrParish(): bool
136
    {
137
        return $this->getLegalForm() == LegalForms::LEGAL_FORM_STATE_PARISH;
138
    }
139
140
    public function isIncorporated(): bool
141
    {
142
        return $this->getLegalForm() == LegalForms::LEGAL_FORM_INCORPORATED;
143
    }
144
145
    public function isPartnership(): bool
146
    {
147
        return $this->getLegalForm() == LegalForms::LEGAL_FORM_PARTNERSHIP;
148
    }
149
150
    public function isAssociation(): bool
151
    {
152
        return $this->getLegalForm() == LegalForms::LEGAL_FORM_ASSOCIATION;
153
    }
154
155
    public function isNonProfit(): bool
156
    {
157
        return $this->getLegalForm() == LegalForms::LEGAL_FORM_NONPROFIT;
158
    }
159
160
    public function isTradingCompany(): bool
161
    {
162
        return $this->getLegalForm() == LegalForms::LEGAL_FORM_TRADING;
163
    }
164
}
165