Passed
Push — master ( 2b46b9...8a14ce )
by Claudio
02:08
created

Builder::unsetOrderByCustomSorter()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 0
Metric Value
eloc 2
dl 0
loc 5
ccs 3
cts 3
cp 1
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 0
crap 1
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 Budgegeria\IntlSort\SorterFactory\Factory;
14
use Collator;
15
16
class Builder
17
{
18
    /**
19
     * @var Collator
20
     */
21
    private $collator;
22
23
    /**
24
     * @var bool
25
     */
26
    private $isAsc = true;
27
28
    /**
29
     * @var bool
30
     */
31
    private $isKeySort = false;
32
33
    /**
34
     * @var Factory|null
35
     */
36
    private $sorterFactory;
37
38 33
    public function __construct(string $locale)
39
    {
40 33
        $this->collator = new Collator($locale);
41 33
    }
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()} or {@see enableHiraganaQuaternaryMode()}
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 2
    public function orderByAsc(): self
183
    {
184 2
        $this->isAsc = true;
185
186 2
        return $this;
187
    }
188
189 3
    public function orderByDesc(): self
190
    {
191 3
        $this->isAsc = false;
192
193 3
        return $this;
194
    }
195
196 4
    public function orderByKeys(): self
197
    {
198 4
        $this->isKeySort = true;
199
200 4
        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 orderByCustomSorter(Factory $factory): self
211
    {
212 2
        $this->sorterFactory = $factory;
213
214 2
        return $this;
215
    }
216
217 1
    public function unsetOrderByCustomSorter(): self
218
    {
219 1
        $this->sorterFactory = null;
220
221 1
        return $this;
222
    }
223
224 32
    public function getSorter(): Sorter
225
    {
226 32
        if ($this->sorterFactory === null) {
227 31
            if ($this->isKeySort) {
228 2
                $sorter = new Key($this->collator);
229
            } else {
230 31
                $sorter = new Asc($this->collator, Collator::SORT_STRING);
231
            }
232
        } else {
233 1
            $sorter = $this->sorterFactory->createSorter($this->collator);
234
        }
235
236 32
        if (! $this->isAsc) {
237 2
            return new Desc($sorter);
238
        }
239
240 30
        return $sorter;
241
    }
242
243 1
    public function getComparator(): Comparable
244
    {
245 1
        return new Comparator($this->collator);
246
    }
247
}