Passed
Push — master ( 8a14ce...7e62f1 )
by Claudio
06:57
created

Builder::getSorter()   A

Complexity

Conditions 3
Paths 4

Size

Total Lines 14
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 8
CRAP Score 3

Importance

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