| 1 |
<?php |
| 2 |
|
| 3 |
/** |
| 4 |
* Exit if accessed directly |
| 5 |
*/ |
| 6 |
if ( ! defined( 'ABSPATH' ) ) { |
| 7 |
exit; |
| 8 |
} |
| 9 |
|
| 10 |
if ( ! class_exists( 'Dfrapi_Price' ) ) { |
| 11 |
|
| 12 |
class Dfrapi_Price { |
| 13 |
|
| 14 |
/** @var int Default number of decimal places to use when displaying prices. */ |
| 15 |
const DEFAULT_DECIMAL_PLACES = 2; |
| 16 |
|
| 17 |
/** @var bool Whether or not to trim trailing double zeros. Default false. */ |
| 18 |
const TRIM_TRAILING_DOUBLE_ZEROS = false; |
| 19 |
|
| 20 |
/** @var Dfrapi_Currency $currency */ |
| 21 |
public $currency; |
| 22 |
|
| 23 |
/** @var string|integer|numeric $value The original value pass to constructor to be used as the price. */ |
| 24 |
private $value; |
| 25 |
|
| 26 |
/** @var mixed $context The context we are displaying the price in. */ |
| 27 |
private $context; |
| 28 |
|
| 29 |
/** @var integer $int The $value converted into an integer format. */ |
| 30 |
private $int; |
| 31 |
|
| 32 |
/** |
| 33 |
* Dfrapi_Price constructor. |
| 34 |
* |
| 35 |
* @param mixed $value |
| 36 |
* @param Dfrapi_Currency $currency |
| 37 |
* @param mixed $context |
| 38 |
*/ |
| 39 |
public function __construct( $value, Dfrapi_Currency $currency, $context = null ) { |
| 40 |
$this->value = $value; |
| 41 |
$this->int = dfrapi_intify( $value ); |
| 42 |
$this->currency = $currency; |
| 43 |
$this->context = $context; |
| 44 |
} |
| 45 |
|
| 46 |
/** |
| 47 |
* Returns a price formatted with decimal, thousands separator, currency symbols and any other |
| 48 |
* data that appears in the "format" field of the currency. |
| 49 |
* |
| 50 |
* @return string |
| 51 |
*/ |
| 52 |
public function get_price() { |
| 53 |
return apply_filters( 'dfrapi_price', strtr( $this->get_signed_format(), $this->get_translation_pairs() ), $this ); |
| 54 |
} |
| 55 |
|
| 56 |
/** |
| 57 |
* Returns the initial $value passed in the constructor. |
| 58 |
* |
| 59 |
* @return float|int|string |
| 60 |
*/ |
| 61 |
public function get_value() { |
| 62 |
return $this->value; |
| 63 |
} |
| 64 |
|
| 65 |
/** |
| 66 |
* Returns the $value after its been "intified". |
| 67 |
* |
| 68 |
* @return int |
| 69 |
*/ |
| 70 |
public function get_int() { |
| 71 |
return $this->int; |
| 72 |
} |
| 73 |
|
| 74 |
/** |
| 75 |
* Returns the initial $context passed in the constructor. |
| 76 |
* |
| 77 |
* @return mixed |
| 78 |
*/ |
| 79 |
public function get_context() { |
| 80 |
return $this->context; |
| 81 |
} |
| 82 |
|
| 83 |
/** |
| 84 |
* Get default number of decimal places to use when formatting a price number. |
| 85 |
* |
| 86 |
* @return int |
| 87 |
*/ |
| 88 |
public function get_decimal_places() { |
| 89 |
return absint( apply_filters( 'dfrapi_price_decimal_places', self::DEFAULT_DECIMAL_PLACES, $this ) ); |
| 90 |
} |
| 91 |
|
| 92 |
/** |
| 93 |
* Returns the format to display the price (either positive or negative). |
| 94 |
* |
| 95 |
* @return string |
| 96 |
*/ |
| 97 |
public function get_signed_format() { |
| 98 |
return $this->int < 0 ? $this->currency->get_neg_format() : $this->currency->get_format(); |
| 99 |
} |
| 100 |
|
| 101 |
/** |
| 102 |
* Returns the $int value formatted with the proper decimal point and thousands separator. |
| 103 |
* |
| 104 |
* Also handles trimming of trailing zeros if required. |
| 105 |
* |
| 106 |
* @return integer|float |
| 107 |
*/ |
| 108 |
public function get_formatted_number() { |
| 109 |
|
| 110 |
$formatted_number = $this->number_format(); |
| 111 |
|
| 112 |
if ( boolval( apply_filters( 'dfrapi_price_trim_trailing_double_zeros', self::TRIM_TRAILING_DOUBLE_ZEROS, $this ) ) ) { |
| 113 |
$formatted_number = self::trim_trailing_double_zeros_from_formatted_number( $formatted_number ); |
| 114 |
} |
| 115 |
|
| 116 |
return apply_filters( 'dfrapi_price_formatted_number', $formatted_number, $this ); |
| 117 |
} |
| 118 |
|
| 119 |
/** |
| 120 |
* Returns the whole/integral number (everything before decimal) of the formatted number. |
| 121 |
* |
| 122 |
* @return string |
| 123 |
*/ |
| 124 |
public function get_whole_part() { |
| 125 |
return dfrapi_str_before_last( $this->get_formatted_number(), $this->currency->get_decimal_point() ); |
| 126 |
} |
| 127 |
|
| 128 |
/** |
| 129 |
* Returns the fractional number (everything after decimal) of the formatted number. |
| 130 |
* |
| 131 |
* Returns an empty string if the formatted number does not contain a decimal point. |
| 132 |
* |
| 133 |
* @return string |
| 134 |
*/ |
| 135 |
public function get_fraction_part() { |
| 136 |
|
| 137 |
$formatted_number = $this->get_formatted_number(); |
| 138 |
$decimal_point = $this->currency->get_decimal_point(); |
| 139 |
|
| 140 |
if ( ! dfrapi_str_contains( $formatted_number, $decimal_point ) ) { |
| 141 |
return ''; |
| 142 |
} |
| 143 |
|
| 144 |
return dfrapi_str_after_last( $formatted_number, $decimal_point ); |
| 145 |
} |
| 146 |
|
| 147 |
/** |
| 148 |
* Returns a formatted version of the $this->int value. |
| 149 |
* |
| 150 |
* @return string |
| 151 |
*/ |
| 152 |
public function number_format() { |
| 153 |
return number_format( |
| 154 |
( absint( $this->int ) / 100 ), |
| 155 |
$this->get_decimal_places(), |
| 156 |
$this->currency->get_decimal_point(), |
| 157 |
$this->currency->get_thousands_sep() |
| 158 |
); |
| 159 |
} |
| 160 |
|
| 161 |
/** |
| 162 |
* Removes trailing ".00" and ",00" from a formatted number. |
| 163 |
* |
| 164 |
* @param string A formatted version of number. |
| 165 |
* |
| 166 |
* @return string A formatted version of number. |
| 167 |
*/ |
| 168 |
public static function trim_trailing_double_zeros_from_formatted_number( $number ) { |
| 169 |
|
| 170 |
$needles = apply_filters( 'dfrapi_price_double_zero_needles', [ '.00', ',00' ], $number ); |
| 171 |
|
| 172 |
foreach ( $needles as $needle ) { |
| 173 |
if ( dfrapi_ends_with( $number, $needle ) ) { |
| 174 |
return str_replace( $needle, '', $number ); |
| 175 |
} |
| 176 |
} |
| 177 |
|
| 178 |
return $number; |
| 179 |
} |
| 180 |
|
| 181 |
/** |
| 182 |
* Returns an array of translation pairs to use in the formatting of the price. |
| 183 |
* |
| 184 |
* {symbol} Currency Symbol (ie. $, €, £, etc...) |
| 185 |
* {price} Price (formatted with decimal & thousands separator) |
| 186 |
* {code} Currency Code (ie. USD, EUR, GBP, etc...) |
| 187 |
* {name} Currency Name (ie. US Dollar, Euro, Great Britain Pound Sterling, etc...) |
| 188 |
* {decimal} The character to use as the decimal point (separating the whole number and fraction number. |
| 189 |
* {whole} The price's formatted whole number including thousands separator (everything before the decimal point). |
| 190 |
* {fraction} The price's fraction (everything after the decimal point). |
| 191 |
* |
| 192 |
* @return array |
| 193 |
*/ |
| 194 |
private function get_translation_pairs() { |
| 195 |
return apply_filters( 'dfrapi_price_translation_pairs', |
| 196 |
[ |
| 197 |
'{code}' => $this->currency->get_currency_code(), |
| 198 |
'{symbol}' => $this->currency->get_currency_symbol(), |
| 199 |
'{price}' => $this->get_formatted_number(), |
| 200 |
'{name}' => $this->currency->get_currency_name(), |
| 201 |
'{decimal}' => $this->currency->get_decimal_point(), |
| 202 |
'{whole}' => $this->get_whole_part(), |
| 203 |
'{fraction}' => $this->get_fraction_part(), |
| 204 |
], |
| 205 |
$this |
| 206 |
); |
| 207 |
} |
| 208 |
} |
| 209 |
} |
| 210 |
|