Completed
Push — develop ( bb86d7...c508c0 )
by Remco
01:43
created

Money::get_currency()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
nc 1
nop 0
dl 0
loc 3
rs 10
c 0
b 0
f 0
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.0
18
 * @since   1.0.0
19
 */
20
class Money {
21
	/**
22
	 * Amount.
23
	 *
24
	 * @var float
25
	 */
26
	private $amount;
27
28
	/**
29
	 * Currency.
30
	 *
31
	 * @var Currency
32
	 */
33
	private $currency;
34
35
	/**
36
	 * Construct and initialize money object.
37
	 *
38
	 * @param string|int|float     $amount   Amount.
39
	 * @param Currency|string|null $currency Currency.
40
	 */
41
	public function __construct( $amount = 0, $currency = null ) {
42
		$this->set_amount( $amount );
43
		$this->set_currency( $currency );
0 ignored issues
show
Bug introduced by
It seems like $currency defined by parameter $currency on line 41 can also be of type null or object<Pronamic\WordPress\Money\Currency>; however, Pronamic\WordPress\Money\Money::set_currency() does only seem to accept string, maybe add an additional type check?

This check looks at variables that have been passed in as parameters and are passed out again to other methods.

If the outgoing method call has stricter type requirements than the method itself, an issue is raised.

An additional type check may prevent trouble.

Loading history...
44
	}
45
46
	/**
47
	 * Get default format.
48
	 *
49
	 * @return string
50
	 */
51
	public static function get_default_format() {
52
		/* translators: 1: currency symbol, 2: amount, 3: currency code, note: use non-breaking space! */
53
		$format = _x( '%1$s%2$s %3$s', 'money format', 'pronamic-money' );
54
		// Note:               ↳ Non-breaking space
55
56
		$format = apply_filters( 'pronamic_money_default_format', $format );
57
58
		return $format;
59
	}
60
61
	/**
62
	 * Format i18n.
63
	 *
64
	 * @param string|null $format Format.
65
	 *
66
	 * @return string
67
	 */
68
	public function format_i18n( $format = null ) {
69
		if ( is_null( $format ) ) {
70
			$format = self::get_default_format();
71
		}
72
73
		if ( isset( $this->currency ) ) {
74
			return sprintf(
75
				$format,
76
				$this->currency->get_symbol(),
77
				number_format_i18n( $this->amount, $this->currency->get_number_decimals() ),
78
				$this->currency->get_alphabetic_code()
79
			);
80
		}
81
82
		return number_format_i18n( $this->amount, 2 );
83
	}
84
85
	/**
86
	 * Get amount.
87
	 *
88
	 * @return float float amount.
89
	 */
90
	public function get_amount() {
91
		return $this->amount;
92
	}
93
94
	/**
95
	 * Get cents.
96
	 *
97
	 * @return float 
98
	 */
99
	public function get_cents() {
100
		return $this->amount * 100;
101
	}
102
103
	/**
104
	 * Set amount.
105
	 *
106
	 * @param mixed $amount Amount.
107
	 */
108
	public function set_amount( $amount ) {
109
		$this->amount = floatval( $amount );
110
	}
111
112
	/**
113
	 * Get currency.
114
	 *
115
	 * @return Currency
116
	 */
117
	public function get_currency() {
118
		return $this->currency;
119
	}
120
121
	/**
122
	 * Set currency.
123
	 *
124
	 * @param string $currency Currency.
125
	 */
126
	public function set_currency( $currency ) {
127
		if ( is_object( $currency ) && is_a( $currency, __NAMESPACE__ . '\Currency' ) ) {
128
			$this->currency = $currency;
129
130
			return;
131
		}
132
133
		$this->currency = Currency::get_instance( $currency );
134
	}
135
136
	/**
137
	 * Create a string representation of this money object.
138
	 *
139
	 * @return string
140
	 */
141
	public function __toString() {
142
		return $this->format_i18n();
143
	}
144
}
145