@@ -19,223 +19,223 @@ |
||
| 19 | 19 | class Currency |
| 20 | 20 | { |
| 21 | 21 | |
| 22 | - /** |
|
| 23 | - * eg 'US' |
|
| 24 | - * |
|
| 25 | - * @var string $code |
|
| 26 | - */ |
|
| 27 | - private $code; |
|
| 28 | - |
|
| 29 | - /** |
|
| 30 | - * @var Label $label |
|
| 31 | - */ |
|
| 32 | - private $label; |
|
| 33 | - |
|
| 34 | - /** |
|
| 35 | - * currency sign |
|
| 36 | - * |
|
| 37 | - * @var string $sign |
|
| 38 | - * eg '$' |
|
| 39 | - */ |
|
| 40 | - private $sign; |
|
| 41 | - |
|
| 42 | - /** |
|
| 43 | - * Whether the currency sign should come before the number or not |
|
| 44 | - * |
|
| 45 | - * @var boolean $sign_b4 |
|
| 46 | - */ |
|
| 47 | - private $sign_b4; |
|
| 48 | - |
|
| 49 | - /** |
|
| 50 | - * How many digits should come after the decimal place |
|
| 51 | - * Although not theoretically true, it can effectively |
|
| 52 | - * be considered that all currencies are decimal based. |
|
| 53 | - * Therefore the number of decimal places can be used |
|
| 54 | - * to calculate number of subunits like so: |
|
| 55 | - * subunits = pow( 10, decimal places ) |
|
| 56 | - * |
|
| 57 | - * @var int $decimal_places |
|
| 58 | - */ |
|
| 59 | - private $decimal_places; |
|
| 60 | - |
|
| 61 | - /** |
|
| 62 | - * Symbol to use for decimal mark |
|
| 63 | - * |
|
| 64 | - * @var string $decimal_mark |
|
| 65 | - * eg '.' |
|
| 66 | - */ |
|
| 67 | - private $decimal_mark; |
|
| 68 | - |
|
| 69 | - /** |
|
| 70 | - * Symbol to use for thousands |
|
| 71 | - * |
|
| 72 | - * @var string $thousands |
|
| 73 | - * eg ',' |
|
| 74 | - */ |
|
| 75 | - private $thousands; |
|
| 76 | - |
|
| 77 | - /** |
|
| 78 | - * The number of fractional divisions of a currency's main unit |
|
| 79 | - * Can be used to determine the number of decimal places used. |
|
| 80 | - * Because |
|
| 81 | - * subunits = pow( 10, decimal places ) |
|
| 82 | - * then |
|
| 83 | - * decimal places = log( subunits ) |
|
| 84 | - * except that a result of 1 means there are zero decimal places |
|
| 85 | - * |
|
| 86 | - * @var int |
|
| 87 | - */ |
|
| 88 | - private $subunits; |
|
| 89 | - |
|
| 90 | - |
|
| 91 | - |
|
| 92 | - /** |
|
| 93 | - * Currency constructor. |
|
| 94 | - * |
|
| 95 | - * @param string $code |
|
| 96 | - * @param Label $label |
|
| 97 | - * @param string $sign |
|
| 98 | - * @param bool $sign_b4 |
|
| 99 | - * @param int $decimal_places the number of decimal places to use when DISPLAYING the currency |
|
| 100 | - * @param string $decimal_mark |
|
| 101 | - * @param string $thousands |
|
| 102 | - * @param int $subunits number of fractional divisions of a currency's main unit |
|
| 103 | - */ |
|
| 104 | - public function __construct( |
|
| 105 | - $code, |
|
| 106 | - Label $label, |
|
| 107 | - $sign, |
|
| 108 | - $sign_b4, |
|
| 109 | - $decimal_places, |
|
| 110 | - $decimal_mark, |
|
| 111 | - $thousands, |
|
| 112 | - $subunits |
|
| 113 | - ) { |
|
| 114 | - $this->code = $code; |
|
| 115 | - $this->label = $label; |
|
| 116 | - $this->sign = $sign; |
|
| 117 | - $this->sign_b4 = $sign_b4; |
|
| 118 | - $this->decimal_places = $decimal_places; |
|
| 119 | - $this->decimal_mark = $decimal_mark; |
|
| 120 | - $this->thousands = $thousands; |
|
| 121 | - $this->subunits = $subunits; |
|
| 122 | - } |
|
| 123 | - |
|
| 124 | - |
|
| 125 | - |
|
| 126 | - /** |
|
| 127 | - * returns true if this currency is the same as the supplied currency |
|
| 128 | - * |
|
| 129 | - * @param Currency $other |
|
| 130 | - * @return bool |
|
| 131 | - */ |
|
| 132 | - public function equals(Currency $other) |
|
| 133 | - { |
|
| 134 | - return $this->code() === $other->code(); |
|
| 135 | - } |
|
| 136 | - |
|
| 137 | - |
|
| 138 | - |
|
| 139 | - /** |
|
| 140 | - * @return string |
|
| 141 | - */ |
|
| 142 | - public function code() |
|
| 143 | - { |
|
| 144 | - return $this->code; |
|
| 145 | - } |
|
| 146 | - |
|
| 147 | - |
|
| 148 | - |
|
| 149 | - /** |
|
| 150 | - * @return string |
|
| 151 | - */ |
|
| 152 | - public function name() |
|
| 153 | - { |
|
| 154 | - return $this->label->singular(); |
|
| 155 | - } |
|
| 156 | - |
|
| 157 | - |
|
| 158 | - |
|
| 159 | - /** |
|
| 160 | - * @return string |
|
| 161 | - */ |
|
| 162 | - public function plural() |
|
| 163 | - { |
|
| 164 | - return $this->label->plural(); |
|
| 165 | - } |
|
| 166 | - |
|
| 167 | - |
|
| 168 | - |
|
| 169 | - /** |
|
| 170 | - * @return string |
|
| 171 | - */ |
|
| 172 | - public function sign() |
|
| 173 | - { |
|
| 174 | - return $this->sign; |
|
| 175 | - } |
|
| 176 | - |
|
| 177 | - |
|
| 178 | - |
|
| 179 | - /** |
|
| 180 | - * @return bool |
|
| 181 | - */ |
|
| 182 | - public function signB4() |
|
| 183 | - { |
|
| 184 | - return $this->sign_b4; |
|
| 185 | - } |
|
| 186 | - |
|
| 187 | - |
|
| 188 | - |
|
| 189 | - /** |
|
| 190 | - * @return int |
|
| 191 | - */ |
|
| 192 | - public function decimalPlaces() |
|
| 193 | - { |
|
| 194 | - return $this->decimal_places; |
|
| 195 | - } |
|
| 196 | - |
|
| 197 | - |
|
| 198 | - |
|
| 199 | - /** |
|
| 200 | - * @return string |
|
| 201 | - */ |
|
| 202 | - public function decimalMark() |
|
| 203 | - { |
|
| 204 | - return $this->decimal_mark; |
|
| 205 | - } |
|
| 22 | + /** |
|
| 23 | + * eg 'US' |
|
| 24 | + * |
|
| 25 | + * @var string $code |
|
| 26 | + */ |
|
| 27 | + private $code; |
|
| 28 | + |
|
| 29 | + /** |
|
| 30 | + * @var Label $label |
|
| 31 | + */ |
|
| 32 | + private $label; |
|
| 33 | + |
|
| 34 | + /** |
|
| 35 | + * currency sign |
|
| 36 | + * |
|
| 37 | + * @var string $sign |
|
| 38 | + * eg '$' |
|
| 39 | + */ |
|
| 40 | + private $sign; |
|
| 41 | + |
|
| 42 | + /** |
|
| 43 | + * Whether the currency sign should come before the number or not |
|
| 44 | + * |
|
| 45 | + * @var boolean $sign_b4 |
|
| 46 | + */ |
|
| 47 | + private $sign_b4; |
|
| 48 | + |
|
| 49 | + /** |
|
| 50 | + * How many digits should come after the decimal place |
|
| 51 | + * Although not theoretically true, it can effectively |
|
| 52 | + * be considered that all currencies are decimal based. |
|
| 53 | + * Therefore the number of decimal places can be used |
|
| 54 | + * to calculate number of subunits like so: |
|
| 55 | + * subunits = pow( 10, decimal places ) |
|
| 56 | + * |
|
| 57 | + * @var int $decimal_places |
|
| 58 | + */ |
|
| 59 | + private $decimal_places; |
|
| 60 | + |
|
| 61 | + /** |
|
| 62 | + * Symbol to use for decimal mark |
|
| 63 | + * |
|
| 64 | + * @var string $decimal_mark |
|
| 65 | + * eg '.' |
|
| 66 | + */ |
|
| 67 | + private $decimal_mark; |
|
| 68 | + |
|
| 69 | + /** |
|
| 70 | + * Symbol to use for thousands |
|
| 71 | + * |
|
| 72 | + * @var string $thousands |
|
| 73 | + * eg ',' |
|
| 74 | + */ |
|
| 75 | + private $thousands; |
|
| 76 | + |
|
| 77 | + /** |
|
| 78 | + * The number of fractional divisions of a currency's main unit |
|
| 79 | + * Can be used to determine the number of decimal places used. |
|
| 80 | + * Because |
|
| 81 | + * subunits = pow( 10, decimal places ) |
|
| 82 | + * then |
|
| 83 | + * decimal places = log( subunits ) |
|
| 84 | + * except that a result of 1 means there are zero decimal places |
|
| 85 | + * |
|
| 86 | + * @var int |
|
| 87 | + */ |
|
| 88 | + private $subunits; |
|
| 89 | + |
|
| 90 | + |
|
| 91 | + |
|
| 92 | + /** |
|
| 93 | + * Currency constructor. |
|
| 94 | + * |
|
| 95 | + * @param string $code |
|
| 96 | + * @param Label $label |
|
| 97 | + * @param string $sign |
|
| 98 | + * @param bool $sign_b4 |
|
| 99 | + * @param int $decimal_places the number of decimal places to use when DISPLAYING the currency |
|
| 100 | + * @param string $decimal_mark |
|
| 101 | + * @param string $thousands |
|
| 102 | + * @param int $subunits number of fractional divisions of a currency's main unit |
|
| 103 | + */ |
|
| 104 | + public function __construct( |
|
| 105 | + $code, |
|
| 106 | + Label $label, |
|
| 107 | + $sign, |
|
| 108 | + $sign_b4, |
|
| 109 | + $decimal_places, |
|
| 110 | + $decimal_mark, |
|
| 111 | + $thousands, |
|
| 112 | + $subunits |
|
| 113 | + ) { |
|
| 114 | + $this->code = $code; |
|
| 115 | + $this->label = $label; |
|
| 116 | + $this->sign = $sign; |
|
| 117 | + $this->sign_b4 = $sign_b4; |
|
| 118 | + $this->decimal_places = $decimal_places; |
|
| 119 | + $this->decimal_mark = $decimal_mark; |
|
| 120 | + $this->thousands = $thousands; |
|
| 121 | + $this->subunits = $subunits; |
|
| 122 | + } |
|
| 123 | + |
|
| 124 | + |
|
| 125 | + |
|
| 126 | + /** |
|
| 127 | + * returns true if this currency is the same as the supplied currency |
|
| 128 | + * |
|
| 129 | + * @param Currency $other |
|
| 130 | + * @return bool |
|
| 131 | + */ |
|
| 132 | + public function equals(Currency $other) |
|
| 133 | + { |
|
| 134 | + return $this->code() === $other->code(); |
|
| 135 | + } |
|
| 136 | + |
|
| 137 | + |
|
| 138 | + |
|
| 139 | + /** |
|
| 140 | + * @return string |
|
| 141 | + */ |
|
| 142 | + public function code() |
|
| 143 | + { |
|
| 144 | + return $this->code; |
|
| 145 | + } |
|
| 146 | + |
|
| 147 | + |
|
| 148 | + |
|
| 149 | + /** |
|
| 150 | + * @return string |
|
| 151 | + */ |
|
| 152 | + public function name() |
|
| 153 | + { |
|
| 154 | + return $this->label->singular(); |
|
| 155 | + } |
|
| 156 | + |
|
| 157 | + |
|
| 158 | + |
|
| 159 | + /** |
|
| 160 | + * @return string |
|
| 161 | + */ |
|
| 162 | + public function plural() |
|
| 163 | + { |
|
| 164 | + return $this->label->plural(); |
|
| 165 | + } |
|
| 166 | + |
|
| 167 | + |
|
| 168 | + |
|
| 169 | + /** |
|
| 170 | + * @return string |
|
| 171 | + */ |
|
| 172 | + public function sign() |
|
| 173 | + { |
|
| 174 | + return $this->sign; |
|
| 175 | + } |
|
| 176 | + |
|
| 177 | + |
|
| 178 | + |
|
| 179 | + /** |
|
| 180 | + * @return bool |
|
| 181 | + */ |
|
| 182 | + public function signB4() |
|
| 183 | + { |
|
| 184 | + return $this->sign_b4; |
|
| 185 | + } |
|
| 186 | + |
|
| 187 | + |
|
| 188 | + |
|
| 189 | + /** |
|
| 190 | + * @return int |
|
| 191 | + */ |
|
| 192 | + public function decimalPlaces() |
|
| 193 | + { |
|
| 194 | + return $this->decimal_places; |
|
| 195 | + } |
|
| 196 | + |
|
| 197 | + |
|
| 198 | + |
|
| 199 | + /** |
|
| 200 | + * @return string |
|
| 201 | + */ |
|
| 202 | + public function decimalMark() |
|
| 203 | + { |
|
| 204 | + return $this->decimal_mark; |
|
| 205 | + } |
|
| 206 | 206 | |
| 207 | 207 | |
| 208 | 208 | |
| 209 | - /** |
|
| 210 | - * @return string |
|
| 211 | - */ |
|
| 212 | - public function thousands() |
|
| 213 | - { |
|
| 214 | - return $this->thousands; |
|
| 215 | - } |
|
| 209 | + /** |
|
| 210 | + * @return string |
|
| 211 | + */ |
|
| 212 | + public function thousands() |
|
| 213 | + { |
|
| 214 | + return $this->thousands; |
|
| 215 | + } |
|
| 216 | 216 | |
| 217 | 217 | |
| 218 | 218 | |
| 219 | - /** |
|
| 220 | - * The difference between currency units and subunits |
|
| 221 | - * is 10-to-the-power-of-this. |
|
| 222 | - * DecimalMark is used for display, this is used for calculations |
|
| 223 | - * @return int |
|
| 224 | - */ |
|
| 225 | - public function subunits() |
|
| 226 | - { |
|
| 227 | - return $this->subunits; |
|
| 228 | - } |
|
| 219 | + /** |
|
| 220 | + * The difference between currency units and subunits |
|
| 221 | + * is 10-to-the-power-of-this. |
|
| 222 | + * DecimalMark is used for display, this is used for calculations |
|
| 223 | + * @return int |
|
| 224 | + */ |
|
| 225 | + public function subunits() |
|
| 226 | + { |
|
| 227 | + return $this->subunits; |
|
| 228 | + } |
|
| 229 | 229 | |
| 230 | 230 | |
| 231 | 231 | |
| 232 | - /** |
|
| 233 | - * @return string |
|
| 234 | - */ |
|
| 235 | - public function __toString() |
|
| 236 | - { |
|
| 237 | - return $this->code(); |
|
| 238 | - } |
|
| 232 | + /** |
|
| 233 | + * @return string |
|
| 234 | + */ |
|
| 235 | + public function __toString() |
|
| 236 | + { |
|
| 237 | + return $this->code(); |
|
| 238 | + } |
|
| 239 | 239 | |
| 240 | 240 | |
| 241 | 241 | |
@@ -22,344 +22,344 @@ |
||
| 22 | 22 | class Money |
| 23 | 23 | { |
| 24 | 24 | |
| 25 | - /** |
|
| 26 | - * number of decimal places to be added to currencies for internal computations, |
|
| 27 | - * but removed before any output or formatting is applied. |
|
| 28 | - * This allows us to avoid rounding errors during calculations. |
|
| 29 | - */ |
|
| 30 | - const EXTRA_PRECISION = 3; |
|
| 31 | - |
|
| 32 | - /** |
|
| 33 | - * @var int $amount |
|
| 34 | - */ |
|
| 35 | - private $amount; |
|
| 36 | - |
|
| 37 | - /** |
|
| 38 | - * @var Currency $currency |
|
| 39 | - */ |
|
| 40 | - private $currency; |
|
| 41 | - |
|
| 42 | - /** |
|
| 43 | - * @var Calculator $calculator |
|
| 44 | - */ |
|
| 45 | - protected $calculator; |
|
| 46 | - |
|
| 47 | - /** |
|
| 48 | - * @var MoneyFormatter[] $formatters |
|
| 49 | - */ |
|
| 50 | - protected $formatters; |
|
| 51 | - |
|
| 52 | - |
|
| 53 | - |
|
| 54 | - /** |
|
| 55 | - * Money constructor. |
|
| 56 | - * |
|
| 57 | - * @param float|int|string $amount money amount IN THE STANDARD UNIT FOR THE CURRENCY ie: dollars, Euros, etc |
|
| 58 | - * example: $12.5 USD would equate to a value amount of 12.50 |
|
| 59 | - * @param Currency $currency |
|
| 60 | - * @param Calculator $calculator |
|
| 61 | - * @param MoneyFormatter[] $formatters |
|
| 62 | - * @throws InvalidDataTypeException |
|
| 63 | - */ |
|
| 64 | - public function __construct($amount, Currency $currency, Calculator $calculator, array $formatters) |
|
| 65 | - { |
|
| 66 | - $this->currency = $currency; |
|
| 67 | - $this->amount = $this->parseAmount($amount); |
|
| 68 | - $this->calculator = $calculator; |
|
| 69 | - $this->formatters = $formatters; |
|
| 70 | - } |
|
| 71 | - |
|
| 72 | - |
|
| 73 | - |
|
| 74 | - /** |
|
| 75 | - * @return Calculator |
|
| 76 | - */ |
|
| 77 | - protected function calculator() |
|
| 78 | - { |
|
| 79 | - return $this->calculator; |
|
| 80 | - } |
|
| 81 | - |
|
| 82 | - |
|
| 83 | - |
|
| 84 | - /** |
|
| 85 | - * @return MoneyFormatter[] |
|
| 86 | - */ |
|
| 87 | - protected function formatters() |
|
| 88 | - { |
|
| 89 | - return $this->formatters; |
|
| 90 | - } |
|
| 91 | - |
|
| 92 | - |
|
| 93 | - |
|
| 94 | - /** |
|
| 95 | - * Convert's a standard unit amount into the subunits of the currency |
|
| 96 | - * |
|
| 97 | - * @param float|int|string $amount money amount IN THE STANDARD UNIT FOR THE CURRENCY ie: dollars, Euros, etc |
|
| 98 | - * example: $12.5 USD would equate to a value amount of 12.50 |
|
| 99 | - * @return integer in the currency's subunit |
|
| 100 | - * @throws InvalidDataTypeException |
|
| 101 | - */ |
|
| 102 | - private function parseAmount($amount) |
|
| 103 | - { |
|
| 104 | - if (! in_array(gettype($amount), array('integer', 'double', 'string'), true)) { |
|
| 105 | - throw new InvalidDataTypeException( |
|
| 106 | - '$amount', |
|
| 107 | - $amount, |
|
| 108 | - 'integer (or float or string)' |
|
| 109 | - ); |
|
| 110 | - } |
|
| 111 | - if ($this->currency->decimalMark() !== '.') { |
|
| 112 | - // remove thousands separator and replace decimal place with standard decimal. |
|
| 113 | - $amount = str_replace( |
|
| 114 | - array( |
|
| 115 | - $this->currency->thousands(), |
|
| 116 | - $this->currency->decimalMark(), |
|
| 117 | - ), |
|
| 118 | - array( |
|
| 119 | - '', |
|
| 120 | - '.', |
|
| 121 | - ), |
|
| 122 | - $amount |
|
| 123 | - ); |
|
| 124 | - } |
|
| 125 | - // remove any non numeric values but leave the decimal |
|
| 126 | - $amount = (float) preg_replace('/([^0-9\\.-])/', '', $amount); |
|
| 127 | - // convert the incoming decimal amount to the currencies subunits |
|
| 128 | - // ex: 12.5 for a currency with 100 subunits would become 1250 |
|
| 129 | - $amount *= $this->currency()->subunits(); |
|
| 130 | - // then shift the decimal position by the number of decimal places used internally |
|
| 131 | - // so if our extra internal precision was 3, it would become 1250000 |
|
| 132 | - $amount *= pow(10, $this->precision()); |
|
| 133 | - // then round up the remaining value if there is still a fractional amount left |
|
| 134 | - $amount = round($amount); |
|
| 135 | - return (int)$amount; |
|
| 136 | - } |
|
| 137 | - |
|
| 138 | - |
|
| 139 | - |
|
| 140 | - /** |
|
| 141 | - * adds or subtracts additional decimal places based on the value of the Money::EXTRA_PRECISION constant |
|
| 142 | - * |
|
| 143 | - * @param bool $adding_precision if true, will move the decimal to the right (increase amount) |
|
| 144 | - * if false, will move the decimal to the left (decrease amount) |
|
| 145 | - * @return integer |
|
| 146 | - */ |
|
| 147 | - private function precision($adding_precision = true) |
|
| 148 | - { |
|
| 149 | - $sign = $adding_precision ? 1 : -1; |
|
| 150 | - return Money::EXTRA_PRECISION * $sign; |
|
| 151 | - } |
|
| 152 | - |
|
| 153 | - |
|
| 154 | - |
|
| 155 | - /** |
|
| 156 | - * Returns the money amount as an unformatted string |
|
| 157 | - * IF YOU REQUIRE A FORMATTED STRING, THEN USE Money::format() |
|
| 158 | - * In the currency's standard units |
|
| 159 | - * |
|
| 160 | - * @return string |
|
| 161 | - */ |
|
| 162 | - public function amount() |
|
| 163 | - { |
|
| 164 | - // shift the decimal position BACK by the number of decimal places used internally |
|
| 165 | - // ex: if our extra internal precision was 3, then 1250000 would become 1250 |
|
| 166 | - $amount = $this->amount * pow(10, $this->precision(false)); |
|
| 167 | - // then adjust for the currencies subunits |
|
| 168 | - // ex: 1250 for a currency with 100 subunits would become 12.50 |
|
| 169 | - $amount /= $this->currency()->subunits(); |
|
| 170 | - // then shave off our extra internal precision using the number of decimal places for the currency |
|
| 171 | - $amount = round($amount, $this->currency->decimalPlaces()); |
|
| 172 | - return (string) $amount; |
|
| 173 | - } |
|
| 174 | - |
|
| 175 | - |
|
| 176 | - |
|
| 177 | - /** |
|
| 178 | - * Returns the money SUBUNITS amount as an INTEGER |
|
| 179 | - * |
|
| 180 | - * @return integer |
|
| 181 | - */ |
|
| 182 | - public function amountInSubunits() |
|
| 183 | - { |
|
| 184 | - // shift the decimal position BACK by the number of decimal places used internally |
|
| 185 | - // for extra internal precision, but NOT for the number of decimals used by the currency |
|
| 186 | - // ex: if our extra internal precision was 3, then 1250000 would become 1250 |
|
| 187 | - // and even if the currency used 100 subunits, we would return 1250 and NOT 12.50 |
|
| 188 | - $amount = (string) $this->amount * pow(10, $this->precision(false)); |
|
| 189 | - // then shave off anything after the decimal |
|
| 190 | - $amount = round($amount); |
|
| 191 | - return (int) $amount; |
|
| 192 | - } |
|
| 193 | - |
|
| 194 | - |
|
| 195 | - |
|
| 196 | - /** |
|
| 197 | - * applies formatting based on the specified formatting level |
|
| 198 | - * corresponding to one of the constants on MoneyFormatter |
|
| 199 | - * |
|
| 200 | - * @param int $formatting_level |
|
| 201 | - * @return string |
|
| 202 | - */ |
|
| 203 | - public function format($formatting_level = MoneyFormatter::ADD_THOUSANDS) |
|
| 204 | - { |
|
| 205 | - $formatted_amount = $this->amount(); |
|
| 206 | - $formatters = $this->formatters(); |
|
| 207 | - // if we are applying thousands formatting... |
|
| 208 | - if ($formatting_level >= MoneyFormatter::ADD_THOUSANDS) { |
|
| 209 | - // then let's remove decimal formatting since it's included in thousands formatting |
|
| 210 | - unset($formatters[ MoneyFormatter::DECIMAL_ONLY ]); |
|
| 211 | - } |
|
| 212 | - for ($x = 1; $x <= $formatting_level; $x++) { |
|
| 213 | - if (isset($formatters[ $x ]) && $formatters[ $x ] instanceof MoneyFormatter) { |
|
| 214 | - $formatted_amount = $formatters[ $x ]->format($formatted_amount, $this->currency); |
|
| 215 | - } |
|
| 216 | - } |
|
| 217 | - return (string) apply_filters( |
|
| 218 | - 'FHEE__EventEspresso_core_domain_values_currency_Money__format__formatted_amount', |
|
| 219 | - $formatted_amount, |
|
| 220 | - $this |
|
| 221 | - ); |
|
| 222 | - } |
|
| 223 | - |
|
| 224 | - |
|
| 225 | - |
|
| 226 | - /** |
|
| 227 | - * Returns the Currency object for this money |
|
| 228 | - * |
|
| 229 | - * @return Currency |
|
| 230 | - */ |
|
| 231 | - public function currency() |
|
| 232 | - { |
|
| 233 | - return $this->currency; |
|
| 234 | - } |
|
| 235 | - |
|
| 236 | - |
|
| 237 | - |
|
| 238 | - /** |
|
| 239 | - * adds the supplied Money amount to this Money amount |
|
| 240 | - * and returns a new Money object |
|
| 241 | - * |
|
| 242 | - * @param Money $other |
|
| 243 | - * @return Money |
|
| 244 | - * @throws InvalidArgumentException |
|
| 245 | - */ |
|
| 246 | - public function add(Money $other) |
|
| 247 | - { |
|
| 248 | - $this->verifySameCurrency($other->currency()); |
|
| 249 | - return new Money( |
|
| 250 | - $this->calculator()->add( |
|
| 251 | - $this->amount(), |
|
| 252 | - $other->amount() |
|
| 253 | - ), |
|
| 254 | - $this->currency(), |
|
| 255 | - $this->calculator(), |
|
| 256 | - $this->formatters() |
|
| 257 | - ); |
|
| 258 | - } |
|
| 259 | - |
|
| 260 | - |
|
| 261 | - |
|
| 262 | - /** |
|
| 263 | - * subtracts the supplied Money amount from this Money amount |
|
| 264 | - * and returns a new Money object |
|
| 265 | - * |
|
| 266 | - * @param Money $other |
|
| 267 | - * @return Money |
|
| 268 | - * @throws InvalidArgumentException |
|
| 269 | - */ |
|
| 270 | - public function subtract(Money $other) |
|
| 271 | - { |
|
| 272 | - $this->verifySameCurrency($other->currency()); |
|
| 273 | - return new Money( |
|
| 274 | - $this->calculator()->subtract( |
|
| 275 | - $this->amount(), |
|
| 276 | - $other->amount() |
|
| 277 | - ), |
|
| 278 | - $this->currency(), |
|
| 279 | - $this->calculator(), |
|
| 280 | - $this->formatters() |
|
| 281 | - ); |
|
| 282 | - } |
|
| 283 | - |
|
| 284 | - |
|
| 285 | - |
|
| 286 | - /** |
|
| 287 | - * multiplies this Money amount by the supplied $multiplier |
|
| 288 | - * and returns a new Money object |
|
| 289 | - * |
|
| 290 | - * @param float|int|string $multiplier |
|
| 291 | - * @param int $rounding_mode |
|
| 292 | - * @return Money |
|
| 293 | - * @throws InvalidDataTypeException |
|
| 294 | - */ |
|
| 295 | - public function multiply($multiplier, $rounding_mode = Calculator::ROUND_HALF_UP) |
|
| 296 | - { |
|
| 297 | - return new Money( |
|
| 298 | - $this->calculator()->multiply( |
|
| 299 | - $this->amount(), |
|
| 300 | - $multiplier, |
|
| 301 | - $this->precision(), |
|
| 302 | - $rounding_mode |
|
| 303 | - ), |
|
| 304 | - $this->currency(), |
|
| 305 | - $this->calculator(), |
|
| 306 | - $this->formatters() |
|
| 307 | - ); |
|
| 308 | - } |
|
| 309 | - |
|
| 310 | - |
|
| 311 | - |
|
| 312 | - /** |
|
| 313 | - * divides this Money amount by the supplied $divisor |
|
| 314 | - * and returns a new Money object |
|
| 315 | - * |
|
| 316 | - * @param float|int|string $divisor |
|
| 317 | - * @param int $rounding_mode |
|
| 318 | - * @return Money |
|
| 319 | - * @throws InvalidDataTypeException |
|
| 320 | - */ |
|
| 321 | - public function divide($divisor, $rounding_mode = Calculator::ROUND_HALF_UP) |
|
| 322 | - { |
|
| 323 | - return new Money( |
|
| 324 | - $this->calculator()->divide( |
|
| 325 | - $this->amount(), |
|
| 326 | - $divisor, |
|
| 327 | - $this->precision(), |
|
| 328 | - $rounding_mode |
|
| 329 | - ), |
|
| 330 | - $this->currency(), |
|
| 331 | - $this->calculator(), |
|
| 332 | - $this->formatters() |
|
| 333 | - ); |
|
| 334 | - } |
|
| 335 | - |
|
| 336 | - |
|
| 337 | - |
|
| 338 | - /** |
|
| 339 | - * @param Currency $other_currency |
|
| 340 | - * @throws InvalidArgumentException |
|
| 341 | - */ |
|
| 342 | - public function verifySameCurrency(Currency $other_currency) |
|
| 343 | - { |
|
| 344 | - if ($this->currency()->equals($other_currency) !== true) { |
|
| 345 | - throw new InvalidArgumentException( |
|
| 346 | - esc_html__( |
|
| 347 | - 'Currencies must be the same in order to add or subtract their values.', |
|
| 348 | - 'event_espresso' |
|
| 349 | - ) |
|
| 350 | - ); |
|
| 351 | - } |
|
| 352 | - } |
|
| 353 | - |
|
| 354 | - |
|
| 355 | - |
|
| 356 | - /** |
|
| 357 | - * @return string |
|
| 358 | - */ |
|
| 359 | - public function __toString() |
|
| 360 | - { |
|
| 361 | - return $this->format(MoneyFormatter::DECIMAL_ONLY); |
|
| 362 | - } |
|
| 25 | + /** |
|
| 26 | + * number of decimal places to be added to currencies for internal computations, |
|
| 27 | + * but removed before any output or formatting is applied. |
|
| 28 | + * This allows us to avoid rounding errors during calculations. |
|
| 29 | + */ |
|
| 30 | + const EXTRA_PRECISION = 3; |
|
| 31 | + |
|
| 32 | + /** |
|
| 33 | + * @var int $amount |
|
| 34 | + */ |
|
| 35 | + private $amount; |
|
| 36 | + |
|
| 37 | + /** |
|
| 38 | + * @var Currency $currency |
|
| 39 | + */ |
|
| 40 | + private $currency; |
|
| 41 | + |
|
| 42 | + /** |
|
| 43 | + * @var Calculator $calculator |
|
| 44 | + */ |
|
| 45 | + protected $calculator; |
|
| 46 | + |
|
| 47 | + /** |
|
| 48 | + * @var MoneyFormatter[] $formatters |
|
| 49 | + */ |
|
| 50 | + protected $formatters; |
|
| 51 | + |
|
| 52 | + |
|
| 53 | + |
|
| 54 | + /** |
|
| 55 | + * Money constructor. |
|
| 56 | + * |
|
| 57 | + * @param float|int|string $amount money amount IN THE STANDARD UNIT FOR THE CURRENCY ie: dollars, Euros, etc |
|
| 58 | + * example: $12.5 USD would equate to a value amount of 12.50 |
|
| 59 | + * @param Currency $currency |
|
| 60 | + * @param Calculator $calculator |
|
| 61 | + * @param MoneyFormatter[] $formatters |
|
| 62 | + * @throws InvalidDataTypeException |
|
| 63 | + */ |
|
| 64 | + public function __construct($amount, Currency $currency, Calculator $calculator, array $formatters) |
|
| 65 | + { |
|
| 66 | + $this->currency = $currency; |
|
| 67 | + $this->amount = $this->parseAmount($amount); |
|
| 68 | + $this->calculator = $calculator; |
|
| 69 | + $this->formatters = $formatters; |
|
| 70 | + } |
|
| 71 | + |
|
| 72 | + |
|
| 73 | + |
|
| 74 | + /** |
|
| 75 | + * @return Calculator |
|
| 76 | + */ |
|
| 77 | + protected function calculator() |
|
| 78 | + { |
|
| 79 | + return $this->calculator; |
|
| 80 | + } |
|
| 81 | + |
|
| 82 | + |
|
| 83 | + |
|
| 84 | + /** |
|
| 85 | + * @return MoneyFormatter[] |
|
| 86 | + */ |
|
| 87 | + protected function formatters() |
|
| 88 | + { |
|
| 89 | + return $this->formatters; |
|
| 90 | + } |
|
| 91 | + |
|
| 92 | + |
|
| 93 | + |
|
| 94 | + /** |
|
| 95 | + * Convert's a standard unit amount into the subunits of the currency |
|
| 96 | + * |
|
| 97 | + * @param float|int|string $amount money amount IN THE STANDARD UNIT FOR THE CURRENCY ie: dollars, Euros, etc |
|
| 98 | + * example: $12.5 USD would equate to a value amount of 12.50 |
|
| 99 | + * @return integer in the currency's subunit |
|
| 100 | + * @throws InvalidDataTypeException |
|
| 101 | + */ |
|
| 102 | + private function parseAmount($amount) |
|
| 103 | + { |
|
| 104 | + if (! in_array(gettype($amount), array('integer', 'double', 'string'), true)) { |
|
| 105 | + throw new InvalidDataTypeException( |
|
| 106 | + '$amount', |
|
| 107 | + $amount, |
|
| 108 | + 'integer (or float or string)' |
|
| 109 | + ); |
|
| 110 | + } |
|
| 111 | + if ($this->currency->decimalMark() !== '.') { |
|
| 112 | + // remove thousands separator and replace decimal place with standard decimal. |
|
| 113 | + $amount = str_replace( |
|
| 114 | + array( |
|
| 115 | + $this->currency->thousands(), |
|
| 116 | + $this->currency->decimalMark(), |
|
| 117 | + ), |
|
| 118 | + array( |
|
| 119 | + '', |
|
| 120 | + '.', |
|
| 121 | + ), |
|
| 122 | + $amount |
|
| 123 | + ); |
|
| 124 | + } |
|
| 125 | + // remove any non numeric values but leave the decimal |
|
| 126 | + $amount = (float) preg_replace('/([^0-9\\.-])/', '', $amount); |
|
| 127 | + // convert the incoming decimal amount to the currencies subunits |
|
| 128 | + // ex: 12.5 for a currency with 100 subunits would become 1250 |
|
| 129 | + $amount *= $this->currency()->subunits(); |
|
| 130 | + // then shift the decimal position by the number of decimal places used internally |
|
| 131 | + // so if our extra internal precision was 3, it would become 1250000 |
|
| 132 | + $amount *= pow(10, $this->precision()); |
|
| 133 | + // then round up the remaining value if there is still a fractional amount left |
|
| 134 | + $amount = round($amount); |
|
| 135 | + return (int)$amount; |
|
| 136 | + } |
|
| 137 | + |
|
| 138 | + |
|
| 139 | + |
|
| 140 | + /** |
|
| 141 | + * adds or subtracts additional decimal places based on the value of the Money::EXTRA_PRECISION constant |
|
| 142 | + * |
|
| 143 | + * @param bool $adding_precision if true, will move the decimal to the right (increase amount) |
|
| 144 | + * if false, will move the decimal to the left (decrease amount) |
|
| 145 | + * @return integer |
|
| 146 | + */ |
|
| 147 | + private function precision($adding_precision = true) |
|
| 148 | + { |
|
| 149 | + $sign = $adding_precision ? 1 : -1; |
|
| 150 | + return Money::EXTRA_PRECISION * $sign; |
|
| 151 | + } |
|
| 152 | + |
|
| 153 | + |
|
| 154 | + |
|
| 155 | + /** |
|
| 156 | + * Returns the money amount as an unformatted string |
|
| 157 | + * IF YOU REQUIRE A FORMATTED STRING, THEN USE Money::format() |
|
| 158 | + * In the currency's standard units |
|
| 159 | + * |
|
| 160 | + * @return string |
|
| 161 | + */ |
|
| 162 | + public function amount() |
|
| 163 | + { |
|
| 164 | + // shift the decimal position BACK by the number of decimal places used internally |
|
| 165 | + // ex: if our extra internal precision was 3, then 1250000 would become 1250 |
|
| 166 | + $amount = $this->amount * pow(10, $this->precision(false)); |
|
| 167 | + // then adjust for the currencies subunits |
|
| 168 | + // ex: 1250 for a currency with 100 subunits would become 12.50 |
|
| 169 | + $amount /= $this->currency()->subunits(); |
|
| 170 | + // then shave off our extra internal precision using the number of decimal places for the currency |
|
| 171 | + $amount = round($amount, $this->currency->decimalPlaces()); |
|
| 172 | + return (string) $amount; |
|
| 173 | + } |
|
| 174 | + |
|
| 175 | + |
|
| 176 | + |
|
| 177 | + /** |
|
| 178 | + * Returns the money SUBUNITS amount as an INTEGER |
|
| 179 | + * |
|
| 180 | + * @return integer |
|
| 181 | + */ |
|
| 182 | + public function amountInSubunits() |
|
| 183 | + { |
|
| 184 | + // shift the decimal position BACK by the number of decimal places used internally |
|
| 185 | + // for extra internal precision, but NOT for the number of decimals used by the currency |
|
| 186 | + // ex: if our extra internal precision was 3, then 1250000 would become 1250 |
|
| 187 | + // and even if the currency used 100 subunits, we would return 1250 and NOT 12.50 |
|
| 188 | + $amount = (string) $this->amount * pow(10, $this->precision(false)); |
|
| 189 | + // then shave off anything after the decimal |
|
| 190 | + $amount = round($amount); |
|
| 191 | + return (int) $amount; |
|
| 192 | + } |
|
| 193 | + |
|
| 194 | + |
|
| 195 | + |
|
| 196 | + /** |
|
| 197 | + * applies formatting based on the specified formatting level |
|
| 198 | + * corresponding to one of the constants on MoneyFormatter |
|
| 199 | + * |
|
| 200 | + * @param int $formatting_level |
|
| 201 | + * @return string |
|
| 202 | + */ |
|
| 203 | + public function format($formatting_level = MoneyFormatter::ADD_THOUSANDS) |
|
| 204 | + { |
|
| 205 | + $formatted_amount = $this->amount(); |
|
| 206 | + $formatters = $this->formatters(); |
|
| 207 | + // if we are applying thousands formatting... |
|
| 208 | + if ($formatting_level >= MoneyFormatter::ADD_THOUSANDS) { |
|
| 209 | + // then let's remove decimal formatting since it's included in thousands formatting |
|
| 210 | + unset($formatters[ MoneyFormatter::DECIMAL_ONLY ]); |
|
| 211 | + } |
|
| 212 | + for ($x = 1; $x <= $formatting_level; $x++) { |
|
| 213 | + if (isset($formatters[ $x ]) && $formatters[ $x ] instanceof MoneyFormatter) { |
|
| 214 | + $formatted_amount = $formatters[ $x ]->format($formatted_amount, $this->currency); |
|
| 215 | + } |
|
| 216 | + } |
|
| 217 | + return (string) apply_filters( |
|
| 218 | + 'FHEE__EventEspresso_core_domain_values_currency_Money__format__formatted_amount', |
|
| 219 | + $formatted_amount, |
|
| 220 | + $this |
|
| 221 | + ); |
|
| 222 | + } |
|
| 223 | + |
|
| 224 | + |
|
| 225 | + |
|
| 226 | + /** |
|
| 227 | + * Returns the Currency object for this money |
|
| 228 | + * |
|
| 229 | + * @return Currency |
|
| 230 | + */ |
|
| 231 | + public function currency() |
|
| 232 | + { |
|
| 233 | + return $this->currency; |
|
| 234 | + } |
|
| 235 | + |
|
| 236 | + |
|
| 237 | + |
|
| 238 | + /** |
|
| 239 | + * adds the supplied Money amount to this Money amount |
|
| 240 | + * and returns a new Money object |
|
| 241 | + * |
|
| 242 | + * @param Money $other |
|
| 243 | + * @return Money |
|
| 244 | + * @throws InvalidArgumentException |
|
| 245 | + */ |
|
| 246 | + public function add(Money $other) |
|
| 247 | + { |
|
| 248 | + $this->verifySameCurrency($other->currency()); |
|
| 249 | + return new Money( |
|
| 250 | + $this->calculator()->add( |
|
| 251 | + $this->amount(), |
|
| 252 | + $other->amount() |
|
| 253 | + ), |
|
| 254 | + $this->currency(), |
|
| 255 | + $this->calculator(), |
|
| 256 | + $this->formatters() |
|
| 257 | + ); |
|
| 258 | + } |
|
| 259 | + |
|
| 260 | + |
|
| 261 | + |
|
| 262 | + /** |
|
| 263 | + * subtracts the supplied Money amount from this Money amount |
|
| 264 | + * and returns a new Money object |
|
| 265 | + * |
|
| 266 | + * @param Money $other |
|
| 267 | + * @return Money |
|
| 268 | + * @throws InvalidArgumentException |
|
| 269 | + */ |
|
| 270 | + public function subtract(Money $other) |
|
| 271 | + { |
|
| 272 | + $this->verifySameCurrency($other->currency()); |
|
| 273 | + return new Money( |
|
| 274 | + $this->calculator()->subtract( |
|
| 275 | + $this->amount(), |
|
| 276 | + $other->amount() |
|
| 277 | + ), |
|
| 278 | + $this->currency(), |
|
| 279 | + $this->calculator(), |
|
| 280 | + $this->formatters() |
|
| 281 | + ); |
|
| 282 | + } |
|
| 283 | + |
|
| 284 | + |
|
| 285 | + |
|
| 286 | + /** |
|
| 287 | + * multiplies this Money amount by the supplied $multiplier |
|
| 288 | + * and returns a new Money object |
|
| 289 | + * |
|
| 290 | + * @param float|int|string $multiplier |
|
| 291 | + * @param int $rounding_mode |
|
| 292 | + * @return Money |
|
| 293 | + * @throws InvalidDataTypeException |
|
| 294 | + */ |
|
| 295 | + public function multiply($multiplier, $rounding_mode = Calculator::ROUND_HALF_UP) |
|
| 296 | + { |
|
| 297 | + return new Money( |
|
| 298 | + $this->calculator()->multiply( |
|
| 299 | + $this->amount(), |
|
| 300 | + $multiplier, |
|
| 301 | + $this->precision(), |
|
| 302 | + $rounding_mode |
|
| 303 | + ), |
|
| 304 | + $this->currency(), |
|
| 305 | + $this->calculator(), |
|
| 306 | + $this->formatters() |
|
| 307 | + ); |
|
| 308 | + } |
|
| 309 | + |
|
| 310 | + |
|
| 311 | + |
|
| 312 | + /** |
|
| 313 | + * divides this Money amount by the supplied $divisor |
|
| 314 | + * and returns a new Money object |
|
| 315 | + * |
|
| 316 | + * @param float|int|string $divisor |
|
| 317 | + * @param int $rounding_mode |
|
| 318 | + * @return Money |
|
| 319 | + * @throws InvalidDataTypeException |
|
| 320 | + */ |
|
| 321 | + public function divide($divisor, $rounding_mode = Calculator::ROUND_HALF_UP) |
|
| 322 | + { |
|
| 323 | + return new Money( |
|
| 324 | + $this->calculator()->divide( |
|
| 325 | + $this->amount(), |
|
| 326 | + $divisor, |
|
| 327 | + $this->precision(), |
|
| 328 | + $rounding_mode |
|
| 329 | + ), |
|
| 330 | + $this->currency(), |
|
| 331 | + $this->calculator(), |
|
| 332 | + $this->formatters() |
|
| 333 | + ); |
|
| 334 | + } |
|
| 335 | + |
|
| 336 | + |
|
| 337 | + |
|
| 338 | + /** |
|
| 339 | + * @param Currency $other_currency |
|
| 340 | + * @throws InvalidArgumentException |
|
| 341 | + */ |
|
| 342 | + public function verifySameCurrency(Currency $other_currency) |
|
| 343 | + { |
|
| 344 | + if ($this->currency()->equals($other_currency) !== true) { |
|
| 345 | + throw new InvalidArgumentException( |
|
| 346 | + esc_html__( |
|
| 347 | + 'Currencies must be the same in order to add or subtract their values.', |
|
| 348 | + 'event_espresso' |
|
| 349 | + ) |
|
| 350 | + ); |
|
| 351 | + } |
|
| 352 | + } |
|
| 353 | + |
|
| 354 | + |
|
| 355 | + |
|
| 356 | + /** |
|
| 357 | + * @return string |
|
| 358 | + */ |
|
| 359 | + public function __toString() |
|
| 360 | + { |
|
| 361 | + return $this->format(MoneyFormatter::DECIMAL_ONLY); |
|
| 362 | + } |
|
| 363 | 363 | } |
| 364 | 364 | // End of file Money.php |
| 365 | 365 | // Location: core/entities/money/Money.php |
@@ -27,155 +27,155 @@ |
||
| 27 | 27 | class CurrencyFactory |
| 28 | 28 | { |
| 29 | 29 | |
| 30 | - /** |
|
| 31 | - * @var array[] $country_currency_data |
|
| 32 | - */ |
|
| 33 | - private $country_currency_data; |
|
| 34 | - |
|
| 35 | - /** |
|
| 36 | - * @var array[] $country_currencies_by_iso_code |
|
| 37 | - */ |
|
| 38 | - private $country_currencies_by_iso_code; |
|
| 39 | - |
|
| 40 | - /** |
|
| 41 | - * @var array[] $country_currencies_by_currency |
|
| 42 | - */ |
|
| 43 | - private $country_currencies_by_currency; |
|
| 44 | - |
|
| 45 | - /** |
|
| 46 | - * @var string $site_country_iso |
|
| 47 | - */ |
|
| 48 | - private $site_country_iso; |
|
| 49 | - |
|
| 50 | - |
|
| 51 | - /** |
|
| 52 | - * CurrencyFactory constructor. |
|
| 53 | - * |
|
| 54 | - * @param EE_Organization_Config $organization_config |
|
| 55 | - */ |
|
| 56 | - public function __construct(EE_Organization_Config $organization_config) { |
|
| 57 | - $this->site_country_iso = $organization_config->CNT_ISO; |
|
| 58 | - } |
|
| 59 | - |
|
| 60 | - |
|
| 61 | - /** |
|
| 62 | - * @return array[] |
|
| 63 | - * @throws InvalidArgumentException |
|
| 64 | - * @throws EE_Error |
|
| 65 | - */ |
|
| 66 | - private function getCountryCurrencyData() |
|
| 67 | - { |
|
| 68 | - if ($this->country_currency_data === null) { |
|
| 69 | - $country_currency_data = json_decode( |
|
| 70 | - EEH_File::get_file_contents(__DIR__ . DS . 'country-currencies.json'), |
|
| 71 | - true |
|
| 72 | - ); |
|
| 73 | - $this->parseCountryCurrencyData($country_currency_data); |
|
| 74 | - } |
|
| 75 | - return $this->country_currency_data; |
|
| 76 | - } |
|
| 77 | - |
|
| 78 | - |
|
| 79 | - /** |
|
| 80 | - * @param array[] $country_currency_data |
|
| 81 | - * @throws InvalidArgumentException |
|
| 82 | - */ |
|
| 83 | - private function parseCountryCurrencyData($country_currency_data) |
|
| 84 | - { |
|
| 85 | - foreach ($country_currency_data as $country_currency) { |
|
| 86 | - $this->country_currencies_by_iso_code[ $country_currency['CountryISO'] ] = $country_currency; |
|
| 87 | - $this->country_currencies_by_currency[ $country_currency['CurrencyCode'] ] = $country_currency; |
|
| 88 | - } |
|
| 89 | - $this->country_currency_data = $country_currency_data; |
|
| 90 | - } |
|
| 91 | - |
|
| 92 | - |
|
| 93 | - /** |
|
| 94 | - * @param array $country_currency |
|
| 95 | - * @return Currency |
|
| 96 | - */ |
|
| 97 | - private function createCurrencyFromCountryCurrency(array $country_currency) |
|
| 98 | - { |
|
| 99 | - return new Currency( |
|
| 100 | - $country_currency['CurrencyCode'], |
|
| 101 | - new Label( |
|
| 102 | - $country_currency['CurrencyNameSingle'], |
|
| 103 | - $country_currency['CurrencyNamePlural'] |
|
| 104 | - ), |
|
| 105 | - $country_currency['CurrencySign'], |
|
| 106 | - $country_currency['CurrencySignB4'], |
|
| 107 | - $country_currency['CurrencyDecimalPlaces'], |
|
| 108 | - $country_currency['CurrencyDecimalMark'], |
|
| 109 | - $country_currency['CurrencyThousands'], |
|
| 110 | - $country_currency['CurrencySubunits'] |
|
| 111 | - ); |
|
| 112 | - } |
|
| 113 | - |
|
| 114 | - |
|
| 115 | - |
|
| 116 | - /** |
|
| 117 | - * returns a Currency object for the supplied country code |
|
| 118 | - * |
|
| 119 | - * @param string $CNT_ISO |
|
| 120 | - * @return Currency |
|
| 121 | - * @throws InvalidIdentifierException |
|
| 122 | - * @throws InvalidDataTypeException |
|
| 123 | - * @throws InvalidInterfaceException |
|
| 124 | - * @throws EE_Error |
|
| 125 | - * @throws InvalidArgumentException |
|
| 126 | - */ |
|
| 127 | - public function createFromCountryCode($CNT_ISO = null) |
|
| 128 | - { |
|
| 129 | - $this->getCountryCurrencyData(); |
|
| 130 | - $CNT_ISO = $CNT_ISO !== null ? $CNT_ISO : $this->site_country_iso; |
|
| 131 | - if(! isset($this->country_currencies_by_iso_code[ $CNT_ISO ])) { |
|
| 132 | - throw new InvalidArgumentException( |
|
| 133 | - sprintf( |
|
| 134 | - esc_html__( |
|
| 135 | - 'Valid country currency data could not be found for the "%1$s" country code;', |
|
| 136 | - 'event_espresso' |
|
| 137 | - ), |
|
| 138 | - $CNT_ISO |
|
| 139 | - ) |
|
| 140 | - ); |
|
| 141 | - } |
|
| 142 | - return $this->createCurrencyFromCountryCurrency( |
|
| 143 | - $this->country_currencies_by_iso_code[ $CNT_ISO ] |
|
| 144 | - ); |
|
| 145 | - } |
|
| 146 | - |
|
| 147 | - |
|
| 148 | - |
|
| 149 | - /** |
|
| 150 | - * returns a Currency object for the supplied currency code |
|
| 151 | - * PLZ NOTE: variations may exist between how different countries display the same currency, |
|
| 152 | - * so it may be necessary to use CreateCurrency::fromCountryCode() to achieve the desired results |
|
| 153 | - * |
|
| 154 | - * @param string $code |
|
| 155 | - * @return Currency |
|
| 156 | - * @throws InvalidIdentifierException |
|
| 157 | - * @throws InvalidInterfaceException |
|
| 158 | - * @throws InvalidDataTypeException |
|
| 159 | - * @throws InvalidArgumentException |
|
| 160 | - * @throws EE_Error |
|
| 161 | - */ |
|
| 162 | - public function createFromCode($code) |
|
| 163 | - { |
|
| 164 | - $this->getCountryCurrencyData(); |
|
| 165 | - if (! isset($this->country_currencies_by_currency[ $code ])) { |
|
| 166 | - throw new InvalidArgumentException( |
|
| 167 | - sprintf( |
|
| 168 | - esc_html__( |
|
| 169 | - 'A valid currency could not be found for the "%1$s" currency code;', |
|
| 170 | - 'event_espresso' |
|
| 171 | - ), |
|
| 172 | - $code |
|
| 173 | - ) |
|
| 174 | - ); |
|
| 175 | - } |
|
| 176 | - return $this->createCurrencyFromCountryCurrency( |
|
| 177 | - $this->country_currencies_by_currency[ $code ] |
|
| 178 | - ); |
|
| 179 | - } |
|
| 30 | + /** |
|
| 31 | + * @var array[] $country_currency_data |
|
| 32 | + */ |
|
| 33 | + private $country_currency_data; |
|
| 34 | + |
|
| 35 | + /** |
|
| 36 | + * @var array[] $country_currencies_by_iso_code |
|
| 37 | + */ |
|
| 38 | + private $country_currencies_by_iso_code; |
|
| 39 | + |
|
| 40 | + /** |
|
| 41 | + * @var array[] $country_currencies_by_currency |
|
| 42 | + */ |
|
| 43 | + private $country_currencies_by_currency; |
|
| 44 | + |
|
| 45 | + /** |
|
| 46 | + * @var string $site_country_iso |
|
| 47 | + */ |
|
| 48 | + private $site_country_iso; |
|
| 49 | + |
|
| 50 | + |
|
| 51 | + /** |
|
| 52 | + * CurrencyFactory constructor. |
|
| 53 | + * |
|
| 54 | + * @param EE_Organization_Config $organization_config |
|
| 55 | + */ |
|
| 56 | + public function __construct(EE_Organization_Config $organization_config) { |
|
| 57 | + $this->site_country_iso = $organization_config->CNT_ISO; |
|
| 58 | + } |
|
| 59 | + |
|
| 60 | + |
|
| 61 | + /** |
|
| 62 | + * @return array[] |
|
| 63 | + * @throws InvalidArgumentException |
|
| 64 | + * @throws EE_Error |
|
| 65 | + */ |
|
| 66 | + private function getCountryCurrencyData() |
|
| 67 | + { |
|
| 68 | + if ($this->country_currency_data === null) { |
|
| 69 | + $country_currency_data = json_decode( |
|
| 70 | + EEH_File::get_file_contents(__DIR__ . DS . 'country-currencies.json'), |
|
| 71 | + true |
|
| 72 | + ); |
|
| 73 | + $this->parseCountryCurrencyData($country_currency_data); |
|
| 74 | + } |
|
| 75 | + return $this->country_currency_data; |
|
| 76 | + } |
|
| 77 | + |
|
| 78 | + |
|
| 79 | + /** |
|
| 80 | + * @param array[] $country_currency_data |
|
| 81 | + * @throws InvalidArgumentException |
|
| 82 | + */ |
|
| 83 | + private function parseCountryCurrencyData($country_currency_data) |
|
| 84 | + { |
|
| 85 | + foreach ($country_currency_data as $country_currency) { |
|
| 86 | + $this->country_currencies_by_iso_code[ $country_currency['CountryISO'] ] = $country_currency; |
|
| 87 | + $this->country_currencies_by_currency[ $country_currency['CurrencyCode'] ] = $country_currency; |
|
| 88 | + } |
|
| 89 | + $this->country_currency_data = $country_currency_data; |
|
| 90 | + } |
|
| 91 | + |
|
| 92 | + |
|
| 93 | + /** |
|
| 94 | + * @param array $country_currency |
|
| 95 | + * @return Currency |
|
| 96 | + */ |
|
| 97 | + private function createCurrencyFromCountryCurrency(array $country_currency) |
|
| 98 | + { |
|
| 99 | + return new Currency( |
|
| 100 | + $country_currency['CurrencyCode'], |
|
| 101 | + new Label( |
|
| 102 | + $country_currency['CurrencyNameSingle'], |
|
| 103 | + $country_currency['CurrencyNamePlural'] |
|
| 104 | + ), |
|
| 105 | + $country_currency['CurrencySign'], |
|
| 106 | + $country_currency['CurrencySignB4'], |
|
| 107 | + $country_currency['CurrencyDecimalPlaces'], |
|
| 108 | + $country_currency['CurrencyDecimalMark'], |
|
| 109 | + $country_currency['CurrencyThousands'], |
|
| 110 | + $country_currency['CurrencySubunits'] |
|
| 111 | + ); |
|
| 112 | + } |
|
| 113 | + |
|
| 114 | + |
|
| 115 | + |
|
| 116 | + /** |
|
| 117 | + * returns a Currency object for the supplied country code |
|
| 118 | + * |
|
| 119 | + * @param string $CNT_ISO |
|
| 120 | + * @return Currency |
|
| 121 | + * @throws InvalidIdentifierException |
|
| 122 | + * @throws InvalidDataTypeException |
|
| 123 | + * @throws InvalidInterfaceException |
|
| 124 | + * @throws EE_Error |
|
| 125 | + * @throws InvalidArgumentException |
|
| 126 | + */ |
|
| 127 | + public function createFromCountryCode($CNT_ISO = null) |
|
| 128 | + { |
|
| 129 | + $this->getCountryCurrencyData(); |
|
| 130 | + $CNT_ISO = $CNT_ISO !== null ? $CNT_ISO : $this->site_country_iso; |
|
| 131 | + if(! isset($this->country_currencies_by_iso_code[ $CNT_ISO ])) { |
|
| 132 | + throw new InvalidArgumentException( |
|
| 133 | + sprintf( |
|
| 134 | + esc_html__( |
|
| 135 | + 'Valid country currency data could not be found for the "%1$s" country code;', |
|
| 136 | + 'event_espresso' |
|
| 137 | + ), |
|
| 138 | + $CNT_ISO |
|
| 139 | + ) |
|
| 140 | + ); |
|
| 141 | + } |
|
| 142 | + return $this->createCurrencyFromCountryCurrency( |
|
| 143 | + $this->country_currencies_by_iso_code[ $CNT_ISO ] |
|
| 144 | + ); |
|
| 145 | + } |
|
| 146 | + |
|
| 147 | + |
|
| 148 | + |
|
| 149 | + /** |
|
| 150 | + * returns a Currency object for the supplied currency code |
|
| 151 | + * PLZ NOTE: variations may exist between how different countries display the same currency, |
|
| 152 | + * so it may be necessary to use CreateCurrency::fromCountryCode() to achieve the desired results |
|
| 153 | + * |
|
| 154 | + * @param string $code |
|
| 155 | + * @return Currency |
|
| 156 | + * @throws InvalidIdentifierException |
|
| 157 | + * @throws InvalidInterfaceException |
|
| 158 | + * @throws InvalidDataTypeException |
|
| 159 | + * @throws InvalidArgumentException |
|
| 160 | + * @throws EE_Error |
|
| 161 | + */ |
|
| 162 | + public function createFromCode($code) |
|
| 163 | + { |
|
| 164 | + $this->getCountryCurrencyData(); |
|
| 165 | + if (! isset($this->country_currencies_by_currency[ $code ])) { |
|
| 166 | + throw new InvalidArgumentException( |
|
| 167 | + sprintf( |
|
| 168 | + esc_html__( |
|
| 169 | + 'A valid currency could not be found for the "%1$s" currency code;', |
|
| 170 | + 'event_espresso' |
|
| 171 | + ), |
|
| 172 | + $code |
|
| 173 | + ) |
|
| 174 | + ); |
|
| 175 | + } |
|
| 176 | + return $this->createCurrencyFromCountryCurrency( |
|
| 177 | + $this->country_currencies_by_currency[ $code ] |
|
| 178 | + ); |
|
| 179 | + } |
|
| 180 | 180 | } |
| 181 | 181 | // Location: CreateCurrency.php |
@@ -67,7 +67,7 @@ discard block |
||
| 67 | 67 | { |
| 68 | 68 | if ($this->country_currency_data === null) { |
| 69 | 69 | $country_currency_data = json_decode( |
| 70 | - EEH_File::get_file_contents(__DIR__ . DS . 'country-currencies.json'), |
|
| 70 | + EEH_File::get_file_contents(__DIR__.DS.'country-currencies.json'), |
|
| 71 | 71 | true |
| 72 | 72 | ); |
| 73 | 73 | $this->parseCountryCurrencyData($country_currency_data); |
@@ -83,8 +83,8 @@ discard block |
||
| 83 | 83 | private function parseCountryCurrencyData($country_currency_data) |
| 84 | 84 | { |
| 85 | 85 | foreach ($country_currency_data as $country_currency) { |
| 86 | - $this->country_currencies_by_iso_code[ $country_currency['CountryISO'] ] = $country_currency; |
|
| 87 | - $this->country_currencies_by_currency[ $country_currency['CurrencyCode'] ] = $country_currency; |
|
| 86 | + $this->country_currencies_by_iso_code[$country_currency['CountryISO']] = $country_currency; |
|
| 87 | + $this->country_currencies_by_currency[$country_currency['CurrencyCode']] = $country_currency; |
|
| 88 | 88 | } |
| 89 | 89 | $this->country_currency_data = $country_currency_data; |
| 90 | 90 | } |
@@ -128,7 +128,7 @@ discard block |
||
| 128 | 128 | { |
| 129 | 129 | $this->getCountryCurrencyData(); |
| 130 | 130 | $CNT_ISO = $CNT_ISO !== null ? $CNT_ISO : $this->site_country_iso; |
| 131 | - if(! isset($this->country_currencies_by_iso_code[ $CNT_ISO ])) { |
|
| 131 | + if ( ! isset($this->country_currencies_by_iso_code[$CNT_ISO])) { |
|
| 132 | 132 | throw new InvalidArgumentException( |
| 133 | 133 | sprintf( |
| 134 | 134 | esc_html__( |
@@ -140,7 +140,7 @@ discard block |
||
| 140 | 140 | ); |
| 141 | 141 | } |
| 142 | 142 | return $this->createCurrencyFromCountryCurrency( |
| 143 | - $this->country_currencies_by_iso_code[ $CNT_ISO ] |
|
| 143 | + $this->country_currencies_by_iso_code[$CNT_ISO] |
|
| 144 | 144 | ); |
| 145 | 145 | } |
| 146 | 146 | |
@@ -162,7 +162,7 @@ discard block |
||
| 162 | 162 | public function createFromCode($code) |
| 163 | 163 | { |
| 164 | 164 | $this->getCountryCurrencyData(); |
| 165 | - if (! isset($this->country_currencies_by_currency[ $code ])) { |
|
| 165 | + if ( ! isset($this->country_currencies_by_currency[$code])) { |
|
| 166 | 166 | throw new InvalidArgumentException( |
| 167 | 167 | sprintf( |
| 168 | 168 | esc_html__( |
@@ -174,7 +174,7 @@ discard block |
||
| 174 | 174 | ); |
| 175 | 175 | } |
| 176 | 176 | return $this->createCurrencyFromCountryCurrency( |
| 177 | - $this->country_currencies_by_currency[ $code ] |
|
| 177 | + $this->country_currencies_by_currency[$code] |
|
| 178 | 178 | ); |
| 179 | 179 | } |
| 180 | 180 | } |
@@ -38,217 +38,217 @@ |
||
| 38 | 38 | * @since 4.0 |
| 39 | 39 | */ |
| 40 | 40 | if (function_exists('espresso_version')) { |
| 41 | - if (! function_exists('espresso_duplicate_plugin_error')) { |
|
| 42 | - /** |
|
| 43 | - * espresso_duplicate_plugin_error |
|
| 44 | - * displays if more than one version of EE is activated at the same time |
|
| 45 | - */ |
|
| 46 | - function espresso_duplicate_plugin_error() |
|
| 47 | - { |
|
| 48 | - ?> |
|
| 41 | + if (! function_exists('espresso_duplicate_plugin_error')) { |
|
| 42 | + /** |
|
| 43 | + * espresso_duplicate_plugin_error |
|
| 44 | + * displays if more than one version of EE is activated at the same time |
|
| 45 | + */ |
|
| 46 | + function espresso_duplicate_plugin_error() |
|
| 47 | + { |
|
| 48 | + ?> |
|
| 49 | 49 | <div class="error"> |
| 50 | 50 | <p> |
| 51 | 51 | <?php |
| 52 | - echo esc_html__( |
|
| 53 | - 'Can not run multiple versions of Event Espresso! One version has been automatically deactivated. Please verify that you have the correct version you want still active.', |
|
| 54 | - 'event_espresso' |
|
| 55 | - ); ?> |
|
| 52 | + echo esc_html__( |
|
| 53 | + 'Can not run multiple versions of Event Espresso! One version has been automatically deactivated. Please verify that you have the correct version you want still active.', |
|
| 54 | + 'event_espresso' |
|
| 55 | + ); ?> |
|
| 56 | 56 | </p> |
| 57 | 57 | </div> |
| 58 | 58 | <?php |
| 59 | - espresso_deactivate_plugin(plugin_basename(__FILE__)); |
|
| 60 | - } |
|
| 61 | - } |
|
| 62 | - add_action('admin_notices', 'espresso_duplicate_plugin_error', 1); |
|
| 59 | + espresso_deactivate_plugin(plugin_basename(__FILE__)); |
|
| 60 | + } |
|
| 61 | + } |
|
| 62 | + add_action('admin_notices', 'espresso_duplicate_plugin_error', 1); |
|
| 63 | 63 | |
| 64 | 64 | } else { |
| 65 | - define('EE_MIN_PHP_VER_REQUIRED', '5.3.9'); |
|
| 66 | - if (! version_compare(PHP_VERSION, EE_MIN_PHP_VER_REQUIRED, '>=')) { |
|
| 67 | - /** |
|
| 68 | - * espresso_minimum_php_version_error |
|
| 69 | - * |
|
| 70 | - * @return void |
|
| 71 | - */ |
|
| 72 | - function espresso_minimum_php_version_error() |
|
| 73 | - { |
|
| 74 | - ?> |
|
| 65 | + define('EE_MIN_PHP_VER_REQUIRED', '5.3.9'); |
|
| 66 | + if (! version_compare(PHP_VERSION, EE_MIN_PHP_VER_REQUIRED, '>=')) { |
|
| 67 | + /** |
|
| 68 | + * espresso_minimum_php_version_error |
|
| 69 | + * |
|
| 70 | + * @return void |
|
| 71 | + */ |
|
| 72 | + function espresso_minimum_php_version_error() |
|
| 73 | + { |
|
| 74 | + ?> |
|
| 75 | 75 | <div class="error"> |
| 76 | 76 | <p> |
| 77 | 77 | <?php |
| 78 | - printf( |
|
| 79 | - esc_html__( |
|
| 80 | - 'We\'re sorry, but Event Espresso requires PHP version %1$s or greater in order to operate. You are currently running version %2$s.%3$sIn order to update your version of PHP, you will need to contact your current hosting provider.%3$sFor information on stable PHP versions, please go to %4$s.', |
|
| 81 | - 'event_espresso' |
|
| 82 | - ), |
|
| 83 | - EE_MIN_PHP_VER_REQUIRED, |
|
| 84 | - PHP_VERSION, |
|
| 85 | - '<br/>', |
|
| 86 | - '<a href="http://php.net/downloads.php">http://php.net/downloads.php</a>' |
|
| 87 | - ); |
|
| 88 | - ?> |
|
| 78 | + printf( |
|
| 79 | + esc_html__( |
|
| 80 | + 'We\'re sorry, but Event Espresso requires PHP version %1$s or greater in order to operate. You are currently running version %2$s.%3$sIn order to update your version of PHP, you will need to contact your current hosting provider.%3$sFor information on stable PHP versions, please go to %4$s.', |
|
| 81 | + 'event_espresso' |
|
| 82 | + ), |
|
| 83 | + EE_MIN_PHP_VER_REQUIRED, |
|
| 84 | + PHP_VERSION, |
|
| 85 | + '<br/>', |
|
| 86 | + '<a href="http://php.net/downloads.php">http://php.net/downloads.php</a>' |
|
| 87 | + ); |
|
| 88 | + ?> |
|
| 89 | 89 | </p> |
| 90 | 90 | </div> |
| 91 | 91 | <?php |
| 92 | - espresso_deactivate_plugin(plugin_basename(__FILE__)); |
|
| 93 | - } |
|
| 92 | + espresso_deactivate_plugin(plugin_basename(__FILE__)); |
|
| 93 | + } |
|
| 94 | 94 | |
| 95 | - add_action('admin_notices', 'espresso_minimum_php_version_error', 1); |
|
| 96 | - } else { |
|
| 97 | - define('EVENT_ESPRESSO_MAIN_FILE', __FILE__); |
|
| 98 | - /** |
|
| 99 | - * espresso_version |
|
| 100 | - * Returns the plugin version |
|
| 101 | - * |
|
| 102 | - * @return string |
|
| 103 | - */ |
|
| 104 | - function espresso_version() |
|
| 105 | - { |
|
| 106 | - return apply_filters('FHEE__espresso__espresso_version', '4.9.53.rc.005'); |
|
| 107 | - } |
|
| 95 | + add_action('admin_notices', 'espresso_minimum_php_version_error', 1); |
|
| 96 | + } else { |
|
| 97 | + define('EVENT_ESPRESSO_MAIN_FILE', __FILE__); |
|
| 98 | + /** |
|
| 99 | + * espresso_version |
|
| 100 | + * Returns the plugin version |
|
| 101 | + * |
|
| 102 | + * @return string |
|
| 103 | + */ |
|
| 104 | + function espresso_version() |
|
| 105 | + { |
|
| 106 | + return apply_filters('FHEE__espresso__espresso_version', '4.9.53.rc.005'); |
|
| 107 | + } |
|
| 108 | 108 | |
| 109 | - /** |
|
| 110 | - * espresso_plugin_activation |
|
| 111 | - * adds a wp-option to indicate that EE has been activated via the WP admin plugins page |
|
| 112 | - */ |
|
| 113 | - function espresso_plugin_activation() |
|
| 114 | - { |
|
| 115 | - update_option('ee_espresso_activation', true); |
|
| 116 | - } |
|
| 109 | + /** |
|
| 110 | + * espresso_plugin_activation |
|
| 111 | + * adds a wp-option to indicate that EE has been activated via the WP admin plugins page |
|
| 112 | + */ |
|
| 113 | + function espresso_plugin_activation() |
|
| 114 | + { |
|
| 115 | + update_option('ee_espresso_activation', true); |
|
| 116 | + } |
|
| 117 | 117 | |
| 118 | - register_activation_hook(EVENT_ESPRESSO_MAIN_FILE, 'espresso_plugin_activation'); |
|
| 119 | - /** |
|
| 120 | - * espresso_load_error_handling |
|
| 121 | - * this function loads EE's class for handling exceptions and errors |
|
| 122 | - */ |
|
| 123 | - function espresso_load_error_handling() |
|
| 124 | - { |
|
| 125 | - static $error_handling_loaded = false; |
|
| 126 | - if ($error_handling_loaded) { |
|
| 127 | - return; |
|
| 128 | - } |
|
| 129 | - // load debugging tools |
|
| 130 | - if (WP_DEBUG === true && is_readable(EE_HELPERS . 'EEH_Debug_Tools.helper.php')) { |
|
| 131 | - require_once EE_HELPERS . 'EEH_Debug_Tools.helper.php'; |
|
| 132 | - \EEH_Debug_Tools::instance(); |
|
| 133 | - } |
|
| 134 | - // load error handling |
|
| 135 | - if (is_readable(EE_CORE . 'EE_Error.core.php')) { |
|
| 136 | - require_once EE_CORE . 'EE_Error.core.php'; |
|
| 137 | - } else { |
|
| 138 | - wp_die(esc_html__('The EE_Error core class could not be loaded.', 'event_espresso')); |
|
| 139 | - } |
|
| 140 | - $error_handling_loaded = true; |
|
| 141 | - } |
|
| 118 | + register_activation_hook(EVENT_ESPRESSO_MAIN_FILE, 'espresso_plugin_activation'); |
|
| 119 | + /** |
|
| 120 | + * espresso_load_error_handling |
|
| 121 | + * this function loads EE's class for handling exceptions and errors |
|
| 122 | + */ |
|
| 123 | + function espresso_load_error_handling() |
|
| 124 | + { |
|
| 125 | + static $error_handling_loaded = false; |
|
| 126 | + if ($error_handling_loaded) { |
|
| 127 | + return; |
|
| 128 | + } |
|
| 129 | + // load debugging tools |
|
| 130 | + if (WP_DEBUG === true && is_readable(EE_HELPERS . 'EEH_Debug_Tools.helper.php')) { |
|
| 131 | + require_once EE_HELPERS . 'EEH_Debug_Tools.helper.php'; |
|
| 132 | + \EEH_Debug_Tools::instance(); |
|
| 133 | + } |
|
| 134 | + // load error handling |
|
| 135 | + if (is_readable(EE_CORE . 'EE_Error.core.php')) { |
|
| 136 | + require_once EE_CORE . 'EE_Error.core.php'; |
|
| 137 | + } else { |
|
| 138 | + wp_die(esc_html__('The EE_Error core class could not be loaded.', 'event_espresso')); |
|
| 139 | + } |
|
| 140 | + $error_handling_loaded = true; |
|
| 141 | + } |
|
| 142 | 142 | |
| 143 | - /** |
|
| 144 | - * espresso_load_required |
|
| 145 | - * given a class name and path, this function will load that file or throw an exception |
|
| 146 | - * |
|
| 147 | - * @param string $classname |
|
| 148 | - * @param string $full_path_to_file |
|
| 149 | - * @throws EE_Error |
|
| 150 | - */ |
|
| 151 | - function espresso_load_required($classname, $full_path_to_file) |
|
| 152 | - { |
|
| 153 | - if (is_readable($full_path_to_file)) { |
|
| 154 | - require_once $full_path_to_file; |
|
| 155 | - } else { |
|
| 156 | - throw new \EE_Error ( |
|
| 157 | - sprintf( |
|
| 158 | - esc_html__( |
|
| 159 | - 'The %s class file could not be located or is not readable due to file permissions.', |
|
| 160 | - 'event_espresso' |
|
| 161 | - ), |
|
| 162 | - $classname |
|
| 163 | - ) |
|
| 164 | - ); |
|
| 165 | - } |
|
| 166 | - } |
|
| 143 | + /** |
|
| 144 | + * espresso_load_required |
|
| 145 | + * given a class name and path, this function will load that file or throw an exception |
|
| 146 | + * |
|
| 147 | + * @param string $classname |
|
| 148 | + * @param string $full_path_to_file |
|
| 149 | + * @throws EE_Error |
|
| 150 | + */ |
|
| 151 | + function espresso_load_required($classname, $full_path_to_file) |
|
| 152 | + { |
|
| 153 | + if (is_readable($full_path_to_file)) { |
|
| 154 | + require_once $full_path_to_file; |
|
| 155 | + } else { |
|
| 156 | + throw new \EE_Error ( |
|
| 157 | + sprintf( |
|
| 158 | + esc_html__( |
|
| 159 | + 'The %s class file could not be located or is not readable due to file permissions.', |
|
| 160 | + 'event_espresso' |
|
| 161 | + ), |
|
| 162 | + $classname |
|
| 163 | + ) |
|
| 164 | + ); |
|
| 165 | + } |
|
| 166 | + } |
|
| 167 | 167 | |
| 168 | - /** |
|
| 169 | - * @since 4.9.27 |
|
| 170 | - * @throws \EE_Error |
|
| 171 | - * @throws \EventEspresso\core\exceptions\InvalidInterfaceException |
|
| 172 | - * @throws \EventEspresso\core\exceptions\InvalidEntityException |
|
| 173 | - * @throws \EventEspresso\core\exceptions\InvalidIdentifierException |
|
| 174 | - * @throws \EventEspresso\core\exceptions\InvalidClassException |
|
| 175 | - * @throws \EventEspresso\core\exceptions\InvalidDataTypeException |
|
| 176 | - * @throws \EventEspresso\core\services\container\exceptions\ServiceExistsException |
|
| 177 | - * @throws \EventEspresso\core\services\container\exceptions\ServiceNotFoundException |
|
| 178 | - * @throws \OutOfBoundsException |
|
| 179 | - */ |
|
| 180 | - function bootstrap_espresso() |
|
| 181 | - { |
|
| 182 | - require_once __DIR__ . '/core/espresso_definitions.php'; |
|
| 183 | - try { |
|
| 184 | - espresso_load_error_handling(); |
|
| 185 | - espresso_load_required( |
|
| 186 | - 'EEH_Base', |
|
| 187 | - EE_CORE . 'helpers' . DS . 'EEH_Base.helper.php' |
|
| 188 | - ); |
|
| 189 | - espresso_load_required( |
|
| 190 | - 'EEH_File', |
|
| 191 | - EE_CORE . 'interfaces' . DS . 'EEHI_File.interface.php' |
|
| 192 | - ); |
|
| 193 | - espresso_load_required( |
|
| 194 | - 'EEH_File', |
|
| 195 | - EE_CORE . 'helpers' . DS . 'EEH_File.helper.php' |
|
| 196 | - ); |
|
| 197 | - espresso_load_required( |
|
| 198 | - 'EEH_Array', |
|
| 199 | - EE_CORE . 'helpers' . DS . 'EEH_Array.helper.php' |
|
| 200 | - ); |
|
| 201 | - // instantiate and configure PSR4 autoloader |
|
| 202 | - espresso_load_required( |
|
| 203 | - 'Psr4Autoloader', |
|
| 204 | - EE_CORE . 'Psr4Autoloader.php' |
|
| 205 | - ); |
|
| 206 | - espresso_load_required( |
|
| 207 | - 'EE_Psr4AutoloaderInit', |
|
| 208 | - EE_CORE . 'EE_Psr4AutoloaderInit.core.php' |
|
| 209 | - ); |
|
| 210 | - $AutoloaderInit = new EE_Psr4AutoloaderInit(); |
|
| 211 | - $AutoloaderInit->initializeAutoloader(); |
|
| 212 | - espresso_load_required( |
|
| 213 | - 'EE_Request', |
|
| 214 | - EE_CORE . 'request_stack' . DS . 'EE_Request.core.php' |
|
| 215 | - ); |
|
| 216 | - espresso_load_required( |
|
| 217 | - 'EE_Response', |
|
| 218 | - EE_CORE . 'request_stack' . DS . 'EE_Response.core.php' |
|
| 219 | - ); |
|
| 220 | - espresso_load_required( |
|
| 221 | - 'EE_Bootstrap', |
|
| 222 | - EE_CORE . 'EE_Bootstrap.core.php' |
|
| 223 | - ); |
|
| 224 | - // bootstrap EE and the request stack |
|
| 225 | - new EE_Bootstrap( |
|
| 226 | - new EE_Request($_GET, $_POST, $_COOKIE), |
|
| 227 | - new EE_Response() |
|
| 228 | - ); |
|
| 229 | - } catch (Exception $e) { |
|
| 230 | - require_once EE_CORE . 'exceptions' . DS . 'ExceptionStackTraceDisplay.php'; |
|
| 231 | - new EventEspresso\core\exceptions\ExceptionStackTraceDisplay($e); |
|
| 232 | - } |
|
| 233 | - } |
|
| 234 | - bootstrap_espresso(); |
|
| 235 | - } |
|
| 168 | + /** |
|
| 169 | + * @since 4.9.27 |
|
| 170 | + * @throws \EE_Error |
|
| 171 | + * @throws \EventEspresso\core\exceptions\InvalidInterfaceException |
|
| 172 | + * @throws \EventEspresso\core\exceptions\InvalidEntityException |
|
| 173 | + * @throws \EventEspresso\core\exceptions\InvalidIdentifierException |
|
| 174 | + * @throws \EventEspresso\core\exceptions\InvalidClassException |
|
| 175 | + * @throws \EventEspresso\core\exceptions\InvalidDataTypeException |
|
| 176 | + * @throws \EventEspresso\core\services\container\exceptions\ServiceExistsException |
|
| 177 | + * @throws \EventEspresso\core\services\container\exceptions\ServiceNotFoundException |
|
| 178 | + * @throws \OutOfBoundsException |
|
| 179 | + */ |
|
| 180 | + function bootstrap_espresso() |
|
| 181 | + { |
|
| 182 | + require_once __DIR__ . '/core/espresso_definitions.php'; |
|
| 183 | + try { |
|
| 184 | + espresso_load_error_handling(); |
|
| 185 | + espresso_load_required( |
|
| 186 | + 'EEH_Base', |
|
| 187 | + EE_CORE . 'helpers' . DS . 'EEH_Base.helper.php' |
|
| 188 | + ); |
|
| 189 | + espresso_load_required( |
|
| 190 | + 'EEH_File', |
|
| 191 | + EE_CORE . 'interfaces' . DS . 'EEHI_File.interface.php' |
|
| 192 | + ); |
|
| 193 | + espresso_load_required( |
|
| 194 | + 'EEH_File', |
|
| 195 | + EE_CORE . 'helpers' . DS . 'EEH_File.helper.php' |
|
| 196 | + ); |
|
| 197 | + espresso_load_required( |
|
| 198 | + 'EEH_Array', |
|
| 199 | + EE_CORE . 'helpers' . DS . 'EEH_Array.helper.php' |
|
| 200 | + ); |
|
| 201 | + // instantiate and configure PSR4 autoloader |
|
| 202 | + espresso_load_required( |
|
| 203 | + 'Psr4Autoloader', |
|
| 204 | + EE_CORE . 'Psr4Autoloader.php' |
|
| 205 | + ); |
|
| 206 | + espresso_load_required( |
|
| 207 | + 'EE_Psr4AutoloaderInit', |
|
| 208 | + EE_CORE . 'EE_Psr4AutoloaderInit.core.php' |
|
| 209 | + ); |
|
| 210 | + $AutoloaderInit = new EE_Psr4AutoloaderInit(); |
|
| 211 | + $AutoloaderInit->initializeAutoloader(); |
|
| 212 | + espresso_load_required( |
|
| 213 | + 'EE_Request', |
|
| 214 | + EE_CORE . 'request_stack' . DS . 'EE_Request.core.php' |
|
| 215 | + ); |
|
| 216 | + espresso_load_required( |
|
| 217 | + 'EE_Response', |
|
| 218 | + EE_CORE . 'request_stack' . DS . 'EE_Response.core.php' |
|
| 219 | + ); |
|
| 220 | + espresso_load_required( |
|
| 221 | + 'EE_Bootstrap', |
|
| 222 | + EE_CORE . 'EE_Bootstrap.core.php' |
|
| 223 | + ); |
|
| 224 | + // bootstrap EE and the request stack |
|
| 225 | + new EE_Bootstrap( |
|
| 226 | + new EE_Request($_GET, $_POST, $_COOKIE), |
|
| 227 | + new EE_Response() |
|
| 228 | + ); |
|
| 229 | + } catch (Exception $e) { |
|
| 230 | + require_once EE_CORE . 'exceptions' . DS . 'ExceptionStackTraceDisplay.php'; |
|
| 231 | + new EventEspresso\core\exceptions\ExceptionStackTraceDisplay($e); |
|
| 232 | + } |
|
| 233 | + } |
|
| 234 | + bootstrap_espresso(); |
|
| 235 | + } |
|
| 236 | 236 | } |
| 237 | 237 | if (! function_exists('espresso_deactivate_plugin')) { |
| 238 | - /** |
|
| 239 | - * deactivate_plugin |
|
| 240 | - * usage: espresso_deactivate_plugin( plugin_basename( __FILE__ )); |
|
| 241 | - * |
|
| 242 | - * @access public |
|
| 243 | - * @param string $plugin_basename - the results of plugin_basename( __FILE__ ) for the plugin's main file |
|
| 244 | - * @return void |
|
| 245 | - */ |
|
| 246 | - function espresso_deactivate_plugin($plugin_basename = '') |
|
| 247 | - { |
|
| 248 | - if (! function_exists('deactivate_plugins')) { |
|
| 249 | - require_once ABSPATH . 'wp-admin/includes/plugin.php'; |
|
| 250 | - } |
|
| 251 | - unset($_GET['activate'], $_REQUEST['activate']); |
|
| 252 | - deactivate_plugins($plugin_basename); |
|
| 253 | - } |
|
| 238 | + /** |
|
| 239 | + * deactivate_plugin |
|
| 240 | + * usage: espresso_deactivate_plugin( plugin_basename( __FILE__ )); |
|
| 241 | + * |
|
| 242 | + * @access public |
|
| 243 | + * @param string $plugin_basename - the results of plugin_basename( __FILE__ ) for the plugin's main file |
|
| 244 | + * @return void |
|
| 245 | + */ |
|
| 246 | + function espresso_deactivate_plugin($plugin_basename = '') |
|
| 247 | + { |
|
| 248 | + if (! function_exists('deactivate_plugins')) { |
|
| 249 | + require_once ABSPATH . 'wp-admin/includes/plugin.php'; |
|
| 250 | + } |
|
| 251 | + unset($_GET['activate'], $_REQUEST['activate']); |
|
| 252 | + deactivate_plugins($plugin_basename); |
|
| 253 | + } |
|
| 254 | 254 | } |