CurrencyNumberInfo::isNegative()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 1
nc 1
nop 0
dl 0
loc 3
rs 10
c 1
b 0
f 0
1
<?php
2
/**
3
 * @package Localization
4
 * @subpackage Currencies
5
 */
6
7
declare(strict_types=1);
8
9
namespace AppLocalize\Localization\Currencies;
10
11
/**
12
 * Number container for currency price notations, used to
13
 * provide a simple API to work with currency-specific
14
 * price notations.
15
 *
16
 * @package Localization
17
 * @subpackage Currencies
18
 * @author Sebastian Mordziol <[email protected]>
19
 * @link http://www.mistralys.com
20
 * @see BaseCurrency::tryParseNumber()
21
 */
22
class CurrencyNumberInfo
23
{
24
    protected int $number;
25
    protected int $decimals;
26
27
    /**
28
     * @var float
29
     */
30
    protected $float;
31
32
    /**
33
     * @param int $number
34
     * @param int $decimals
35
     */
36
    public function __construct(int $number, int $decimals = 0)
37
    {
38
        $this->number = $number;
39
        $this->decimals = $decimals;
40
    }
41
42
    public function isNegative() : bool
43
    {
44
        return $this->number < 0;
45
    }
46
47
    /**
48
     * Gets the number as a float, e.g. 100.25
49
     * @return float
50
     */
51
    public function getFloat() : float
52
    {
53
        return (float)($this->getString());
54
    }
55
56
    /**
57
     * Returns the number without decimals (positive integer). e.g. 100
58
     * @return int
59
     */
60
    public function getNumber() : int
61
    {
62
        return $this->number;
63
    }
64
65
    /**
66
     * Returns the decimals of the number, if any.
67
     * @return int
68
     */
69
    public function getDecimals() : int
70
    {
71
        return $this->decimals;
72
    }
73
74
    /**
75
     * Counts the number of decimals in the number.
76
     * @return int
77
     */
78
    public function countDecimals() : int
79
    {
80
        if ($this->decimals === 0) {
81
            return 0;
82
        }
83
84
        return strlen((string)$this->decimals);
85
    }
86
87
    public function getString() : string
88
    {
89
        return $this->number . '.' . $this->decimals;
90
    }
91
}
92