pronamic /
wp-money
These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more
| 1 | <?php |
||
| 2 | /** |
||
| 3 | * Money |
||
| 4 | * |
||
| 5 | * @author Pronamic <[email protected]> |
||
| 6 | * @copyright 2005-2019 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.2.0 |
||
| 18 | * @since 1.0.0 |
||
| 19 | */ |
||
| 20 | class Money { |
||
| 21 | /** |
||
| 22 | * Amount value. |
||
| 23 | * |
||
| 24 | * @var float |
||
| 25 | */ |
||
| 26 | private $value; |
||
| 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 $value Amount value. |
||
| 39 | * @param Currency|string|null $currency Currency. |
||
| 40 | */ |
||
| 41 | public function __construct( $value = 0, $currency = null ) { |
||
| 42 | $this->set_value( $value ); |
||
| 43 | $this->set_currency( $currency ); |
||
|
0 ignored issues
–
show
|
|||
| 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 value, 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 | $format = apply_filters( 'pronamic_money_default_format', $format ); |
||
| 56 | |||
| 57 | return $format; |
||
| 58 | } |
||
| 59 | |||
| 60 | /** |
||
| 61 | * Format i18n. |
||
| 62 | * |
||
| 63 | * @param string|null $format Format. |
||
| 64 | * |
||
| 65 | * @return string |
||
| 66 | */ |
||
| 67 | public function format_i18n( $format = null ) { |
||
| 68 | if ( is_null( $format ) ) { |
||
| 69 | $format = self::get_default_format(); |
||
| 70 | } |
||
| 71 | |||
| 72 | if ( isset( $this->currency ) ) { |
||
| 73 | return sprintf( |
||
| 74 | $format, |
||
| 75 | $this->currency->get_symbol(), |
||
| 76 | number_format_i18n( $this->get_value(), $this->currency->get_number_decimals() ), |
||
| 77 | $this->currency->get_alphabetic_code() |
||
| 78 | ); |
||
| 79 | } |
||
| 80 | |||
| 81 | return number_format_i18n( $this->get_value(), 2 ); |
||
| 82 | } |
||
| 83 | |||
| 84 | /** |
||
| 85 | * Get value. |
||
| 86 | * |
||
| 87 | * @return float Amount value. |
||
| 88 | */ |
||
| 89 | public function get_value() { |
||
| 90 | return $this->value; |
||
| 91 | } |
||
| 92 | |||
| 93 | /** |
||
| 94 | * Get amount. |
||
| 95 | * |
||
| 96 | * @deprecated 1.2.0 |
||
| 97 | * @return float Amount value. |
||
| 98 | */ |
||
| 99 | public function get_amount() { |
||
| 100 | _deprecated_function( __METHOD__, '1.2.0', 'Money::get_value()' ); |
||
| 101 | |||
| 102 | return $this->get_value(); |
||
| 103 | } |
||
| 104 | |||
| 105 | /** |
||
| 106 | * Get cents. |
||
| 107 | * |
||
| 108 | * @return float |
||
| 109 | */ |
||
| 110 | public function get_cents() { |
||
| 111 | return $this->value * 100; |
||
| 112 | } |
||
| 113 | |||
| 114 | /** |
||
| 115 | * Get amount in minor units. |
||
| 116 | * |
||
| 117 | * Examples for value 10: |
||
| 118 | * JPY 0 decimals: 10 |
||
| 119 | * EUR 2 decimals: 1000 |
||
| 120 | * BHD 3 decimals: 10000 |
||
| 121 | * NLG 4 decimals: 100000 |
||
| 122 | * |
||
| 123 | * @return int |
||
| 124 | */ |
||
| 125 | public function get_minor_units() { |
||
| 126 | // Use 2 decimals by default (most common). |
||
| 127 | $decimals = 2; |
||
| 128 | |||
| 129 | // Get number of decimals from currency if available. |
||
| 130 | if ( $this->get_currency() ) { |
||
| 131 | $decimals = $this->currency->get_number_decimals(); |
||
| 132 | } |
||
| 133 | |||
| 134 | // Return amount in minor units. |
||
| 135 | if ( function_exists( 'bcmul' ) ) { |
||
| 136 | $minor_units = bcmul( $this->value, pow( 10, $decimals ), 0 ); |
||
| 137 | } else { |
||
| 138 | $minor_units = $this->value * pow( 10, $decimals ); |
||
| 139 | } |
||
| 140 | |||
| 141 | return (int) (string) $minor_units; |
||
| 142 | } |
||
| 143 | |||
| 144 | /** |
||
| 145 | * Set value. |
||
| 146 | * |
||
| 147 | * @param mixed $value Amount value. |
||
| 148 | */ |
||
| 149 | public function set_value( $value ) { |
||
| 150 | $this->value = floatval( $value ); |
||
| 151 | } |
||
| 152 | |||
| 153 | /** |
||
| 154 | * Set amount. |
||
| 155 | * |
||
| 156 | * @deprecated 1.2.0 |
||
| 157 | * @param mixed $value Amount value. |
||
| 158 | */ |
||
| 159 | public function set_amount( $value ) { |
||
| 160 | _deprecated_function( __METHOD__, '1.2.0', 'Money::set_value()' ); |
||
| 161 | |||
| 162 | $this->set_value( $value ); |
||
| 163 | } |
||
| 164 | |||
| 165 | /** |
||
| 166 | * Get currency. |
||
| 167 | * |
||
| 168 | * @return Currency |
||
| 169 | */ |
||
| 170 | public function get_currency() { |
||
| 171 | return $this->currency; |
||
| 172 | } |
||
| 173 | |||
| 174 | /** |
||
| 175 | * Set currency. |
||
| 176 | * |
||
| 177 | * @param string $currency Currency. |
||
| 178 | */ |
||
| 179 | public function set_currency( $currency ) { |
||
| 180 | if ( is_object( $currency ) && is_a( $currency, __NAMESPACE__ . '\Currency' ) ) { |
||
| 181 | $this->currency = $currency; |
||
| 182 | |||
| 183 | return; |
||
| 184 | } |
||
| 185 | |||
| 186 | $this->currency = Currency::get_instance( $currency ); |
||
| 187 | } |
||
| 188 | |||
| 189 | /** |
||
| 190 | * Create a string representation of this money object. |
||
| 191 | * |
||
| 192 | * @return string |
||
| 193 | */ |
||
| 194 | public function __toString() { |
||
| 195 | return $this->format_i18n(); |
||
| 196 | } |
||
| 197 | } |
||
| 198 |
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.