Passed
Push — new-version ( f1c08c...9ae918 )
by Jeroen
04:22 queued 01:51
created

Gender::__construct()   B

Complexity

Conditions 5
Paths 6

Size

Total Lines 16
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 5
eloc 8
nc 6
nop 2
dl 0
loc 16
rs 8.8571
c 0
b 0
f 0
1
<?php
2
3
namespace JeroenDesloovere\VCard\Property;
4
5
use JeroenDesloovere\VCard\Exception\PropertyException;
6
use JeroenDesloovere\VCard\Formatter\Property\GenderFormatter;
7
use JeroenDesloovere\VCard\Formatter\Property\NodeFormatterInterface;
8
use JeroenDesloovere\VCard\Parser\Property\GenderParser;
9
use JeroenDesloovere\VCard\Parser\Property\NodeParserInterface;
10
11
final class Gender implements PropertyInterface, NodeInterface
12
{
13
    protected const EMPTY = '';
14
    protected const FEMALE = 'F';
15
    protected const MALE = 'M';
16
    protected const NONE = 'N';
17
    protected const OTHER = 'O';
18
    protected const UNKNOWN = 'U';
19
20
    public const POSSIBLE_VALUES = [
21
        self::EMPTY,
22
        self::FEMALE,
23
        self::MALE,
24
        self::NONE,
25
        self::OTHER,
26
        self::UNKNOWN,
27
    ];
28
29
    /**
30
     * @var string
31
     */
32
    private $value;
33
34
    /**
35
     * @var null|string
36
     */
37
    private $note;
38
39
    public function __construct(?string $value = '', ?string $note = null)
40
    {
41
        if ($value === null) {
42
            $value = '';
43
        }
44
45
        if (!in_array($value, self::POSSIBLE_VALUES, true)) {
46
            throw PropertyException::forWrongValue($value, self::POSSIBLE_VALUES);
47
        }
48
49
        if ($value === self::EMPTY && $note === null) {
50
            throw PropertyException::forEmptyProperty();
51
        }
52
53
        $this->value = $value;
54
        $this->note = $note;
55
    }
56
57
    public function __toString(): string
58
    {
59
        return $this->value;
60
    }
61
62
    public function getValue(): string
63
    {
64
        return $this->value;
65
    }
66
67
    public function getNote(): ?string
68
    {
69
        return $this->note;
70
    }
71
72
    public function hasNote(): bool
73
    {
74
        return $this->note !== null;
75
    }
76
77
    public static function empty(string $note = null): self
78
    {
79
        return new self(self::EMPTY, $note);
80
    }
81
82
    public function isEmpty(): bool
83
    {
84
        return $this->value === self::EMPTY;
85
    }
86
87
    public static function female(string $note = null): self
88
    {
89
        return new self(self::FEMALE, $note);
90
    }
91
92
    public function isFemale(): bool
93
    {
94
        return $this->value === self::FEMALE;
95
    }
96
97
    public static function male(string $note = null): self
98
    {
99
        return new self(self::MALE, $note);
100
    }
101
102
    public function isMale(): bool
103
    {
104
        return $this->value === self::MALE;
105
    }
106
107
    public static function none(string $note = null): self
108
    {
109
        return new self(self::NONE, $note);
110
    }
111
112
    public function isNone(): bool
113
    {
114
        return $this->value === self::NONE;
115
    }
116
117
    public static function other(string $note = null): self
118
    {
119
        return new self(self::OTHER, $note);
120
    }
121
122
    public function isOther(): bool
123
    {
124
        return $this->value === self::OTHER;
125
    }
126
127
    public static function unknown(string $note = null): self
128
    {
129
        return new self(self::UNKNOWN, $note);
130
    }
131
132
    public function isUnknown(): bool
133
    {
134
        return $this->value === self::UNKNOWN;
135
    }
136
137
    public function getFormatter(): NodeFormatterInterface
138
    {
139
        return new GenderFormatter($this);
140
    }
141
142
    public static function getNode(): string
143
    {
144
        return 'GENDER';
145
    }
146
147
    public static function getParser(): NodeParserInterface
148
    {
149
        return new GenderParser();
150
    }
151
152
    public function isAllowedMultipleTimes(): bool
153
    {
154
        return false;
155
    }
156
}
157