ConfigurableCollator::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 2
Code Lines 0

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 1
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 0
c 1
b 0
f 0
nc 1
nop 2
dl 0
loc 2
ccs 1
cts 1
cp 1
crap 1
rs 10
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Budgegeria\IntlSort\Collator;
6
7
use Budgegeria\IntlSort\Exception\IntlSortException;
8
use Collator as IntlCollator;
9
use IntlException;
10
11
use function assert;
12
use function is_int;
13
14
final class ConfigurableCollator implements Collator
15
{
16 64
    public function __construct(private IntlCollator $collator, private Configuration $config)
17
    {
18 64
    }
19
20
    /** @throws IntlSortException */
21 61
    public function compare(mixed $value, mixed $comparativeValue): int
22
    {
23 61
        if (($comparativeValue === null || $value === null) && $this->config->getNullableSort() === Configuration::NULL_VALUES_LAST) {
24 1
            return $comparativeValue <=> $value;
25
        }
26
27 61
        if (($comparativeValue === null || $value === null) && $this->config->getNullableSort() === Configuration::NULL_VALUES_FIRST) {
28 2
            return Configuration::NULL_VALUES_FIRST;
29
        }
30
31
        try {
32 61
            $compared = $this->collator->compare((string) $value, (string) $comparativeValue);
33
34 59
            assert(is_int($compared));
35
36 59
            if ($this->collator->getErrorCode() !== 0) {
37 59
                throw IntlSortException::errorOnSort($this->collator->getErrorMessage());
38
            }
39 4
        } catch (IntlException $e) {
40 2
            throw IntlSortException::errorOnSort($e->getMessage());
41
        }
42
43 57
        return $compared;
44
    }
45
}
46