Completed
Pull Request — master (#21)
by Hannes
09:11
created

BasicIdTrait::getSerialPostDelimiter()   A

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