|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* Money |
|
4
|
|
|
* |
|
5
|
|
|
* @author Pronamic <[email protected]> |
|
6
|
|
|
* @copyright 2005-2018 Pronamic |
|
7
|
|
|
* @license GPL-3.0-or-later |
|
8
|
|
|
* @package Pronamic\WordPress\Money |
|
9
|
|
|
*/ |
|
10
|
|
|
|
|
11
|
|
|
namespace Pronamic\WordPress\Money; |
|
12
|
|
|
|
|
13
|
|
|
/** |
|
14
|
|
|
* Money |
|
15
|
|
|
* |
|
16
|
|
|
* @author Remco Tolsma |
|
17
|
|
|
* @version 1.0 |
|
18
|
|
|
*/ |
|
19
|
|
|
class Money { |
|
20
|
|
|
/** |
|
21
|
|
|
* Amount. |
|
22
|
|
|
* |
|
23
|
|
|
* @var float |
|
24
|
|
|
*/ |
|
25
|
|
|
private $amount; |
|
26
|
|
|
|
|
27
|
|
|
/** |
|
28
|
|
|
* Currency. |
|
29
|
|
|
* |
|
30
|
|
|
* @var Currency |
|
31
|
|
|
*/ |
|
32
|
|
|
private $currency; |
|
33
|
|
|
|
|
34
|
|
|
/** |
|
35
|
|
|
* Construct and initialize money object. |
|
36
|
|
|
* |
|
37
|
|
|
* @var float $amount |
|
38
|
|
|
* @var Currency|string|null $currency |
|
39
|
|
|
*/ |
|
40
|
|
|
public function __construct( $amount = 0, $currency = null ) { |
|
41
|
|
|
$this->set_amount( $amount ); |
|
42
|
|
|
$this->set_currency( $currency ); |
|
|
|
|
|
|
43
|
|
|
} |
|
44
|
|
|
|
|
45
|
|
|
public static function get_default_format() { |
|
46
|
|
|
/* translators: 1: currency symbol, 2: amount, 3: currency code */ |
|
47
|
|
|
$format = _x( '%1$s%2$s %3$s', 'money format', 'pronamic-money' ); |
|
48
|
|
|
|
|
49
|
|
|
$format = apply_filters( 'pronamic_money_default_format', $format ); |
|
50
|
|
|
|
|
51
|
|
|
return $format; |
|
52
|
|
|
} |
|
53
|
|
|
|
|
54
|
|
|
/** |
|
55
|
|
|
* Format i18n. |
|
56
|
|
|
* |
|
57
|
|
|
* @return string |
|
58
|
|
|
*/ |
|
59
|
|
|
public function format_i18n( $format = null ) { |
|
60
|
|
|
if ( is_null( $format ) ) { |
|
61
|
|
|
$format = self::get_default_format(); |
|
62
|
|
|
} |
|
63
|
|
|
|
|
64
|
|
|
if ( isset( $this->currency ) ) { |
|
65
|
|
|
return sprintf( |
|
66
|
|
|
$format, |
|
67
|
|
|
$this->currency->get_symbol(), |
|
68
|
|
|
number_format_i18n( $this->amount, $this->currency->get_number_decimals() ), |
|
69
|
|
|
$this->currency->get_alphabetic_code() |
|
70
|
|
|
); |
|
71
|
|
|
} |
|
72
|
|
|
|
|
73
|
|
|
return number_format_i18n( $this->amount, 2 ); |
|
74
|
|
|
} |
|
75
|
|
|
|
|
76
|
|
|
/** |
|
77
|
|
|
* Get amount. |
|
78
|
|
|
* |
|
79
|
|
|
* @return float float amount. |
|
80
|
|
|
*/ |
|
81
|
|
|
public function get_amount() { |
|
82
|
|
|
return $this->amount; |
|
83
|
|
|
} |
|
84
|
|
|
|
|
85
|
|
|
/** |
|
86
|
|
|
* Set amount. |
|
87
|
|
|
* |
|
88
|
|
|
* @param float $amount Amount. |
|
89
|
|
|
*/ |
|
90
|
|
|
public function set_amount( $amount ) { |
|
91
|
|
|
$this->amount = $amount; |
|
92
|
|
|
} |
|
93
|
|
|
|
|
94
|
|
|
/** |
|
95
|
|
|
* Get currency. |
|
96
|
|
|
* |
|
97
|
|
|
* @return Currency |
|
98
|
|
|
*/ |
|
99
|
|
|
public function get_currency() { |
|
100
|
|
|
return $this->currency; |
|
101
|
|
|
} |
|
102
|
|
|
|
|
103
|
|
|
/** |
|
104
|
|
|
* Set currency. |
|
105
|
|
|
* |
|
106
|
|
|
* @param string $currency Currency. |
|
107
|
|
|
*/ |
|
108
|
|
|
public function set_currency( $currency ) { |
|
109
|
|
|
if ( is_string( $currency ) ) { |
|
110
|
|
|
$currency = Currency::get_instance( $currency ); |
|
111
|
|
|
} |
|
112
|
|
|
|
|
113
|
|
|
$this->currency = $currency; |
|
114
|
|
|
} |
|
115
|
|
|
|
|
116
|
|
|
/** |
|
117
|
|
|
* Create a string representation of this money object. |
|
118
|
|
|
* |
|
119
|
|
|
* @return string |
|
120
|
|
|
*/ |
|
121
|
|
|
public function __toString() { |
|
122
|
|
|
return $this->format_i18n(); |
|
123
|
|
|
} |
|
124
|
|
|
} |
|
125
|
|
|
|
If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:
If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.