Completed
Push — new-version ( ed33c3...474545 )
by Jeroen
03:04
created

Gender::hasNote()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 0
dl 0
loc 3
rs 10
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 (!in_array($value, self::POSSIBLE_VALUES, true)) {
42
            throw PropertyException::forWrongValue($value, self::POSSIBLE_VALUES);
0 ignored issues
show
Bug introduced by
It seems like $value can also be of type null; however, parameter $value of JeroenDesloovere\VCard\E...eption::forWrongValue() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

42
            throw PropertyException::forWrongValue(/** @scrutinizer ignore-type */ $value, self::POSSIBLE_VALUES);
Loading history...
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;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this->note could return the type null which is incompatible with the type-hinted return string. Consider adding an additional type-check to rule them out.
Loading history...
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