Controllers
1 week ago
DigitalProducts
5 months ago
Emails
3 months ago
Models
1 month ago
PaymentMethods
1 month ago
Repositories
2 months ago
Services
4 months ago
CheckoutFields.php
1 month ago
CurrencyFormatter.php
9 months ago
Init.php
2 years ago
StatSync.php
1 year ago
index.php
3 years ago
CurrencyFormatter.php
231 lines
| 1 | <?php |
| 2 | |
| 3 | namespace ProfilePress\Core\Membership; |
| 4 | |
| 5 | use ProfilePress\Core\Membership\Services\Calculator; |
| 6 | |
| 7 | class CurrencyFormatter |
| 8 | { |
| 9 | private $amount; |
| 10 | |
| 11 | /** |
| 12 | * @var string Original, unmodified amount passed in via constructor. |
| 13 | */ |
| 14 | private $original_amount; |
| 15 | |
| 16 | private $currency_code; |
| 17 | |
| 18 | private $symbol; |
| 19 | |
| 20 | private $decimal_separator; |
| 21 | |
| 22 | private $thousands_separator; |
| 23 | |
| 24 | private $number_decimals; |
| 25 | |
| 26 | private $currency_position; |
| 27 | |
| 28 | /** |
| 29 | * @var string Symbol/text to display before amounts. |
| 30 | */ |
| 31 | private $prefix = ''; |
| 32 | |
| 33 | /** |
| 34 | * @var string Symbol/text to display after amounts. |
| 35 | */ |
| 36 | private $suffix = ''; |
| 37 | |
| 38 | /** |
| 39 | * CurrencyFormatter constructor. |
| 40 | * |
| 41 | * @param string $amount |
| 42 | * @param string $currency_code |
| 43 | */ |
| 44 | public function __construct($amount, $currency_code = '') |
| 45 | { |
| 46 | $this->amount = $this->original_amount = $amount; |
| 47 | |
| 48 | $this->currency_code = strtoupper(! empty($currency_code) ? $currency_code : ppress_get_currency()); |
| 49 | |
| 50 | $this->symbol = html_entity_decode(ppress_get_currency_symbol($this->currency_code)); |
| 51 | |
| 52 | $this->currency_position = apply_filters( |
| 53 | 'ppress_currency_position', |
| 54 | ppress_get_setting('currency_position', 'left', true), |
| 55 | $this->currency_code |
| 56 | ); |
| 57 | |
| 58 | $this->number_decimals = apply_filters( |
| 59 | 'ppress_currency_decimal_number', |
| 60 | ppress_get_setting('currency_decimal_number', '2', true), |
| 61 | $this->currency_code |
| 62 | ); |
| 63 | |
| 64 | $this->decimal_separator = apply_filters( |
| 65 | 'ppress_currency_decimal_separator', |
| 66 | ppress_get_setting('currency_decimal_separator', '.', true), |
| 67 | $this->currency_code |
| 68 | ); |
| 69 | |
| 70 | $this->thousands_separator = apply_filters( |
| 71 | 'ppress_currency_thousands_separator', |
| 72 | ppress_get_setting('currency_thousand_separator', ',', true), |
| 73 | $this->currency_code |
| 74 | ); |
| 75 | |
| 76 | switch ($this->currency_position) { |
| 77 | case 'left_space': |
| 78 | $this->prefix = $this->symbol . ' '; |
| 79 | break; |
| 80 | case 'left': |
| 81 | $this->prefix = $this->symbol; |
| 82 | break; |
| 83 | case 'right_space': |
| 84 | $this->suffix = ' ' . $this->symbol; |
| 85 | break; |
| 86 | case 'right': |
| 87 | $this->suffix = $this->symbol; |
| 88 | break; |
| 89 | } |
| 90 | |
| 91 | $this->prefix = apply_filters('ppress_currency_prefix', $this->prefix, $this->currency_code); |
| 92 | |
| 93 | $this->suffix = apply_filters('ppress_currency_suffix', $this->suffix, $this->currency_code); |
| 94 | } |
| 95 | |
| 96 | /** |
| 97 | * Un-formats an amount. |
| 98 | * This ensures the amount is put into a state where we can perform mathematical |
| 99 | * operations on it --- that means using `.` as the decimal separator and no |
| 100 | * thousands separator. |
| 101 | * |
| 102 | * @return string |
| 103 | */ |
| 104 | private function unformat() |
| 105 | { |
| 106 | $amount = $this->amount; |
| 107 | |
| 108 | if (Calculator::init($amount)->isNegativeOrZero()) $amount = '0'; |
| 109 | |
| 110 | $sep_found = strpos($amount, $this->decimal_separator); |
| 111 | if (',' === $this->decimal_separator && false !== $sep_found) { |
| 112 | $whole = substr($amount, 0, $sep_found); |
| 113 | $part = substr($amount, $sep_found + 1, (strlen($amount) - 1)); |
| 114 | $amount = $whole . '.' . $part; |
| 115 | } |
| 116 | |
| 117 | // Strip "," and " " from the amount (if set as the thousands separator). |
| 118 | foreach ([',', ' '] as $thousands_separator) { |
| 119 | if ($thousands_separator === $this->thousands_separator && false !== strpos($amount, $this->thousands_separator)) { |
| 120 | $amount = str_replace($thousands_separator, '', $amount); |
| 121 | } |
| 122 | } |
| 123 | |
| 124 | // one last formatting check especially when amount contains currency |
| 125 | $amount = preg_replace('/[^0-9\.]/', '', $amount); |
| 126 | |
| 127 | return $amount; |
| 128 | } |
| 129 | |
| 130 | /** |
| 131 | * Formats the amount for display. |
| 132 | * Does not apply the currency code. |
| 133 | * |
| 134 | * @param bool $decimals |
| 135 | * |
| 136 | * @return self |
| 137 | */ |
| 138 | public function format($decimals = true) |
| 139 | { |
| 140 | $amount = $this->unformat(); |
| 141 | |
| 142 | if (empty($amount)) $amount = 0; |
| 143 | |
| 144 | $decimals = apply_filters('ppress_format_amount_decimals', $decimals ? $this->number_decimals : 0, $amount, $this->currency_code); |
| 145 | |
| 146 | // Format amount using decimals and separators (also rounds up or down) |
| 147 | $formatted = number_format((float)$amount, $decimals, $this->decimal_separator, $this->thousands_separator); |
| 148 | |
| 149 | $this->amount = apply_filters('ppress_format_amount', $formatted, $amount, $decimals, $this->decimal_separator, $this->thousands_separator, $this->currency_code); |
| 150 | |
| 151 | return $this; |
| 152 | } |
| 153 | |
| 154 | /** |
| 155 | * Formats the amount for display. |
| 156 | * Does not apply the currency code. |
| 157 | * |
| 158 | * @param bool $decimals |
| 159 | * |
| 160 | * @return self |
| 161 | */ |
| 162 | public function sanitize($decimals = true) |
| 163 | { |
| 164 | $amount = $this->unformat(); |
| 165 | |
| 166 | if (empty($amount)) $amount = 0; |
| 167 | |
| 168 | $decimals = (int)apply_filters('ppress_format_amount_decimals', $decimals ? $this->number_decimals : 0, $amount, $this->currency_code); |
| 169 | |
| 170 | $formatted = Calculator::init($amount)->toScale($decimals)->val(); |
| 171 | |
| 172 | $this->amount = apply_filters('ppress_sanitize_amount', $formatted, $amount, $decimals, $this->decimal_separator, $this->thousands_separator, $this->currency_code); |
| 173 | |
| 174 | return $this; |
| 175 | } |
| 176 | |
| 177 | /** |
| 178 | * Applies the currency prefix/suffix to the amount. |
| 179 | * |
| 180 | * @return self |
| 181 | */ |
| 182 | public function apply_symbol() |
| 183 | { |
| 184 | $amount = $this->amount; |
| 185 | $is_negative = is_numeric($this->amount) && $this->amount < 0; |
| 186 | |
| 187 | // Remove "-" from start. |
| 188 | if ($is_negative) { |
| 189 | $amount = substr($amount, 1); |
| 190 | } |
| 191 | |
| 192 | $formatted = ''; |
| 193 | if ( ! empty($this->prefix)) { |
| 194 | $formatted .= $this->prefix; |
| 195 | } |
| 196 | |
| 197 | $formatted .= $amount; |
| 198 | |
| 199 | if ( ! empty($this->suffix)) { |
| 200 | $formatted .= $this->suffix; |
| 201 | } |
| 202 | |
| 203 | if ( ! empty($this->prefix)) { |
| 204 | $formatted = apply_filters('ppress_' . strtolower($this->currency_code) . '_currency_filter_before', $formatted, $this->currency_code, $amount); |
| 205 | } |
| 206 | |
| 207 | if ( ! empty($this->suffix)) { |
| 208 | $formatted = apply_filters('ppress_' . strtolower($this->currency_code) . '_currency_filter_after', $formatted, $this->currency_code, $amount); |
| 209 | } |
| 210 | |
| 211 | // Add the "-" sign back to the start of the string. |
| 212 | if ($is_negative) { |
| 213 | $formatted = '-' . $formatted; |
| 214 | } |
| 215 | |
| 216 | $this->amount = $formatted; |
| 217 | |
| 218 | return $this; |
| 219 | } |
| 220 | |
| 221 | /** |
| 222 | * Current working amount. |
| 223 | * |
| 224 | * @return mixed |
| 225 | */ |
| 226 | public function val() |
| 227 | { |
| 228 | return (string)$this->amount; |
| 229 | } |
| 230 | } |
| 231 |