Passed
Push — master ( 89d4ac...68779c )
by Claudio
02:14
created

ConfigurableCollator::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 2
Code Lines 0

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

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 0
cts 1
cp 0
crap 2
rs 10
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Budgegeria\IntlSort\Collator;
6
7
use Collator as IntlCollator;
8
9
use function assert;
10
use function is_int;
11
12
class ConfigurableCollator implements Collator
13
{
14
    public function __construct(private IntlCollator $collator, private Configuration $config)
15
    {
16
    }
17
18
    public function compare(mixed $value, mixed $comparativeValue): int
19
    {
20
        if (($comparativeValue === null || $value === null) && $this->config->getNullableSort() === Configuration::NULL_VALUES_LAST) {
21
            return $comparativeValue <=> $value;
22
        }
23
24
        if (($comparativeValue === null || $value === null) && $this->config->getNullableSort() === Configuration::NULL_VALUES_FIRST) {
25
            return Configuration::NULL_VALUES_FIRST;
26
        }
27
28
        $result = $this->collator->compare((string) $value, (string) $comparativeValue);
29
30
        assert(is_int($result));
31
32
        return $result;
33
    }
34
}
35