UnicodeComparator::__construct()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
eloc 4
c 1
b 0
f 0
nc 2
nop 1
dl 0
loc 8
ccs 4
cts 4
cp 1
crap 2
rs 10
1
<?php
2
3
namespace Ninja\Sorter\Comparator;
4
5
use Collator;
6
use Locale;
7
8
readonly class UnicodeComparator implements ComparatorInterface
9
{
10
    private Collator $collator;
11 35
12
    public function __construct(?string $locale = null)
13 35
    {
14 10
        if (!$locale) {
15
            $locale = Locale::getDefault();
16
        }
17 35
18 35
        $this->collator = new Collator($locale);
19
        $this->collator->setAttribute(Collator::NUMERIC_COLLATION, Collator::ON);
20
    }
21 2
22
    public function getLocale(): string
23 2
    {
24
        return $this->collator->getLocale(Locale::VALID_LOCALE);
25
    }
26
27
    /**
28
     * {@inheritdoc}
29 28
     */
30
    public function compare(mixed $a, mixed $b): int
31 28
    {
32 28
        $result = $this->collator->compare($a, $b);
33
        return $result !== false ? $result : 0;
34
    }
35
36
    /**
37
     * {@inheritdoc}
38 9
     */
39
    public function supports(mixed $value): bool
40 9
    {
41
        return $value === null || is_string($value) || is_int($value) || is_float($value);
42
    }
43
}
44