UnicodeComparator::getLocale()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 1
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 1
c 1
b 0
f 0
nc 1
nop 0
dl 0
loc 3
ccs 1
cts 1
cp 1
crap 1
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