Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
63.64% |
7 / 11 |
|
50.00% |
2 / 4 |
CRAP | |
0.00% |
0 / 1 |
| Money | |
63.64% |
7 / 11 |
|
50.00% |
2 / 4 |
11.08 | |
0.00% |
0 / 1 |
| setLocalization | |
100.00% |
4 / 4 |
|
100.00% |
1 / 1 |
1 | |||
| getCurrency | |
100.00% |
3 / 3 |
|
100.00% |
1 / 1 |
5 | |||
| getCurrencySymbol | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| fromFloatInt | |
0.00% |
0 / 3 |
|
0.00% |
0 / 1 |
2 | |||
| 1 | <?php |
| 2 | /** |
| 3 | * Jingga |
| 4 | * |
| 5 | * PHP Version 8.1 |
| 6 | * |
| 7 | * @package phpOMS\Localization |
| 8 | * @copyright Dennis Eichhorn |
| 9 | * @license OMS License 2.0 |
| 10 | * @version 1.0.0 |
| 11 | * @link https://jingga.app |
| 12 | */ |
| 13 | declare(strict_types=1); |
| 14 | |
| 15 | namespace phpOMS\Localization; |
| 16 | |
| 17 | use phpOMS\Stdlib\Base\FloatInt; |
| 18 | |
| 19 | /** |
| 20 | * Money class. |
| 21 | * |
| 22 | * @package phpOMS\Localization |
| 23 | * @license OMS License 2.0 |
| 24 | * @link https://jingga.app |
| 25 | * @since 1.0.0 |
| 26 | */ |
| 27 | final class Money extends FloatInt |
| 28 | { |
| 29 | /** |
| 30 | * Currency symbol position |
| 31 | * |
| 32 | * @var int |
| 33 | * @since 1.0.0 |
| 34 | */ |
| 35 | private int $position = 0; |
| 36 | |
| 37 | /** |
| 38 | * Currency symbol. |
| 39 | * |
| 40 | * @var string |
| 41 | * @since 1.0.0 |
| 42 | */ |
| 43 | private string $symbol = ISO4217SymbolEnum::_USD; |
| 44 | |
| 45 | /** |
| 46 | * Set localization. |
| 47 | * |
| 48 | * @param string $thousands Thousands separator |
| 49 | * @param string $decimal Decimal separator |
| 50 | * @param string $symbol Currency symbol |
| 51 | * @param int $position Symbol position |
| 52 | * |
| 53 | * @return Money |
| 54 | * |
| 55 | * @since 1.0.0 |
| 56 | */ |
| 57 | public function setLocalization(string $thousands = ',', string $decimal = '.', string $symbol = '', int $position = 0) : self |
| 58 | { |
| 59 | $this->symbol = $symbol; |
| 60 | $this->position = $position; |
| 61 | |
| 62 | parent::setLocalization($thousands, $decimal); |
| 63 | |
| 64 | return $this; |
| 65 | } |
| 66 | |
| 67 | /** |
| 68 | * Get money. |
| 69 | * |
| 70 | * @param int $decimals Precision (null = auto decimals) |
| 71 | * |
| 72 | * @return string |
| 73 | * |
| 74 | * @since 1.0.0 |
| 75 | */ |
| 76 | public function getCurrency(?int $decimals = 2, int $position = null, string $symbol = null) : string |
| 77 | { |
| 78 | return (($position ?? $this->position) === 0 && !empty($symbol ?? $this->symbol) ? ($symbol ?? $this->symbol) . ' ' : '') |
| 79 | . $this->getAmount($decimals) |
| 80 | . (($position ?? $this->position) === 1 && !empty($symbol ?? $this->symbol) ? ' ' . ($symbol ?? $this->symbol) : ''); |
| 81 | } |
| 82 | |
| 83 | /** |
| 84 | * Get currency symbol |
| 85 | * |
| 86 | * @return string |
| 87 | * @since 1.0.0 |
| 88 | */ |
| 89 | public function getCurrencySymbol() : string |
| 90 | { |
| 91 | return $this->symbol; |
| 92 | } |
| 93 | |
| 94 | /** |
| 95 | * Create Money from FloatInt. |
| 96 | * |
| 97 | * @param FloatInt $value FloatInt value |
| 98 | * |
| 99 | * @return self |
| 100 | */ |
| 101 | public static function fromFloatInt(FloatInt $value) : self |
| 102 | { |
| 103 | $money = new self(); |
| 104 | $money->value = $value->value; |
| 105 | |
| 106 | return $money; |
| 107 | } |
| 108 | } |