1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace Vasoft\VersionIncrement\Core; |
6
|
|
|
|
7
|
|
|
final class Sections |
8
|
|
|
{ |
9
|
|
|
public const DEFAULT_SECTION = 'other'; |
10
|
|
|
private bool $sored = true; |
11
|
|
|
private int $defaultOrder = 500; |
12
|
|
|
private array $sections = [ |
13
|
|
|
'feat' => [ |
14
|
|
|
'title' => 'New features', |
15
|
|
|
'order' => 10, |
16
|
|
|
'hidden' => false, |
17
|
|
|
], |
18
|
|
|
'fix' => [ |
19
|
|
|
'title' => 'Fixes', |
20
|
|
|
'order' => 20, |
21
|
|
|
'hidden' => false, |
22
|
|
|
], |
23
|
|
|
'chore' => [ |
24
|
|
|
'title' => 'Other changes', |
25
|
|
|
'order' => 30, |
26
|
|
|
'hidden' => false, |
27
|
|
|
], |
28
|
|
|
'docs' => [ |
29
|
|
|
'title' => 'Documentation', |
30
|
|
|
'order' => 40, |
31
|
|
|
'hidden' => false, |
32
|
|
|
], |
33
|
|
|
'style' => [ |
34
|
|
|
'title' => 'Styling', |
35
|
|
|
'order' => 50, |
36
|
|
|
'hidden' => false, |
37
|
|
|
], |
38
|
|
|
'refactor' => [ |
39
|
|
|
'title' => 'Refactoring', |
40
|
|
|
'order' => 60, |
41
|
|
|
'hidden' => false, |
42
|
|
|
], |
43
|
|
|
'test' => [ |
44
|
|
|
'title' => 'Tests', |
45
|
|
|
'order' => 70, |
46
|
|
|
'hidden' => false, |
47
|
|
|
], |
48
|
|
|
'perf' => [ |
49
|
|
|
'title' => 'Performance', |
50
|
|
|
'order' => 80, |
51
|
|
|
'hidden' => false, |
52
|
|
|
], |
53
|
|
|
'ci' => [ |
54
|
|
|
'title' => 'Configure CI', |
55
|
|
|
'order' => 90, |
56
|
|
|
'hidden' => false, |
57
|
|
|
], |
58
|
|
|
'build' => [ |
59
|
|
|
'title' => 'Change build system', |
60
|
|
|
'order' => 100, |
61
|
|
|
'hidden' => false, |
62
|
|
|
], |
63
|
|
|
'other' => [ |
64
|
|
|
'title' => 'Other', |
65
|
|
|
'order' => 110, |
66
|
|
|
'hidden' => false, |
67
|
|
|
], |
68
|
|
|
]; |
69
|
|
|
|
70
|
7 |
|
public function setSections(array $sections): self |
71
|
|
|
{ |
72
|
7 |
|
$this->sored = false; |
73
|
7 |
|
$this->sections = []; |
74
|
7 |
|
foreach ($sections as $key => $value) { |
75
|
7 |
|
$this->sections[$key] = [ |
76
|
7 |
|
'title' => $value['title'] ?? $key, |
77
|
7 |
|
'order' => $value['order'] ?? ++$this->defaultOrder, |
78
|
7 |
|
'hidden' => $value['hidden'] ?? false, |
79
|
7 |
|
]; |
80
|
|
|
} |
81
|
|
|
|
82
|
7 |
|
return $this; |
83
|
|
|
} |
84
|
|
|
|
85
|
3 |
|
public function setSection( |
86
|
|
|
string $key, |
87
|
|
|
string $title, |
88
|
|
|
int $order = -1, |
89
|
|
|
?bool $hidden = null, |
90
|
|
|
): self { |
91
|
3 |
|
$this->sored = false; |
92
|
3 |
|
$exists = $this->sections[$key] ?? null; |
93
|
3 |
|
$this->sections[$key] = [ |
94
|
3 |
|
'title' => $title, |
95
|
3 |
|
'order' => -1 === $order ? ($exists ? $exists['order'] : ++$this->defaultOrder) : $order, |
96
|
3 |
|
'hidden' => null !== $hidden ? $hidden : ($exists ? $exists['hidden'] : false), |
97
|
3 |
|
]; |
98
|
|
|
|
99
|
3 |
|
return $this; |
100
|
|
|
} |
101
|
|
|
|
102
|
3 |
|
public function getIndex(): array |
103
|
|
|
{ |
104
|
3 |
|
$this->sortSections(); |
105
|
|
|
|
106
|
3 |
|
return array_fill_keys(array_keys($this->sections), []); |
107
|
|
|
} |
108
|
|
|
|
109
|
34 |
|
private function sortSections(): void |
110
|
|
|
{ |
111
|
34 |
|
if (!$this->sored) { |
112
|
9 |
|
$this->checkDefaultSection(); |
113
|
9 |
|
uasort($this->sections, static fn($a, $b) => $a['order'] <=> $b['order']); |
114
|
9 |
|
$this->sored = true; |
115
|
|
|
} |
116
|
|
|
} |
117
|
|
|
|
118
|
9 |
|
private function checkDefaultSection(): void |
119
|
|
|
{ |
120
|
9 |
|
if (!isset($this->sections[self::DEFAULT_SECTION])) { |
121
|
6 |
|
$maxOrder = 0; |
122
|
6 |
|
foreach ($this->sections as $section) { |
123
|
6 |
|
$maxOrder = max($maxOrder, $section['order']); |
124
|
|
|
} |
125
|
6 |
|
$this->sections[self::DEFAULT_SECTION] = [ |
126
|
6 |
|
'title' => 'Other', |
127
|
6 |
|
'order' => $maxOrder + 1000, |
128
|
6 |
|
'hidden' => false, |
129
|
6 |
|
]; |
130
|
|
|
} |
131
|
|
|
} |
132
|
|
|
|
133
|
27 |
|
public function getSortedSections(): array |
134
|
|
|
{ |
135
|
27 |
|
$this->sortSections(); |
136
|
|
|
|
137
|
27 |
|
return $this->sections; |
138
|
|
|
} |
139
|
|
|
|
140
|
4 |
|
public function getTitles(): array |
141
|
|
|
{ |
142
|
4 |
|
$this->sortSections(); |
143
|
4 |
|
$result = []; |
144
|
4 |
|
foreach ($this->sections as $key => $value) { |
145
|
4 |
|
$result[$key] = $value['title']; |
146
|
|
|
} |
147
|
|
|
|
148
|
4 |
|
return $result; |
149
|
|
|
} |
150
|
|
|
|
151
|
2 |
|
public function isHidden(string $key): bool |
152
|
|
|
{ |
153
|
2 |
|
return $this->sections[$key]['hidden'] ?? false; |
154
|
|
|
} |
155
|
|
|
|
156
|
2 |
|
public function getTitle(string $key): string |
157
|
|
|
{ |
158
|
2 |
|
return $this->sections[$key]['title'] ?? $key; |
159
|
|
|
} |
160
|
|
|
|
161
|
22 |
|
public function exits(string $key): bool |
162
|
|
|
{ |
163
|
22 |
|
return array_key_exists($key, $this->sections); |
164
|
|
|
} |
165
|
|
|
} |
166
|
|
|
|