|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* This file is part of the O2System Reactor package. |
|
4
|
|
|
* |
|
5
|
|
|
* For the full copyright and license information, please view the LICENSE |
|
6
|
|
|
* file that was distributed with this source code. |
|
7
|
|
|
* |
|
8
|
|
|
* @author Steeve Andrian Salim |
|
9
|
|
|
* @copyright Copyright (c) Steeve Andrian Salim |
|
10
|
|
|
*/ |
|
11
|
|
|
|
|
12
|
|
|
// ------------------------------------------------------------------------ |
|
13
|
|
|
|
|
14
|
|
|
namespace O2System\Reactor\DataStructures\Commons; |
|
15
|
|
|
|
|
16
|
|
|
// ------------------------------------------------------------------------ |
|
17
|
|
|
|
|
18
|
|
|
use O2System\Spl\Patterns\Structural\Repository\AbstractRepository; |
|
19
|
|
|
|
|
20
|
|
|
/** |
|
21
|
|
|
* Class Money |
|
22
|
|
|
* @package O2System\Reactor\DataStructures\Commons |
|
23
|
|
|
*/ |
|
24
|
|
|
class Money extends AbstractRepository |
|
25
|
|
|
{ |
|
26
|
|
|
/** |
|
27
|
|
|
* Money::__construct |
|
28
|
|
|
* |
|
29
|
|
|
* @param int $amount |
|
30
|
|
|
*/ |
|
31
|
|
|
public function __construct($amount) |
|
32
|
|
|
{ |
|
33
|
|
|
$money = []; |
|
34
|
|
|
if (is_numeric($amount)) { |
|
|
|
|
|
|
35
|
|
|
$money[ 'amount' ] = (int)$amount; |
|
36
|
|
|
} elseif (is_array($amount)) { |
|
37
|
|
|
$money = $amount; |
|
38
|
|
|
} |
|
39
|
|
|
|
|
40
|
|
|
(int)$storage[ 'amount' ] = 0; |
|
|
|
|
|
|
41
|
|
|
$storage[ 'currency' ] = config()->getItem('units')->currency; |
|
|
|
|
|
|
42
|
|
|
|
|
43
|
|
|
$storage = array_merge($storage, $money); |
|
44
|
|
|
(int)$storage[ 'amount' ] = empty($storage[ 'amount' ]) ? 0 : abs($storage[ 'amount' ]); |
|
45
|
|
|
|
|
46
|
|
|
$this->storage = $storage; |
|
47
|
|
|
} |
|
48
|
|
|
|
|
49
|
|
|
// ------------------------------------------------------------------------ |
|
50
|
|
|
|
|
51
|
|
|
/** |
|
52
|
|
|
* Money::shortFormat |
|
53
|
|
|
* |
|
54
|
|
|
* @param int $decimals |
|
55
|
|
|
* |
|
56
|
|
|
* @return string |
|
57
|
|
|
*/ |
|
58
|
|
|
public function shortFormat($decimals = 0) |
|
59
|
|
|
{ |
|
60
|
|
|
return short_format($this->amount, $decimals); |
|
|
|
|
|
|
61
|
|
|
} |
|
62
|
|
|
|
|
63
|
|
|
// ------------------------------------------------------------------------ |
|
64
|
|
|
|
|
65
|
|
|
/** |
|
66
|
|
|
* Money::numberFormat |
|
67
|
|
|
* |
|
68
|
|
|
* @param int $decimals |
|
69
|
|
|
* @param string $thousandSeparator |
|
70
|
|
|
* @param string $decimalSeparator |
|
71
|
|
|
* |
|
72
|
|
|
* @return string |
|
73
|
|
|
*/ |
|
74
|
|
|
public function numberFormat($decimals = 0, $thousandSeparator = '.', $decimalSeparator = ',') |
|
|
|
|
|
|
75
|
|
|
{ |
|
76
|
|
|
$decimalSeparator = $thousandSeparator === '.' ? ',' : '.'; |
|
77
|
|
|
|
|
78
|
|
|
return number_format($this->amount, $decimals, $decimalSeparator, $thousandSeparator); |
|
|
|
|
|
|
79
|
|
|
} |
|
80
|
|
|
|
|
81
|
|
|
// ------------------------------------------------------------------------ |
|
82
|
|
|
|
|
83
|
|
|
/** |
|
84
|
|
|
* Money::isSufficient |
|
85
|
|
|
* |
|
86
|
|
|
* @param int $amount |
|
87
|
|
|
* |
|
88
|
|
|
* @return bool |
|
89
|
|
|
*/ |
|
90
|
|
|
public function isSufficient($amount) |
|
91
|
|
|
{ |
|
92
|
|
|
if ($amount < $this->amount) { |
|
|
|
|
|
|
93
|
|
|
return true; |
|
94
|
|
|
} |
|
95
|
|
|
|
|
96
|
|
|
return false; |
|
97
|
|
|
} |
|
98
|
|
|
|
|
99
|
|
|
// ------------------------------------------------------------------------ |
|
100
|
|
|
|
|
101
|
|
|
/** |
|
102
|
|
|
* Money::__toString |
|
103
|
|
|
* |
|
104
|
|
|
* @return string |
|
105
|
|
|
*/ |
|
106
|
|
|
public function __toString() |
|
107
|
|
|
{ |
|
108
|
|
|
loader()->loadHelper('number'); |
|
109
|
|
|
|
|
110
|
|
|
return $this->currencyFormat(); |
|
111
|
|
|
} |
|
112
|
|
|
|
|
113
|
|
|
// ------------------------------------------------------------------------ |
|
114
|
|
|
|
|
115
|
|
|
/** |
|
116
|
|
|
* Money::currencyFormat |
|
117
|
|
|
* |
|
118
|
|
|
* @param string $locale |
|
119
|
|
|
* @param string $currency |
|
120
|
|
|
* @param bool $addSpace |
|
121
|
|
|
* |
|
122
|
|
|
* @return string |
|
123
|
|
|
*/ |
|
124
|
|
|
public function currencyFormat($locale = 'id_ID', $currency = 'IDR', $addSpace = true) |
|
125
|
|
|
{ |
|
126
|
|
|
return currency_format($this->amount, $locale, $currency, $addSpace); |
|
|
|
|
|
|
127
|
|
|
} |
|
128
|
|
|
} |