UnicodeComparator   A
last analyzed

Complexity

Total Complexity 9

Size/Duplication

Total Lines 34
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 10
c 1
b 0
f 0
dl 0
loc 34
ccs 12
cts 12
cp 1
rs 10
wmc 9

4 Methods

Rating   Name   Duplication   Size   Complexity  
A compare() 0 4 2
A getLocale() 0 3 1
A supports() 0 3 4
A __construct() 0 8 2
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