Passed
Push — master ( 8c9a09...69f647 )
by Sebastian
04:19
created

NumberInfo_Comparer::isSmallerThan()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 3
c 1
b 0
f 0
dl 0
loc 8
rs 10
cc 2
nc 2
nop 0
1
<?php
2
/**
3
 * File containing the class {@see NumberInfo_Comparer}.
4
 *
5
 * @package Application Utils
6
 * @subpackage NumberInfo
7
 * @see NumberInfo_Comparer
8
 */
9
10
declare(strict_types=1);
11
12
namespace AppUtils;
13
14
/**
15
 * Comparison tool for numbers using {@see NumberInfo} instances.
16
 *
17
 * @package Application Utils
18
 * @subpackage NumberInfo
19
 * @author Sebastian Mordziol <[email protected]>
20
 */
21
class NumberInfo_Comparer
22
{
23
    /**
24
     * @var NumberInfo
25
     */
26
    private $a;
27
28
    /**
29
     * @var NumberInfo
30
     */
31
    private $b;
32
33
    /**
34
     * @var bool
35
     */
36
    private $valid;
37
38
    public function __construct(NumberInfo $a, NumberInfo $b)
39
    {
40
        $this->a = $a;
41
        $this->b = $b;
42
43
        $this->valid = $this->validate();
44
    }
45
46
    /**
47
     * Ensures that the two numbers can be validated.
48
     *
49
     * Invalid numbers are:
50
     *
51
     * - Numbers with different units
52
     * - Empty numbers
53
     *
54
     * @return bool
55
     */
56
    private function validate() : bool
57
    {
58
        if($this->a->getUnits() !== $this->b->getUnits())
59
        {
60
            return false;
61
        }
62
63
        if($this->a->isEmpty() || $this->b->isEmpty())
64
        {
65
            return false;
66
        }
67
68
        return true;
69
    }
70
71
    public function isBiggerThan() : bool
72
    {
73
        if($this->valid === false)
74
        {
75
            return false;
76
        }
77
78
        return $this->a->getNumber() > $this->b->getNumber();
79
    }
80
81
    public function isBiggerEqual() : bool
82
    {
83
        if($this->valid === false)
84
        {
85
            return false;
86
        }
87
88
        return $this->a->getNumber() >= $this->b->getNumber();
89
    }
90
91
    public function isSmallerThan() : bool
92
    {
93
        if($this->valid === false)
94
        {
95
            return false;
96
        }
97
98
        return $this->a->getNumber() < $this->b->getNumber();
99
    }
100
101
    public function isSmallerEqual() : bool
102
    {
103
        if($this->valid === false)
104
        {
105
            return false;
106
        }
107
108
        return $this->a->getNumber() <= $this->b->getNumber();
109
    }
110
111
    public function isEqual() : bool
112
    {
113
        if($this->valid === false)
114
        {
115
            return false;
116
        }
117
118
        return $this->a->getNumber() === $this->b->getNumber();
119
    }
120
}
121