Completed
Branch 2.0.0-dev (4220a4)
by Jeroen
03:47
created

Value::boolean()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 1 Features 1
Metric Value
eloc 1
c 1
b 1
f 1
dl 0
loc 3
rs 10
cc 1
nc 1
nop 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace JeroenDesloovere\VCard\Property\Parameter;
6
7
use JeroenDesloovere\VCard\Exception\PropertyParameterException;
8
use JeroenDesloovere\VCard\Formatter\Property\NodeFormatterInterface;
9
use JeroenDesloovere\VCard\Formatter\Property\Parameter\ValueFormatter;
10
use JeroenDesloovere\VCard\Parser\Property\NodeParserInterface;
11
use JeroenDesloovere\VCard\Parser\Property\Parameter\ValueParser;
12
use JeroenDesloovere\VCard\Property\SimpleNodeInterface;
13
14
final class Value implements PropertyParameterInterface, SimpleNodeInterface
15
{
16
    protected const TEXT = 'text';
17
    protected const URI = 'uri';
18
    protected const DATE = 'date';
19
    protected const TIME = 'time';
20
    protected const DATE_TIME = 'date-time';
21
    protected const DATE_AND_OR_TIME = 'date-and-or-time';
22
    protected const TIMESTAMP = 'timestamp';
23
    protected const BOOLEAN = 'boolean';
24
    protected const INTEGER = 'integer';
25
    protected const FLOAT = 'float';
26
    protected const UTC_OFFSET = 'utc-offset';
27
    protected const LANGUAGE_TAG = 'language-tag';
28
29
    public const POSSIBLE_VALUES = [
30
        self::TEXT,
31
        self::URI,
32
        self::DATE,
33
        self::TIME,
34
        self::DATE_TIME,
35
        self::DATE_AND_OR_TIME,
36
        self::TIMESTAMP,
37
        self::BOOLEAN,
38
        self::INTEGER,
39
        self::FLOAT,
40
        self::UTC_OFFSET,
41
        self::LANGUAGE_TAG
42
    ];
43
44
    private $value;
45
46
    /**
47
     * @param string $value
48
     * @throws PropertyParameterException
49
     */
50
    public function __construct(string $value)
51
    {
52
        if (!in_array($value, self::POSSIBLE_VALUES, true)) {
53
            throw PropertyParameterException::forWrongValue($value, self::POSSIBLE_VALUES);
54
        }
55
56
        $this->value = $value;
57
    }
58
59
    public function __toString(): string
60
    {
61
        return $this->value;
62
    }
63
64
    public function getFormatter(): NodeFormatterInterface
65
    {
66
        return new ValueFormatter($this);
67
    }
68
69
    public static function getNode(): string
70
    {
71
        return 'VALUE';
72
    }
73
74
    public static function getParser(): NodeParserInterface
75
    {
76
        return new ValueParser();
77
    }
78
79
    public static function text(): self
80
    {
81
        return new self(self::TEXT);
82
    }
83
84
    public static function uri(): self
85
    {
86
        return new self(self::URI);
87
    }
88
89
    public static function date(): self
90
    {
91
        return new self(self::DATE);
92
    }
93
94
    public static function time(): self
95
    {
96
        return new self(self::TIME);
97
    }
98
99
    public static function dateTime(): self
100
    {
101
        return new self(self::DATE_TIME);
102
    }
103
104
    public static function dateAndOrTime(): self
105
    {
106
        return new self(self::DATE_AND_OR_TIME);
107
    }
108
109
    public static function timestamp(): self
110
    {
111
        return new self(self::TIMESTAMP);
112
    }
113
114
    public static function boolean(): self
115
    {
116
        return new self(self::BOOLEAN);
117
    }
118
119
    public static function integer(): self
120
    {
121
        return new self(self::INTEGER);
122
    }
123
124
    public static function float(): self
125
    {
126
        return new self(self::FLOAT);
127
    }
128
129
    public static function utcOffset(): self
130
    {
131
        return new self(self::UTC_OFFSET);
132
    }
133
134
    public static function languageTag(): self
135
    {
136
        return new self(self::LANGUAGE_TAG);
137
    }
138
}
139