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