-
-
Notifications
You must be signed in to change notification settings - Fork 23
/
Annotations.php
194 lines (172 loc) · 4.28 KB
/
Annotations.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
<?php
namespace Notion\Common;
/** @psalm-type AnnotationsJson = array{
* bold: bool,
* italic: bool,
* strikethrough: bool,
* underline: bool,
* code: bool,
* color: string,
* }
*/
class Annotations
{
/** @var string[] */
private const ALLOWED_COLORS = [
"default", "gray", "brown", "orange", "yellow", "green", "blue",
"purple", "pink", "red", "gray_background", "brown_background",
"orange_background", "yellow_background", "green_background",
"blue_background", "purple_background", "pink_background",
"red_background",
];
private bool $bold;
private bool $italic;
private bool $strikeThrough;
private bool $underline;
private bool $code;
private string $color;
private function __construct(
bool $bold,
bool $italic,
bool $strikeThrough,
bool $underline,
bool $code,
string $color,
) {
$this->checkColor($color);
$this->bold = $bold;
$this->italic = $italic;
$this->strikeThrough = $strikeThrough;
$this->underline = $underline;
$this->code = $code;
$this->color = $color;
}
public static function create(): self
{
return new self(false, false, false, false, false, "default");
}
/**
* @param AnnotationsJson $array
*
* @internal
*/
public static function fromArray(array $array): self
{
return new self(
$array["bold"],
$array["italic"],
$array["strikethrough"],
$array["underline"],
$array["code"],
$array["color"],
);
}
public function toArray(): array
{
return [
"bold" => $this->bold,
"italic" => $this->italic,
"strikethrough" => $this->strikeThrough,
"underline" => $this->underline,
"code" => $this->code,
"color" => $this->color,
];
}
public function isBold(): bool
{
return $this->bold;
}
public function isItalic(): bool
{
return $this->italic;
}
public function isStrikeThrough(): bool
{
return $this->strikeThrough;
}
public function isUnderline(): bool
{
return $this->underline;
}
public function isCode(): bool
{
return $this->code;
}
public function color(): string
{
return $this->color;
}
public function bold(bool $bold = true): self
{
return new self(
$bold,
$this->italic,
$this->strikeThrough,
$this->underline,
$this->code,
$this->color,
);
}
public function italic(bool $italic = true): self
{
return new self(
$this->bold,
$italic,
$this->strikeThrough,
$this->underline,
$this->code,
$this->color,
);
}
public function strikeThrough(bool $strikeThrough = true): self
{
return new self(
$this->bold,
$this->italic,
$strikeThrough,
$this->underline,
$this->code,
$this->color,
);
}
public function underline(bool $underline = true): self
{
return new self(
$this->bold,
$this->italic,
$this->strikeThrough,
$underline,
$this->code,
$this->color,
);
}
public function code(bool $code = true): self
{
return new self(
$this->bold,
$this->italic,
$this->strikeThrough,
$this->underline,
$code,
$this->color,
);
}
public function withColor(string $color): self
{
$this->checkColor($color);
return new self(
$this->bold,
$this->italic,
$this->strikeThrough,
$this->underline,
$this->code,
$color,
);
}
private function checkColor(string $color): void
{
if (!in_array($color, self::ALLOWED_COLORS)) {
throw new \Exception("Invalid color: '{$color}'.");
}
}
}