Passed
Push — master ( 938fb4...cfa6a0 )
by Claudio
07:15
created

Builder::nullFirst()   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
cc 1
eloc 2
nc 1
nop 0
dl 0
loc 5
ccs 3
cts 3
cp 1
crap 1
rs 10
c 0
b 0
f 0
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\Factory;
9
use Budgegeria\IntlSort\ComparatorFactory\Standard;
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
15
class Builder
16
{
17
    /** @var Collator */
18
    private $collator;
19
20
    /** @var bool */
21
    private $isAsc = true;
22
23
    /** @var bool */
24
    private $isKeySort = false;
25
26
    /** @var Factory */
27
    private $comparatorFactory;
28
29 37
    public function __construct(string $locale, ?Factory $comparatorFactory = null)
30
    {
31 37
        $this->collator          = new Collator($locale);
32 37
        $this->comparatorFactory = $comparatorFactory ?? new Standard();
33 37
    }
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()}
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 4
    public function orderByDesc(): self
182
    {
183 4
        $this->isAsc = false;
184
185 4
        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 2
    public function nullFirst(): self
203
    {
204 2
        $this->collator->setNullableSort(Collator::NULL_VALUES_FIRST);
205
206 2
        return $this;
207
    }
208
209 1
    public function nullLast(): self
210
    {
211 1
        $this->collator->setNullableSort(Collator::NULL_VALUES_LAST);
212
213 1
        return $this;
214
    }
215
216
    public function removeNullPosition(): self
217
    {
218
        $this->collator->setNullableSort(null);
219
220
        return $this;
221
    }
222
223 35
    public function getSorter(): Sorter
224
    {
225 35
        $comparator = $this->getComparator();
226 35
        if ($this->isKeySort) {
227 2
            $sorter = new Key($comparator);
228
        } else {
229 33
            $sorter = new Asc($comparator);
230
        }
231
232 35
        if (! $this->isAsc) {
233 3
            return new Desc($sorter);
234
        }
235
236 32
        return $sorter;
237
    }
238
239 37
    public function getComparator(): Comparable
240
    {
241 37
        return $this->comparatorFactory->create($this->collator);
242
    }
243
}
244