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
|
|
|
public function __construct(string $locale) |
31
|
|
|
{ |
32
|
|
|
$this->collator = new Collator($locale); |
33
|
|
|
} |
34
|
|
|
|
35
|
|
|
public function enableFrenchCollation() : self |
36
|
|
|
{ |
37
|
|
|
$this->collator->setAttribute(Collator::FRENCH_COLLATION, Collator::ON); |
38
|
|
|
|
39
|
|
|
return $this; |
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
public function disableFrenchCollation() : self |
43
|
|
|
{ |
44
|
|
|
$this->collator->setAttribute(Collator::FRENCH_COLLATION, Collator::OFF); |
45
|
|
|
|
46
|
|
|
return $this; |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
public function lowerCaseFirst() : self |
50
|
|
|
{ |
51
|
|
|
$this->collator->setAttribute(Collator::CASE_FIRST, Collator::LOWER_FIRST); |
52
|
|
|
|
53
|
|
|
return $this; |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
public function upperCaseFirst() : self |
57
|
|
|
{ |
58
|
|
|
$this->collator->setAttribute(Collator::CASE_FIRST, Collator::UPPER_FIRST); |
59
|
|
|
|
60
|
|
|
return $this; |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
public function removeCaseFirst() : self |
64
|
|
|
{ |
65
|
|
|
$this->collator->setAttribute(Collator::CASE_FIRST, Collator::OFF); |
66
|
|
|
|
67
|
|
|
return $this; |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
public function enableNormalizationMode() : self |
71
|
|
|
{ |
72
|
|
|
$this->collator->setAttribute(Collator::NORMALIZATION_MODE, Collator::ON); |
73
|
|
|
|
74
|
|
|
return $this; |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
public function disableNormalizationMode() : self |
78
|
|
|
{ |
79
|
|
|
$this->collator->setAttribute(Collator::NORMALIZATION_MODE, Collator::OFF); |
80
|
|
|
|
81
|
|
|
return $this; |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
public function enableHiraganaQuaternaryMode() : self |
85
|
|
|
{ |
86
|
|
|
$this->collator->setAttribute(Collator::HIRAGANA_QUATERNARY_MODE, Collator::ON); |
87
|
|
|
|
88
|
|
|
return $this; |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
public function disableHiraganaQuaternaryMode() : self |
92
|
|
|
{ |
93
|
|
|
$this->collator->setAttribute(Collator::HIRAGANA_QUATERNARY_MODE, Collator::OFF); |
94
|
|
|
|
95
|
|
|
return $this; |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
public function enableNumericCollation() : self |
99
|
|
|
{ |
100
|
|
|
$this->collator->setAttribute(Collator::NUMERIC_COLLATION, Collator::ON); |
101
|
|
|
|
102
|
|
|
return $this; |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
public function disableNumericCollation() : self |
106
|
|
|
{ |
107
|
|
|
$this->collator->setAttribute(Collator::NUMERIC_COLLATION, Collator::OFF); |
108
|
|
|
|
109
|
|
|
return $this; |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
public function enableCaseLevel() : self |
113
|
|
|
{ |
114
|
|
|
$this->collator->setAttribute(Collator::CASE_LEVEL, Collator::ON); |
115
|
|
|
|
116
|
|
|
return $this; |
117
|
|
|
} |
118
|
|
|
|
119
|
|
|
public function disableCaseLevel() : self |
120
|
|
|
{ |
121
|
|
|
$this->collator->setAttribute(Collator::CASE_LEVEL, Collator::OFF); |
122
|
|
|
|
123
|
|
|
return $this; |
124
|
|
|
} |
125
|
|
|
|
126
|
|
|
public function nonIgnorableAlternateHandling() : self |
127
|
|
|
{ |
128
|
|
|
$this->collator->setAttribute(Collator::ALTERNATE_HANDLING, Collator::NON_IGNORABLE); |
129
|
|
|
|
130
|
|
|
return $this; |
131
|
|
|
} |
132
|
|
|
|
133
|
|
|
public function shiftedAlternateHandling() : self |
134
|
|
|
{ |
135
|
|
|
$this->collator->setAttribute(Collator::ALTERNATE_HANDLING, Collator::SHIFTED); |
136
|
|
|
|
137
|
|
|
return $this; |
138
|
|
|
} |
139
|
|
|
|
140
|
|
|
public function primaryStrength() : self |
141
|
|
|
{ |
142
|
|
|
$this->collator->setStrength(Collator::PRIMARY); |
143
|
|
|
|
144
|
|
|
return $this; |
145
|
|
|
} |
146
|
|
|
|
147
|
|
|
public function secondaryStrength() : self |
148
|
|
|
{ |
149
|
|
|
$this->collator->setStrength(Collator::SECONDARY); |
150
|
|
|
|
151
|
|
|
return $this; |
152
|
|
|
} |
153
|
|
|
|
154
|
|
|
public function tertiaryStrength() : self |
155
|
|
|
{ |
156
|
|
|
$this->collator->setStrength(Collator::TERTIARY); |
157
|
|
|
|
158
|
|
|
return $this; |
159
|
|
|
} |
160
|
|
|
|
161
|
|
|
public function quarternaryStrength() : self |
162
|
|
|
{ |
163
|
|
|
$this->collator->setStrength(Collator::QUATERNARY); |
164
|
|
|
|
165
|
|
|
return $this; |
166
|
|
|
} |
167
|
|
|
|
168
|
|
|
public function identicalStrength() : self |
169
|
|
|
{ |
170
|
|
|
$this->collator->setStrength(Collator::IDENTICAL); |
171
|
|
|
|
172
|
|
|
return $this; |
173
|
|
|
} |
174
|
|
|
|
175
|
|
|
public function orderByAsc() : self |
176
|
|
|
{ |
177
|
|
|
$this->isAsc = true; |
178
|
|
|
|
179
|
|
|
return $this; |
180
|
|
|
} |
181
|
|
|
|
182
|
|
|
public function orderByDesc() : self |
183
|
|
|
{ |
184
|
|
|
$this->isAsc = false; |
185
|
|
|
|
186
|
|
|
return $this; |
187
|
|
|
} |
188
|
|
|
|
189
|
|
|
public function orderByKeys() : self |
190
|
|
|
{ |
191
|
|
|
$this->isKeySort = true; |
192
|
|
|
|
193
|
|
|
return $this; |
194
|
|
|
} |
195
|
|
|
|
196
|
|
|
public function orderByValues() : self |
197
|
|
|
{ |
198
|
|
|
$this->isKeySort = false; |
199
|
|
|
|
200
|
|
|
return $this; |
201
|
|
|
} |
202
|
|
|
|
203
|
|
|
public function getSorter() : Sorter |
204
|
|
|
{ |
205
|
|
|
if ($this->isKeySort) { |
206
|
|
|
$sorter = new Key($this->collator); |
207
|
|
|
} else { |
208
|
|
|
$sorter = new Asc($this->collator, Collator::SORT_STRING); |
209
|
|
|
} |
210
|
|
|
|
211
|
|
|
if (! $this->isAsc) { |
212
|
|
|
$sorter = new Desc($sorter); |
213
|
|
|
} |
214
|
|
|
|
215
|
|
|
return $sorter; |
216
|
|
|
} |
217
|
|
|
} |