Comparators::scalarComparator()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
eloc 2
dl 0
loc 4
ccs 2
cts 2
cp 1
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 0
crap 1
1
<?php
2
/**
3
 * @author Maxim Sokolovsky
4
 */
5
6
namespace WS\Utils\Collections\Functions;
7
8
use Closure;
9
10
class Comparators
11
{
12
    /**
13
     * Returns <Fn($a: scalar, $b: scalar): int> for scalar value compares such as int, bool, float, string
14
     * @return callable
15
     */
16 6
    public static function scalarComparator(): Closure
17
    {
18
        return static function ($a, $b) {
19 6
            return $a <=> $b;
20 6
        };
21
    }
22
23
    /**
24
     * Returns <Fn($a: object, $b: object): int>  for scalar object property value compares such as int, bool, float, string
25
     * @param string $property
26
     * @return callable
27
     */
28 6
    public static function objectPropertyComparator(string $property): Closure
29
    {
30
        return static function ($a, $b) use ($property) {
31 6
            return ObjectFunctions::getPropertyValue($a, $property) <=> ObjectFunctions::getPropertyValue($b, $property);
32 6
        };
33
    }
34
35
    /**
36
     * Returns <Fn($a: scalar, $b: scalar): int> for scalar value compares such as int, bool, float, string
37
     * @param callable $f <Fn(value: mixed): scalar>
38
     * @return Closure
39
     */
40 7
    public static function callbackComparator(callable $f): Closure
41
    {
42
        return static function ($a, $b) use ($f) {
43 7
            return $f($a) <=> $f($b);
44 7
        };
45
    }
46
}
47