Passed
Push — new-version ( 9ae918...68ed56 )
by Jeroen
05:52 queued 03:03
created

Gender::__construct()   A

Complexity

Conditions 4
Paths 3

Size

Total Lines 12
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 4
eloc 6
c 0
b 0
f 0
nc 3
nop 2
dl 0
loc 12
rs 9.2
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 (!in_array($value, self::POSSIBLE_VALUES, true)) {
42
            throw PropertyException::forWrongValue($value, self::POSSIBLE_VALUES);
43
        }
44
45
        if ($value === self::EMPTY && $note === null) {
46
            throw PropertyException::forEmptyProperty();
47
        }
48
49
        $this->value = $value;
50
        $this->note = $note;
51
    }
52
53
    public function __toString(): string
54
    {
55
        return $this->value;
56
    }
57
58
    public function getValue(): string
59
    {
60
        return $this->value;
61
    }
62
63
    public function getNote(): ?string
64
    {
65
        return $this->note;
66
    }
67
68
    public function hasNote(): bool
69
    {
70
        return $this->note !== null;
71
    }
72
73
    public static function empty(string $note = null): self
74
    {
75
        return new self(self::EMPTY, $note);
76
    }
77
78
    public function isEmpty(): bool
79
    {
80
        return $this->value === self::EMPTY;
81
    }
82
83
    public static function female(string $note = null): self
84
    {
85
        return new self(self::FEMALE, $note);
86
    }
87
88
    public function isFemale(): bool
89
    {
90
        return $this->value === self::FEMALE;
91
    }
92
93
    public static function male(string $note = null): self
94
    {
95
        return new self(self::MALE, $note);
96
    }
97
98
    public function isMale(): bool
99
    {
100
        return $this->value === self::MALE;
101
    }
102
103
    public static function none(string $note = null): self
104
    {
105
        return new self(self::NONE, $note);
106
    }
107
108
    public function isNone(): bool
109
    {
110
        return $this->value === self::NONE;
111
    }
112
113
    public static function other(string $note = null): self
114
    {
115
        return new self(self::OTHER, $note);
116
    }
117
118
    public function isOther(): bool
119
    {
120
        return $this->value === self::OTHER;
121
    }
122
123
    public static function unknown(string $note = null): self
124
    {
125
        return new self(self::UNKNOWN, $note);
126
    }
127
128
    public function isUnknown(): bool
129
    {
130
        return $this->value === self::UNKNOWN;
131
    }
132
133
    public function getFormatter(): NodeFormatterInterface
134
    {
135
        return new GenderFormatter($this);
136
    }
137
138
    public static function getNode(): string
139
    {
140
        return 'GENDER';
141
    }
142
143
    public static function getParser(): NodeParserInterface
144
    {
145
        return new GenderParser();
146
    }
147
148
    public function isAllowedMultipleTimes(): bool
149
    {
150
        return false;
151
    }
152
}
153