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