GreaterValueComparator::isScalarGreater()   B
last analyzed

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