Passed
Push — master ( 8afc73...99d80c )
by Edward
04:17
created

GreaterValueComparator::isScalarGreater()   B

Complexity

Conditions 7
Paths 3

Size

Total Lines 13
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
cc 7
eloc 7
c 1
b 0
f 1
nc 3
nop 2
dl 0
loc 13
rs 8.8333
1
<?php
2
declare(strict_types=1);
3
4
namespace Remorhaz\JSON\Data\Comparator;
5
6
use Collator;
7
use function is_float;
8
use function is_int;
9
use function is_string;
10
use Remorhaz\JSON\Data\Value\ScalarValueInterface;
11
use Remorhaz\JSON\Data\Value\ValueInterface;
12
13
final class GreaterValueComparator implements ComparatorInterface
14
{
15
16
    private $collator;
17
18
    public function __construct(Collator $collator)
19
    {
20
        $this->collator = $collator;
21
    }
22
23
    public function compare(ValueInterface $leftValue, ValueInterface $rightValue): bool
24
    {
25
        if ($leftValue instanceof ScalarValueInterface && $rightValue instanceof ScalarValueInterface) {
26
            return $this->isScalarGreater($leftValue, $rightValue);
27
        }
28
29
        return false;
30
    }
31
32
    private function isScalarGreater(ScalarValueInterface $leftValue, ScalarValueInterface $rightValue): bool
33
    {
34
        $leftData = $leftValue->getData();
35
        $rightData = $rightValue->getData();
36
        if ((is_int($leftData) || is_float($leftData)) && (is_int($rightData) || is_float($rightData))) {
37
            return $leftData > $rightData;
38
        }
39
40
        if (is_string($leftData) && is_string($rightData)) {
41
            return 1 === $this->collator->compare($leftData, $rightData);
42
        }
43
44
        return false;
45
    }
46
}
47