Passed
Push — master ( b1f105...c36d1c )
by Claudio
02:11
created

Key::sort()   A

Complexity

Conditions 2
Paths 1

Size

Total Lines 17
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 10
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 9
c 1
b 0
f 0
dl 0
loc 17
ccs 10
cts 10
cp 1
rs 9.9666
cc 2
nc 1
nop 1
crap 2
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Budgegeria\IntlSort\Sorter;
6
7
use Budgegeria\IntlSort\Exception\IntlSortException;
8
use Collator;
9
use function uksort;
10
11
final class Key implements Sorter
12
{
13
    /**
14
     * @var Collator
15
     */
16
    private $collator;
17
18 3
    public function __construct(Collator $collator)
19
    {
20 3
        $this->collator = $collator;
21 3
    }
22
23
    /**
24
     * {@inheritDoc}
25
     */
26 3
    public function sort(array $values) : array
27
    {
28 3
        $collator = $this->collator;
29 3
        uksort(
30 3
            $values,
31
            static function ($first, $second) use ($collator) : int {
32
                /** @var int|false $result */
33 3
                $result = $collator->compare((string) $first, (string) $second);
34 3
                if (false === $result) {
35 1
                    throw IntlSortException::errorOnSort();
36
                }
37
38 2
                return $result;
39 3
            }
40
        );
41
42 2
        return $values;
43
    }
44
}