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