1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace Budgegeria\IntlSort; |
6
|
|
|
|
7
|
|
|
use Budgegeria\IntlSort\Collator\ConfigurableCollator; |
8
|
|
|
use Budgegeria\IntlSort\Collator\Configuration; |
9
|
|
|
use Budgegeria\IntlSort\Comparator\Comparable; |
10
|
|
|
use Budgegeria\IntlSort\ComparatorFactory\Factory; |
11
|
|
|
use Budgegeria\IntlSort\ComparatorFactory\Standard; |
12
|
|
|
use Budgegeria\IntlSort\Sorter\Asc; |
13
|
|
|
use Budgegeria\IntlSort\Sorter\Desc; |
14
|
|
|
use Budgegeria\IntlSort\Sorter\Key; |
15
|
|
|
use Budgegeria\IntlSort\Sorter\Sorter; |
16
|
|
|
use Collator; |
17
|
|
|
|
18
|
|
|
class Builder |
19
|
|
|
{ |
20
|
|
|
private Collator $collator; |
21
|
|
|
|
22
|
|
|
private bool $isAsc = true; |
23
|
|
|
|
24
|
|
|
private bool $isKeySort = false; |
25
|
|
|
|
26
|
|
|
private Factory $comparatorFactory; |
27
|
|
|
|
28
|
|
|
private Configuration $configuration; |
29
|
|
|
|
30
|
38 |
|
public function __construct(string $locale, Factory|null $comparatorFactory = null) |
31
|
|
|
{ |
32
|
38 |
|
$this->collator = new Collator($locale); |
33
|
38 |
|
$this->configuration = new Configuration(); |
34
|
38 |
|
$this->comparatorFactory = $comparatorFactory ?? new Standard(); |
35
|
|
|
} |
36
|
|
|
|
37
|
38 |
|
public static function create(string $locale): self |
38
|
|
|
{ |
39
|
38 |
|
return new self($locale, new Standard()); |
40
|
|
|
} |
41
|
|
|
|
42
|
2 |
|
public function enableFrenchCollation(): self |
43
|
|
|
{ |
44
|
2 |
|
$this->collator->setAttribute(Collator::FRENCH_COLLATION, Collator::ON); |
45
|
|
|
|
46
|
2 |
|
return $this; |
47
|
|
|
} |
48
|
|
|
|
49
|
1 |
|
public function disableFrenchCollation(): self |
50
|
|
|
{ |
51
|
1 |
|
$this->collator->setAttribute(Collator::FRENCH_COLLATION, Collator::OFF); |
52
|
|
|
|
53
|
1 |
|
return $this; |
54
|
|
|
} |
55
|
|
|
|
56
|
1 |
|
public function lowerCaseFirst(): self |
57
|
|
|
{ |
58
|
1 |
|
$this->collator->setAttribute(Collator::CASE_FIRST, Collator::LOWER_FIRST); |
59
|
|
|
|
60
|
1 |
|
return $this; |
61
|
|
|
} |
62
|
|
|
|
63
|
3 |
|
public function upperCaseFirst(): self |
64
|
|
|
{ |
65
|
3 |
|
$this->collator->setAttribute(Collator::CASE_FIRST, Collator::UPPER_FIRST); |
66
|
|
|
|
67
|
3 |
|
return $this; |
68
|
|
|
} |
69
|
|
|
|
70
|
1 |
|
public function removeCaseFirst(): self |
71
|
|
|
{ |
72
|
1 |
|
$this->collator->setAttribute(Collator::CASE_FIRST, Collator::OFF); |
73
|
|
|
|
74
|
1 |
|
return $this; |
75
|
|
|
} |
76
|
|
|
|
77
|
2 |
|
public function enableNormalizationMode(): self |
78
|
|
|
{ |
79
|
2 |
|
$this->collator->setAttribute(Collator::NORMALIZATION_MODE, Collator::ON); |
80
|
|
|
|
81
|
2 |
|
return $this; |
82
|
|
|
} |
83
|
|
|
|
84
|
1 |
|
public function disableNormalizationMode(): self |
85
|
|
|
{ |
86
|
1 |
|
$this->collator->setAttribute(Collator::NORMALIZATION_MODE, Collator::OFF); |
87
|
|
|
|
88
|
1 |
|
return $this; |
89
|
|
|
} |
90
|
|
|
|
91
|
2 |
|
public function enableNumericCollation(): self |
92
|
|
|
{ |
93
|
2 |
|
$this->collator->setAttribute(Collator::NUMERIC_COLLATION, Collator::ON); |
94
|
|
|
|
95
|
2 |
|
return $this; |
96
|
|
|
} |
97
|
|
|
|
98
|
1 |
|
public function disableNumericCollation(): self |
99
|
|
|
{ |
100
|
1 |
|
$this->collator->setAttribute(Collator::NUMERIC_COLLATION, Collator::OFF); |
101
|
|
|
|
102
|
1 |
|
return $this; |
103
|
|
|
} |
104
|
|
|
|
105
|
3 |
|
public function enableCaseLevel(): self |
106
|
|
|
{ |
107
|
3 |
|
$this->collator->setAttribute(Collator::CASE_LEVEL, Collator::ON); |
108
|
|
|
|
109
|
3 |
|
return $this; |
110
|
|
|
} |
111
|
|
|
|
112
|
2 |
|
public function disableCaseLevel(): self |
113
|
|
|
{ |
114
|
2 |
|
$this->collator->setAttribute(Collator::CASE_LEVEL, Collator::OFF); |
115
|
|
|
|
116
|
2 |
|
return $this; |
117
|
|
|
} |
118
|
|
|
|
119
|
1 |
|
public function nonIgnorableAlternateHandling(): self |
120
|
|
|
{ |
121
|
1 |
|
$this->collator->setAttribute(Collator::ALTERNATE_HANDLING, Collator::NON_IGNORABLE); |
122
|
|
|
|
123
|
1 |
|
return $this; |
124
|
|
|
} |
125
|
|
|
|
126
|
4 |
|
public function shiftedAlternateHandling(): self |
127
|
|
|
{ |
128
|
4 |
|
$this->collator->setAttribute(Collator::ALTERNATE_HANDLING, Collator::SHIFTED); |
129
|
|
|
|
130
|
4 |
|
return $this; |
131
|
|
|
} |
132
|
|
|
|
133
|
|
|
/** |
134
|
|
|
* Ignore accents and case |
135
|
|
|
*/ |
136
|
5 |
|
public function primaryStrength(): self |
137
|
|
|
{ |
138
|
5 |
|
$this->collator->setStrength(Collator::PRIMARY); |
139
|
|
|
|
140
|
5 |
|
return $this; |
141
|
|
|
} |
142
|
|
|
|
143
|
|
|
/** |
144
|
|
|
* Ignore case, consider accents |
145
|
|
|
*/ |
146
|
2 |
|
public function secondaryStrength(): self |
147
|
|
|
{ |
148
|
2 |
|
$this->collator->setStrength(Collator::SECONDARY); |
149
|
|
|
|
150
|
2 |
|
return $this; |
151
|
|
|
} |
152
|
|
|
|
153
|
|
|
/** |
154
|
|
|
* Consider accents and case |
155
|
|
|
*/ |
156
|
3 |
|
public function tertiaryStrength(): self |
157
|
|
|
{ |
158
|
3 |
|
$this->collator->setStrength(Collator::TERTIARY); |
159
|
|
|
|
160
|
3 |
|
return $this; |
161
|
|
|
} |
162
|
|
|
|
163
|
|
|
/** |
164
|
|
|
* Like tertiary, but also considers whitespace, punctuation, and symbols differently when |
165
|
|
|
* used with {@see shiftedAlternateHandling()} |
166
|
|
|
*/ |
167
|
2 |
|
public function quaternaryStrength(): self |
168
|
|
|
{ |
169
|
2 |
|
$this->collator->setStrength(Collator::QUATERNARY); |
170
|
|
|
|
171
|
2 |
|
return $this; |
172
|
|
|
} |
173
|
|
|
|
174
|
4 |
|
public function identicalStrength(): self |
175
|
|
|
{ |
176
|
4 |
|
$this->collator->setStrength(Collator::IDENTICAL); |
177
|
|
|
|
178
|
4 |
|
return $this; |
179
|
|
|
} |
180
|
|
|
|
181
|
1 |
|
public function orderByAsc(): self |
182
|
|
|
{ |
183
|
1 |
|
$this->isAsc = true; |
184
|
|
|
|
185
|
1 |
|
return $this; |
186
|
|
|
} |
187
|
|
|
|
188
|
4 |
|
public function orderByDesc(): self |
189
|
|
|
{ |
190
|
4 |
|
$this->isAsc = false; |
191
|
|
|
|
192
|
4 |
|
return $this; |
193
|
|
|
} |
194
|
|
|
|
195
|
3 |
|
public function orderByKeys(): self |
196
|
|
|
{ |
197
|
3 |
|
$this->isKeySort = true; |
198
|
|
|
|
199
|
3 |
|
return $this; |
200
|
|
|
} |
201
|
|
|
|
202
|
1 |
|
public function orderByValues(): self |
203
|
|
|
{ |
204
|
1 |
|
$this->isKeySort = false; |
205
|
|
|
|
206
|
1 |
|
return $this; |
207
|
|
|
} |
208
|
|
|
|
209
|
2 |
|
public function nullFirst(): self |
210
|
|
|
{ |
211
|
2 |
|
$this->configuration->setNullableSort(Configuration::NULL_VALUES_FIRST); |
212
|
|
|
|
213
|
2 |
|
return $this; |
214
|
|
|
} |
215
|
|
|
|
216
|
2 |
|
public function nullLast(): self |
217
|
|
|
{ |
218
|
2 |
|
$this->configuration->setNullableSort(Configuration::NULL_VALUES_LAST); |
219
|
|
|
|
220
|
2 |
|
return $this; |
221
|
|
|
} |
222
|
|
|
|
223
|
1 |
|
public function removeNullPosition(): self |
224
|
|
|
{ |
225
|
1 |
|
$this->configuration->setNullableSort(null); |
226
|
|
|
|
227
|
1 |
|
return $this; |
228
|
|
|
} |
229
|
|
|
|
230
|
36 |
|
public function getSorter(): Sorter |
231
|
|
|
{ |
232
|
36 |
|
$comparator = $this->getComparator(); |
233
|
36 |
|
if ($this->isKeySort) { |
234
|
2 |
|
$sorter = new Key($comparator); |
235
|
|
|
} else { |
236
|
34 |
|
$sorter = new Asc($comparator); |
237
|
|
|
} |
238
|
|
|
|
239
|
36 |
|
if (! $this->isAsc) { |
240
|
3 |
|
return new Desc($sorter); |
241
|
|
|
} |
242
|
|
|
|
243
|
33 |
|
return $sorter; |
244
|
|
|
} |
245
|
|
|
|
246
|
38 |
|
public function getComparator(): Comparable |
247
|
|
|
{ |
248
|
38 |
|
return $this->comparatorFactory->create(new ConfigurableCollator($this->collator, $this->configuration)); |
249
|
|
|
} |
250
|
|
|
} |
251
|
|
|
|