GreaterValueComparator   A
last analyzed

Complexity

Total Complexity 11

Size/Duplication

Total Lines 32
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
eloc 12
c 1
b 0
f 1
dl 0
loc 32
rs 10
wmc 11

3 Methods

Rating   Name   Duplication   Size   Complexity  
B isScalarGreater() 0 13 7
A compare() 0 7 3
A __construct() 0 3 1
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