@@ -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.50.rc.014'); |
|
| 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.50.rc.014'); |
|
| 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 | } |
@@ -161,7 +161,7 @@ discard block |
||
| 161 | 161 | * Returns the money amount as an unformatted string |
| 162 | 162 | * IF YOU REQUIRE A FORMATTED STRING, THEN USE Money::format() |
| 163 | 163 | * |
| 164 | - * @return string |
|
| 164 | + * @return double |
|
| 165 | 165 | */ |
| 166 | 166 | public function amount() |
| 167 | 167 | { |
@@ -178,7 +178,7 @@ discard block |
||
| 178 | 178 | /** |
| 179 | 179 | * Returns the money SUBUNITS amount as an INTEGER |
| 180 | 180 | * |
| 181 | - * @return integer |
|
| 181 | + * @return double |
|
| 182 | 182 | */ |
| 183 | 183 | public function amountInSubunits() |
| 184 | 184 | { |
@@ -26,363 +26,363 @@ |
||
| 26 | 26 | class Money |
| 27 | 27 | { |
| 28 | 28 | |
| 29 | - /** |
|
| 30 | - * number of decimal places to be added to currencies for internal computations, |
|
| 31 | - * but removed before any output or formatting is applied. |
|
| 32 | - * This allows us to avoid rounding errors during calculations. |
|
| 33 | - */ |
|
| 34 | - const EXTRA_PRECISION = 3; |
|
| 35 | - |
|
| 36 | - /** |
|
| 37 | - * @var int $amount |
|
| 38 | - */ |
|
| 39 | - private $amount; |
|
| 40 | - |
|
| 41 | - /** |
|
| 42 | - * @var Currency $currency |
|
| 43 | - */ |
|
| 44 | - private $currency; |
|
| 45 | - |
|
| 46 | - /** |
|
| 47 | - * @var Calculator $calculator |
|
| 48 | - */ |
|
| 49 | - protected $calculator; |
|
| 50 | - |
|
| 51 | - /** |
|
| 52 | - * @var MoneyFormatter[] $formatters |
|
| 53 | - */ |
|
| 54 | - protected $formatters; |
|
| 55 | - |
|
| 56 | - |
|
| 57 | - |
|
| 58 | - /** |
|
| 59 | - * Money constructor. |
|
| 60 | - * |
|
| 61 | - * @param float|int|string $amount money amount IN THE STANDARD UNIT FOR THE CURRENCY ie: dollars, Euros, etc |
|
| 62 | - * example: $12.5 USD would equate to a value amount of 12.50 |
|
| 63 | - * @param Currency $currency |
|
| 64 | - * @param Calculator $calculator |
|
| 65 | - * @param MoneyFormatter[] $formatters |
|
| 66 | - * @throws InvalidDataTypeException |
|
| 67 | - */ |
|
| 68 | - public function __construct($amount, Currency $currency, Calculator $calculator, array $formatters) |
|
| 69 | - { |
|
| 70 | - $this->currency = $currency; |
|
| 71 | - $this->amount = (string) $this->parseAmount($amount); |
|
| 72 | - $this->calculator = $calculator; |
|
| 73 | - $this->formatters = $formatters; |
|
| 74 | - } |
|
| 75 | - |
|
| 76 | - |
|
| 77 | - |
|
| 78 | - /** |
|
| 79 | - * @return Calculator |
|
| 80 | - */ |
|
| 81 | - protected function calculator() |
|
| 82 | - { |
|
| 83 | - return $this->calculator; |
|
| 84 | - } |
|
| 85 | - |
|
| 86 | - |
|
| 87 | - |
|
| 88 | - /** |
|
| 89 | - * @return MoneyFormatter[] |
|
| 90 | - */ |
|
| 91 | - protected function formatters() |
|
| 92 | - { |
|
| 93 | - return $this->formatters; |
|
| 94 | - } |
|
| 95 | - |
|
| 96 | - |
|
| 97 | - |
|
| 98 | - /** |
|
| 99 | - * @param float|int|string $amount money amount IN THE STANDARD UNIT FOR THE CURRENCY ie: dollars, Euros, etc |
|
| 100 | - * example: $12.5 USD would equate to a value amount of 12.50 |
|
| 101 | - * @return float|int|number|string |
|
| 102 | - * @throws InvalidDataTypeException |
|
| 103 | - */ |
|
| 104 | - private function parseAmount($amount) |
|
| 105 | - { |
|
| 106 | - $type = gettype($amount); |
|
| 107 | - switch ($type) { |
|
| 108 | - case 'integer' : |
|
| 109 | - case 'double' : |
|
| 110 | - case 'string' : |
|
| 111 | - break; |
|
| 112 | - default : |
|
| 113 | - throw new InvalidDataTypeException( |
|
| 114 | - '$amount', |
|
| 115 | - $amount, |
|
| 116 | - 'integer (or float or string)' |
|
| 117 | - ); |
|
| 118 | - } |
|
| 119 | - if ($this->currency->decimalMark() !== '.') { |
|
| 120 | - // remove thousands separator and replace decimal place with standard decimal. |
|
| 121 | - $amount = str_replace( |
|
| 122 | - array( |
|
| 123 | - $this->currency->thousands(), |
|
| 124 | - $this->currency->decimalMark(), |
|
| 125 | - ), |
|
| 126 | - array( |
|
| 127 | - '', |
|
| 128 | - '.', |
|
| 129 | - ), |
|
| 130 | - $amount |
|
| 131 | - ); |
|
| 132 | - } |
|
| 133 | - // remove any non numeric values but leave the decimal |
|
| 134 | - $amount = (float) preg_replace('/([^0-9\\.])/', '', $amount); |
|
| 135 | - // shift the decimal position by the number of decimal places used internally |
|
| 136 | - // ex: 12.5 for a currency using 2 decimal places, would become 1250 |
|
| 137 | - // then if our extra internal precision was 3, it would become 1250000 |
|
| 138 | - $amount *= pow(10, $this->precision()); |
|
| 139 | - // then round up the remaining value if there is still a fractional amount left |
|
| 140 | - $amount = round($amount); |
|
| 141 | - return $amount; |
|
| 142 | - } |
|
| 143 | - |
|
| 144 | - |
|
| 145 | - |
|
| 146 | - /** |
|
| 147 | - * adds or subtracts additional decimal places based on the value of the Money::EXTRA_PRECISION constant |
|
| 148 | - * |
|
| 149 | - * @param bool $positive |
|
| 150 | - * @return int |
|
| 151 | - */ |
|
| 152 | - private function precision($positive = true) |
|
| 153 | - { |
|
| 154 | - $sign = $positive ? 1 : -1; |
|
| 155 | - return ((int) $this->currency->decimalPlaces() + Money::EXTRA_PRECISION) * $sign; |
|
| 156 | - } |
|
| 157 | - |
|
| 158 | - |
|
| 159 | - |
|
| 160 | - /** |
|
| 161 | - * Returns the money amount as an unformatted string |
|
| 162 | - * IF YOU REQUIRE A FORMATTED STRING, THEN USE Money::format() |
|
| 163 | - * |
|
| 164 | - * @return string |
|
| 165 | - */ |
|
| 166 | - public function amount() |
|
| 167 | - { |
|
| 168 | - // shift the decimal position BACK by the number of decimal places used internally |
|
| 169 | - // ex: 1250 for a currency using 2 decimal places, would become 12.50 |
|
| 170 | - $amount = (string) $this->amount * pow(10, $this->precision(false)); |
|
| 171 | - // then shave off our extra internal precision using the number of decimal places for the currency |
|
| 172 | - $amount = round($amount, $this->currency->decimalPlaces()); |
|
| 173 | - return $amount; |
|
| 174 | - } |
|
| 175 | - |
|
| 176 | - |
|
| 177 | - |
|
| 178 | - /** |
|
| 179 | - * Returns the money SUBUNITS amount as an INTEGER |
|
| 180 | - * |
|
| 181 | - * @return integer |
|
| 182 | - */ |
|
| 183 | - public function amountInSubunits() |
|
| 184 | - { |
|
| 185 | - // shift the decimal position BACK |
|
| 186 | - // by the number of decimal places used internally by the extra internal precision |
|
| 187 | - // ex: if our extra internal precision was 3, |
|
| 188 | - // then 1250000 would become 1250 |
|
| 189 | - $amount = (string) $this->amount * pow(10, Money::EXTRA_PRECISION * -1); |
|
| 190 | - // then shave off anything after the decimal |
|
| 191 | - $amount = round($amount); |
|
| 192 | - return $amount; |
|
| 193 | - } |
|
| 194 | - |
|
| 195 | - |
|
| 196 | - |
|
| 197 | - /** |
|
| 198 | - * applies formatting based on the specified formatting level |
|
| 199 | - * corresponding to one of the constants on \EventEspresso\core\services\currency\MoneyFormatter |
|
| 200 | - * |
|
| 201 | - * @param int $formatting_level |
|
| 202 | - * @return string |
|
| 203 | - */ |
|
| 204 | - public function format($formatting_level = MoneyFormatter::ADD_THOUSANDS) |
|
| 205 | - { |
|
| 206 | - $formatted_amount = $this->amount(); |
|
| 207 | - $formatters = $this->formatters(); |
|
| 208 | - // if we are applying thousands formatting... |
|
| 209 | - if ($formatting_level >= MoneyFormatter::ADD_THOUSANDS) { |
|
| 210 | - // then let's remove decimal formatting since it's included in thousands formatting |
|
| 211 | - unset($formatters[ MoneyFormatter::DECIMAL_ONLY ]); |
|
| 212 | - } |
|
| 213 | - for ($x = 1; $x <= $formatting_level; $x++) { |
|
| 214 | - if (isset($formatters[ $x ]) && $formatters[ $x ] instanceof MoneyFormatter) { |
|
| 215 | - $formatted_amount = $formatters[ $x ]->format($formatted_amount, $this->currency); |
|
| 216 | - } |
|
| 217 | - } |
|
| 218 | - return $formatted_amount; |
|
| 219 | - } |
|
| 220 | - |
|
| 221 | - |
|
| 222 | - |
|
| 223 | - /** |
|
| 224 | - * Returns the Currency object for this money |
|
| 225 | - * |
|
| 226 | - * @return Currency |
|
| 227 | - */ |
|
| 228 | - public function currency() |
|
| 229 | - { |
|
| 230 | - return $this->currency; |
|
| 231 | - } |
|
| 232 | - |
|
| 233 | - |
|
| 234 | - |
|
| 235 | - /** |
|
| 236 | - * adds the supplied Money amount to this Money amount |
|
| 237 | - * and returns a new Money object |
|
| 238 | - * |
|
| 239 | - * @param Money $other |
|
| 240 | - * @return Money |
|
| 241 | - * @throws InvalidArgumentException |
|
| 242 | - */ |
|
| 243 | - public function add(Money $other) |
|
| 244 | - { |
|
| 245 | - $this->verifySameCurrency($other->currency()); |
|
| 246 | - return new Money( |
|
| 247 | - $this->calculator()->add( |
|
| 248 | - $this->amount(), |
|
| 249 | - $other->amount() |
|
| 250 | - ), |
|
| 251 | - $this->currency(), |
|
| 252 | - $this->calculator(), |
|
| 253 | - $this->formatters() |
|
| 254 | - ); |
|
| 255 | - } |
|
| 256 | - |
|
| 257 | - |
|
| 258 | - |
|
| 259 | - /** |
|
| 260 | - * subtracts the supplied Money amount from this Money amount |
|
| 261 | - * and returns a new Money object |
|
| 262 | - * |
|
| 263 | - * @param Money $other |
|
| 264 | - * @return Money |
|
| 265 | - * @throws InvalidArgumentException |
|
| 266 | - */ |
|
| 267 | - public function subtract(Money $other) |
|
| 268 | - { |
|
| 269 | - $this->verifySameCurrency($other->currency()); |
|
| 270 | - return new Money( |
|
| 271 | - $this->calculator()->subtract( |
|
| 272 | - $this->amount(), |
|
| 273 | - $other->amount() |
|
| 274 | - ), |
|
| 275 | - $this->currency(), |
|
| 276 | - $this->calculator(), |
|
| 277 | - $this->formatters() |
|
| 278 | - ); |
|
| 279 | - } |
|
| 280 | - |
|
| 281 | - |
|
| 282 | - |
|
| 283 | - /** |
|
| 284 | - * multiplies this Money amount by the supplied $multiplier |
|
| 285 | - * and returns a new Money object |
|
| 286 | - * |
|
| 287 | - * @param float|int|string $multiplier |
|
| 288 | - * @param int $rounding_mode |
|
| 289 | - * @return Money |
|
| 290 | - * @throws InvalidDataTypeException |
|
| 291 | - */ |
|
| 292 | - public function multiply($multiplier, $rounding_mode = Calculator::ROUND_HALF_UP) |
|
| 293 | - { |
|
| 294 | - return new Money( |
|
| 295 | - $this->calculator()->multiply( |
|
| 296 | - $this->amount(), |
|
| 297 | - $multiplier, |
|
| 298 | - $this->precision(), |
|
| 299 | - $rounding_mode |
|
| 300 | - ), |
|
| 301 | - $this->currency(), |
|
| 302 | - $this->calculator(), |
|
| 303 | - $this->formatters() |
|
| 304 | - ); |
|
| 305 | - } |
|
| 306 | - |
|
| 307 | - |
|
| 308 | - |
|
| 309 | - /** |
|
| 310 | - * divides this Money amount by the supplied $divisor |
|
| 311 | - * and returns a new Money object |
|
| 312 | - * |
|
| 313 | - * @param float|int|string $divisor |
|
| 314 | - * @param int $rounding_mode |
|
| 315 | - * @return Money |
|
| 316 | - * @throws InvalidDataTypeException |
|
| 317 | - */ |
|
| 318 | - public function divide($divisor, $rounding_mode = Calculator::ROUND_HALF_UP) |
|
| 319 | - { |
|
| 320 | - return new Money( |
|
| 321 | - $this->calculator()->divide( |
|
| 322 | - $this->amount(), |
|
| 323 | - $divisor, |
|
| 324 | - $this->precision(), |
|
| 325 | - $rounding_mode |
|
| 326 | - ), |
|
| 327 | - $this->currency(), |
|
| 328 | - $this->calculator(), |
|
| 329 | - $this->formatters() |
|
| 330 | - ); |
|
| 331 | - } |
|
| 332 | - |
|
| 333 | - |
|
| 334 | - |
|
| 335 | - /** |
|
| 336 | - * @param Currency $other_currency |
|
| 337 | - * @throws InvalidArgumentException |
|
| 338 | - */ |
|
| 339 | - public function verifySameCurrency(Currency $other_currency) |
|
| 340 | - { |
|
| 341 | - if ($this->currency()->equals($other_currency) !== true) { |
|
| 342 | - throw new InvalidArgumentException( |
|
| 343 | - esc_html__( |
|
| 344 | - 'Currencies must be the same in order to add or subtract their values.', |
|
| 345 | - 'event_espresso' |
|
| 346 | - ) |
|
| 347 | - ); |
|
| 348 | - } |
|
| 349 | - } |
|
| 350 | - |
|
| 351 | - |
|
| 352 | - |
|
| 353 | - /** |
|
| 354 | - * @return string |
|
| 355 | - */ |
|
| 356 | - public function __toString() |
|
| 357 | - { |
|
| 358 | - return $this->format(MoneyFormatter::DECIMAL_ONLY); |
|
| 359 | - } |
|
| 360 | - |
|
| 361 | - |
|
| 362 | - |
|
| 363 | - /** |
|
| 364 | - * factory method that returns a Money object for the currency specified as if it were a class method |
|
| 365 | - * example: Money::USD(12.5); |
|
| 366 | - * money amount IN THE STANDARD UNIT FOR THE CURRENCY ie: dollars, Euros, etc |
|
| 367 | - * example: $12.5 USD would equate to a value amount of 12.50 |
|
| 368 | - * |
|
| 369 | - * @param string $currency_code |
|
| 370 | - * @param array $arguments |
|
| 371 | - * @return Money |
|
| 372 | - * @throws InvalidArgumentException |
|
| 373 | - * @throws InvalidDataTypeException |
|
| 374 | - * @throws EE_Error |
|
| 375 | - * @throws InvalidInterfaceException |
|
| 376 | - */ |
|
| 377 | - public static function __callStatic($currency_code, $arguments) |
|
| 378 | - { |
|
| 379 | - return new Money( |
|
| 380 | - $arguments[0], |
|
| 381 | - CreateCurrency::fromCode($currency_code), |
|
| 382 | - CreateMoney::calculator(), |
|
| 383 | - CreateMoney::formatters() |
|
| 384 | - ); |
|
| 385 | - } |
|
| 29 | + /** |
|
| 30 | + * number of decimal places to be added to currencies for internal computations, |
|
| 31 | + * but removed before any output or formatting is applied. |
|
| 32 | + * This allows us to avoid rounding errors during calculations. |
|
| 33 | + */ |
|
| 34 | + const EXTRA_PRECISION = 3; |
|
| 35 | + |
|
| 36 | + /** |
|
| 37 | + * @var int $amount |
|
| 38 | + */ |
|
| 39 | + private $amount; |
|
| 40 | + |
|
| 41 | + /** |
|
| 42 | + * @var Currency $currency |
|
| 43 | + */ |
|
| 44 | + private $currency; |
|
| 45 | + |
|
| 46 | + /** |
|
| 47 | + * @var Calculator $calculator |
|
| 48 | + */ |
|
| 49 | + protected $calculator; |
|
| 50 | + |
|
| 51 | + /** |
|
| 52 | + * @var MoneyFormatter[] $formatters |
|
| 53 | + */ |
|
| 54 | + protected $formatters; |
|
| 55 | + |
|
| 56 | + |
|
| 57 | + |
|
| 58 | + /** |
|
| 59 | + * Money constructor. |
|
| 60 | + * |
|
| 61 | + * @param float|int|string $amount money amount IN THE STANDARD UNIT FOR THE CURRENCY ie: dollars, Euros, etc |
|
| 62 | + * example: $12.5 USD would equate to a value amount of 12.50 |
|
| 63 | + * @param Currency $currency |
|
| 64 | + * @param Calculator $calculator |
|
| 65 | + * @param MoneyFormatter[] $formatters |
|
| 66 | + * @throws InvalidDataTypeException |
|
| 67 | + */ |
|
| 68 | + public function __construct($amount, Currency $currency, Calculator $calculator, array $formatters) |
|
| 69 | + { |
|
| 70 | + $this->currency = $currency; |
|
| 71 | + $this->amount = (string) $this->parseAmount($amount); |
|
| 72 | + $this->calculator = $calculator; |
|
| 73 | + $this->formatters = $formatters; |
|
| 74 | + } |
|
| 75 | + |
|
| 76 | + |
|
| 77 | + |
|
| 78 | + /** |
|
| 79 | + * @return Calculator |
|
| 80 | + */ |
|
| 81 | + protected function calculator() |
|
| 82 | + { |
|
| 83 | + return $this->calculator; |
|
| 84 | + } |
|
| 85 | + |
|
| 86 | + |
|
| 87 | + |
|
| 88 | + /** |
|
| 89 | + * @return MoneyFormatter[] |
|
| 90 | + */ |
|
| 91 | + protected function formatters() |
|
| 92 | + { |
|
| 93 | + return $this->formatters; |
|
| 94 | + } |
|
| 95 | + |
|
| 96 | + |
|
| 97 | + |
|
| 98 | + /** |
|
| 99 | + * @param float|int|string $amount money amount IN THE STANDARD UNIT FOR THE CURRENCY ie: dollars, Euros, etc |
|
| 100 | + * example: $12.5 USD would equate to a value amount of 12.50 |
|
| 101 | + * @return float|int|number|string |
|
| 102 | + * @throws InvalidDataTypeException |
|
| 103 | + */ |
|
| 104 | + private function parseAmount($amount) |
|
| 105 | + { |
|
| 106 | + $type = gettype($amount); |
|
| 107 | + switch ($type) { |
|
| 108 | + case 'integer' : |
|
| 109 | + case 'double' : |
|
| 110 | + case 'string' : |
|
| 111 | + break; |
|
| 112 | + default : |
|
| 113 | + throw new InvalidDataTypeException( |
|
| 114 | + '$amount', |
|
| 115 | + $amount, |
|
| 116 | + 'integer (or float or string)' |
|
| 117 | + ); |
|
| 118 | + } |
|
| 119 | + if ($this->currency->decimalMark() !== '.') { |
|
| 120 | + // remove thousands separator and replace decimal place with standard decimal. |
|
| 121 | + $amount = str_replace( |
|
| 122 | + array( |
|
| 123 | + $this->currency->thousands(), |
|
| 124 | + $this->currency->decimalMark(), |
|
| 125 | + ), |
|
| 126 | + array( |
|
| 127 | + '', |
|
| 128 | + '.', |
|
| 129 | + ), |
|
| 130 | + $amount |
|
| 131 | + ); |
|
| 132 | + } |
|
| 133 | + // remove any non numeric values but leave the decimal |
|
| 134 | + $amount = (float) preg_replace('/([^0-9\\.])/', '', $amount); |
|
| 135 | + // shift the decimal position by the number of decimal places used internally |
|
| 136 | + // ex: 12.5 for a currency using 2 decimal places, would become 1250 |
|
| 137 | + // then if our extra internal precision was 3, it would become 1250000 |
|
| 138 | + $amount *= pow(10, $this->precision()); |
|
| 139 | + // then round up the remaining value if there is still a fractional amount left |
|
| 140 | + $amount = round($amount); |
|
| 141 | + return $amount; |
|
| 142 | + } |
|
| 143 | + |
|
| 144 | + |
|
| 145 | + |
|
| 146 | + /** |
|
| 147 | + * adds or subtracts additional decimal places based on the value of the Money::EXTRA_PRECISION constant |
|
| 148 | + * |
|
| 149 | + * @param bool $positive |
|
| 150 | + * @return int |
|
| 151 | + */ |
|
| 152 | + private function precision($positive = true) |
|
| 153 | + { |
|
| 154 | + $sign = $positive ? 1 : -1; |
|
| 155 | + return ((int) $this->currency->decimalPlaces() + Money::EXTRA_PRECISION) * $sign; |
|
| 156 | + } |
|
| 157 | + |
|
| 158 | + |
|
| 159 | + |
|
| 160 | + /** |
|
| 161 | + * Returns the money amount as an unformatted string |
|
| 162 | + * IF YOU REQUIRE A FORMATTED STRING, THEN USE Money::format() |
|
| 163 | + * |
|
| 164 | + * @return string |
|
| 165 | + */ |
|
| 166 | + public function amount() |
|
| 167 | + { |
|
| 168 | + // shift the decimal position BACK by the number of decimal places used internally |
|
| 169 | + // ex: 1250 for a currency using 2 decimal places, would become 12.50 |
|
| 170 | + $amount = (string) $this->amount * pow(10, $this->precision(false)); |
|
| 171 | + // then shave off our extra internal precision using the number of decimal places for the currency |
|
| 172 | + $amount = round($amount, $this->currency->decimalPlaces()); |
|
| 173 | + return $amount; |
|
| 174 | + } |
|
| 175 | + |
|
| 176 | + |
|
| 177 | + |
|
| 178 | + /** |
|
| 179 | + * Returns the money SUBUNITS amount as an INTEGER |
|
| 180 | + * |
|
| 181 | + * @return integer |
|
| 182 | + */ |
|
| 183 | + public function amountInSubunits() |
|
| 184 | + { |
|
| 185 | + // shift the decimal position BACK |
|
| 186 | + // by the number of decimal places used internally by the extra internal precision |
|
| 187 | + // ex: if our extra internal precision was 3, |
|
| 188 | + // then 1250000 would become 1250 |
|
| 189 | + $amount = (string) $this->amount * pow(10, Money::EXTRA_PRECISION * -1); |
|
| 190 | + // then shave off anything after the decimal |
|
| 191 | + $amount = round($amount); |
|
| 192 | + return $amount; |
|
| 193 | + } |
|
| 194 | + |
|
| 195 | + |
|
| 196 | + |
|
| 197 | + /** |
|
| 198 | + * applies formatting based on the specified formatting level |
|
| 199 | + * corresponding to one of the constants on \EventEspresso\core\services\currency\MoneyFormatter |
|
| 200 | + * |
|
| 201 | + * @param int $formatting_level |
|
| 202 | + * @return string |
|
| 203 | + */ |
|
| 204 | + public function format($formatting_level = MoneyFormatter::ADD_THOUSANDS) |
|
| 205 | + { |
|
| 206 | + $formatted_amount = $this->amount(); |
|
| 207 | + $formatters = $this->formatters(); |
|
| 208 | + // if we are applying thousands formatting... |
|
| 209 | + if ($formatting_level >= MoneyFormatter::ADD_THOUSANDS) { |
|
| 210 | + // then let's remove decimal formatting since it's included in thousands formatting |
|
| 211 | + unset($formatters[ MoneyFormatter::DECIMAL_ONLY ]); |
|
| 212 | + } |
|
| 213 | + for ($x = 1; $x <= $formatting_level; $x++) { |
|
| 214 | + if (isset($formatters[ $x ]) && $formatters[ $x ] instanceof MoneyFormatter) { |
|
| 215 | + $formatted_amount = $formatters[ $x ]->format($formatted_amount, $this->currency); |
|
| 216 | + } |
|
| 217 | + } |
|
| 218 | + return $formatted_amount; |
|
| 219 | + } |
|
| 220 | + |
|
| 221 | + |
|
| 222 | + |
|
| 223 | + /** |
|
| 224 | + * Returns the Currency object for this money |
|
| 225 | + * |
|
| 226 | + * @return Currency |
|
| 227 | + */ |
|
| 228 | + public function currency() |
|
| 229 | + { |
|
| 230 | + return $this->currency; |
|
| 231 | + } |
|
| 232 | + |
|
| 233 | + |
|
| 234 | + |
|
| 235 | + /** |
|
| 236 | + * adds the supplied Money amount to this Money amount |
|
| 237 | + * and returns a new Money object |
|
| 238 | + * |
|
| 239 | + * @param Money $other |
|
| 240 | + * @return Money |
|
| 241 | + * @throws InvalidArgumentException |
|
| 242 | + */ |
|
| 243 | + public function add(Money $other) |
|
| 244 | + { |
|
| 245 | + $this->verifySameCurrency($other->currency()); |
|
| 246 | + return new Money( |
|
| 247 | + $this->calculator()->add( |
|
| 248 | + $this->amount(), |
|
| 249 | + $other->amount() |
|
| 250 | + ), |
|
| 251 | + $this->currency(), |
|
| 252 | + $this->calculator(), |
|
| 253 | + $this->formatters() |
|
| 254 | + ); |
|
| 255 | + } |
|
| 256 | + |
|
| 257 | + |
|
| 258 | + |
|
| 259 | + /** |
|
| 260 | + * subtracts the supplied Money amount from this Money amount |
|
| 261 | + * and returns a new Money object |
|
| 262 | + * |
|
| 263 | + * @param Money $other |
|
| 264 | + * @return Money |
|
| 265 | + * @throws InvalidArgumentException |
|
| 266 | + */ |
|
| 267 | + public function subtract(Money $other) |
|
| 268 | + { |
|
| 269 | + $this->verifySameCurrency($other->currency()); |
|
| 270 | + return new Money( |
|
| 271 | + $this->calculator()->subtract( |
|
| 272 | + $this->amount(), |
|
| 273 | + $other->amount() |
|
| 274 | + ), |
|
| 275 | + $this->currency(), |
|
| 276 | + $this->calculator(), |
|
| 277 | + $this->formatters() |
|
| 278 | + ); |
|
| 279 | + } |
|
| 280 | + |
|
| 281 | + |
|
| 282 | + |
|
| 283 | + /** |
|
| 284 | + * multiplies this Money amount by the supplied $multiplier |
|
| 285 | + * and returns a new Money object |
|
| 286 | + * |
|
| 287 | + * @param float|int|string $multiplier |
|
| 288 | + * @param int $rounding_mode |
|
| 289 | + * @return Money |
|
| 290 | + * @throws InvalidDataTypeException |
|
| 291 | + */ |
|
| 292 | + public function multiply($multiplier, $rounding_mode = Calculator::ROUND_HALF_UP) |
|
| 293 | + { |
|
| 294 | + return new Money( |
|
| 295 | + $this->calculator()->multiply( |
|
| 296 | + $this->amount(), |
|
| 297 | + $multiplier, |
|
| 298 | + $this->precision(), |
|
| 299 | + $rounding_mode |
|
| 300 | + ), |
|
| 301 | + $this->currency(), |
|
| 302 | + $this->calculator(), |
|
| 303 | + $this->formatters() |
|
| 304 | + ); |
|
| 305 | + } |
|
| 306 | + |
|
| 307 | + |
|
| 308 | + |
|
| 309 | + /** |
|
| 310 | + * divides this Money amount by the supplied $divisor |
|
| 311 | + * and returns a new Money object |
|
| 312 | + * |
|
| 313 | + * @param float|int|string $divisor |
|
| 314 | + * @param int $rounding_mode |
|
| 315 | + * @return Money |
|
| 316 | + * @throws InvalidDataTypeException |
|
| 317 | + */ |
|
| 318 | + public function divide($divisor, $rounding_mode = Calculator::ROUND_HALF_UP) |
|
| 319 | + { |
|
| 320 | + return new Money( |
|
| 321 | + $this->calculator()->divide( |
|
| 322 | + $this->amount(), |
|
| 323 | + $divisor, |
|
| 324 | + $this->precision(), |
|
| 325 | + $rounding_mode |
|
| 326 | + ), |
|
| 327 | + $this->currency(), |
|
| 328 | + $this->calculator(), |
|
| 329 | + $this->formatters() |
|
| 330 | + ); |
|
| 331 | + } |
|
| 332 | + |
|
| 333 | + |
|
| 334 | + |
|
| 335 | + /** |
|
| 336 | + * @param Currency $other_currency |
|
| 337 | + * @throws InvalidArgumentException |
|
| 338 | + */ |
|
| 339 | + public function verifySameCurrency(Currency $other_currency) |
|
| 340 | + { |
|
| 341 | + if ($this->currency()->equals($other_currency) !== true) { |
|
| 342 | + throw new InvalidArgumentException( |
|
| 343 | + esc_html__( |
|
| 344 | + 'Currencies must be the same in order to add or subtract their values.', |
|
| 345 | + 'event_espresso' |
|
| 346 | + ) |
|
| 347 | + ); |
|
| 348 | + } |
|
| 349 | + } |
|
| 350 | + |
|
| 351 | + |
|
| 352 | + |
|
| 353 | + /** |
|
| 354 | + * @return string |
|
| 355 | + */ |
|
| 356 | + public function __toString() |
|
| 357 | + { |
|
| 358 | + return $this->format(MoneyFormatter::DECIMAL_ONLY); |
|
| 359 | + } |
|
| 360 | + |
|
| 361 | + |
|
| 362 | + |
|
| 363 | + /** |
|
| 364 | + * factory method that returns a Money object for the currency specified as if it were a class method |
|
| 365 | + * example: Money::USD(12.5); |
|
| 366 | + * money amount IN THE STANDARD UNIT FOR THE CURRENCY ie: dollars, Euros, etc |
|
| 367 | + * example: $12.5 USD would equate to a value amount of 12.50 |
|
| 368 | + * |
|
| 369 | + * @param string $currency_code |
|
| 370 | + * @param array $arguments |
|
| 371 | + * @return Money |
|
| 372 | + * @throws InvalidArgumentException |
|
| 373 | + * @throws InvalidDataTypeException |
|
| 374 | + * @throws EE_Error |
|
| 375 | + * @throws InvalidInterfaceException |
|
| 376 | + */ |
|
| 377 | + public static function __callStatic($currency_code, $arguments) |
|
| 378 | + { |
|
| 379 | + return new Money( |
|
| 380 | + $arguments[0], |
|
| 381 | + CreateCurrency::fromCode($currency_code), |
|
| 382 | + CreateMoney::calculator(), |
|
| 383 | + CreateMoney::formatters() |
|
| 384 | + ); |
|
| 385 | + } |
|
| 386 | 386 | |
| 387 | 387 | |
| 388 | 388 | } |
@@ -21,82 +21,82 @@ |
||
| 21 | 21 | |
| 22 | 22 | |
| 23 | 23 | |
| 24 | - /** |
|
| 25 | - * returns true if a calculator is supported as determined by loaded server extensions |
|
| 26 | - * |
|
| 27 | - * @return bool |
|
| 28 | - */ |
|
| 29 | - public function isSupported() |
|
| 30 | - { |
|
| 31 | - return true; |
|
| 32 | - } |
|
| 33 | - |
|
| 34 | - |
|
| 35 | - |
|
| 36 | - /** |
|
| 37 | - * adds the supplied Money amount to this Money amount |
|
| 38 | - * and returns a new Money object |
|
| 39 | - * |
|
| 40 | - * @param float|int|string $amount |
|
| 41 | - * @param float|int|string $amount_to_add |
|
| 42 | - * @return string |
|
| 43 | - */ |
|
| 44 | - public function add($amount, $amount_to_add) |
|
| 45 | - { |
|
| 46 | - return (string) ($amount + $amount_to_add); |
|
| 47 | - } |
|
| 48 | - |
|
| 49 | - |
|
| 50 | - |
|
| 51 | - /** |
|
| 52 | - * subtracts the supplied Money amount from this Money amount |
|
| 53 | - * and returns a new Money object |
|
| 54 | - * |
|
| 55 | - * @param float|int|string $amount |
|
| 56 | - * @param float|int|string $amount_to_subtract |
|
| 57 | - * @return string |
|
| 58 | - */ |
|
| 59 | - public function subtract($amount, $amount_to_subtract) |
|
| 60 | - { |
|
| 61 | - return (string) ($amount - $amount_to_subtract); |
|
| 62 | - } |
|
| 63 | - |
|
| 64 | - |
|
| 65 | - |
|
| 66 | - /** |
|
| 67 | - * multiplies this Money amount by the supplied $multiplier |
|
| 68 | - * and returns a new Money object |
|
| 69 | - * |
|
| 70 | - * @param float|int|string $amount |
|
| 71 | - * @param float|int|string $multiplier |
|
| 72 | - * @param int $precision |
|
| 73 | - * @param int $rounding_mode |
|
| 74 | - * @return string |
|
| 75 | - * @throws InvalidArgumentException |
|
| 76 | - */ |
|
| 77 | - public function multiply($amount, $multiplier, $precision = 3, $rounding_mode = Calculator::ROUND_HALF_UP) |
|
| 78 | - { |
|
| 79 | - return $this->round($amount * $multiplier, $precision, $rounding_mode); |
|
| 80 | - } |
|
| 81 | - |
|
| 82 | - |
|
| 83 | - |
|
| 84 | - /** |
|
| 85 | - * divides this Money amount by the supplied $divisor |
|
| 86 | - * and returns a new Money object |
|
| 87 | - * |
|
| 88 | - * @param float|int|string $amount |
|
| 89 | - * @param float|int|string $divisor |
|
| 90 | - * @param int $precision |
|
| 91 | - * @param int $rounding_mode |
|
| 92 | - * @return string |
|
| 93 | - * @throws InvalidArgumentException |
|
| 94 | - */ |
|
| 95 | - public function divide($amount, $divisor, $precision = 3, $rounding_mode = Calculator::ROUND_HALF_UP) |
|
| 96 | - { |
|
| 97 | - $this->validateDivisor($divisor); |
|
| 98 | - return $this->round($amount / $divisor, $precision, $rounding_mode); |
|
| 99 | - } |
|
| 24 | + /** |
|
| 25 | + * returns true if a calculator is supported as determined by loaded server extensions |
|
| 26 | + * |
|
| 27 | + * @return bool |
|
| 28 | + */ |
|
| 29 | + public function isSupported() |
|
| 30 | + { |
|
| 31 | + return true; |
|
| 32 | + } |
|
| 33 | + |
|
| 34 | + |
|
| 35 | + |
|
| 36 | + /** |
|
| 37 | + * adds the supplied Money amount to this Money amount |
|
| 38 | + * and returns a new Money object |
|
| 39 | + * |
|
| 40 | + * @param float|int|string $amount |
|
| 41 | + * @param float|int|string $amount_to_add |
|
| 42 | + * @return string |
|
| 43 | + */ |
|
| 44 | + public function add($amount, $amount_to_add) |
|
| 45 | + { |
|
| 46 | + return (string) ($amount + $amount_to_add); |
|
| 47 | + } |
|
| 48 | + |
|
| 49 | + |
|
| 50 | + |
|
| 51 | + /** |
|
| 52 | + * subtracts the supplied Money amount from this Money amount |
|
| 53 | + * and returns a new Money object |
|
| 54 | + * |
|
| 55 | + * @param float|int|string $amount |
|
| 56 | + * @param float|int|string $amount_to_subtract |
|
| 57 | + * @return string |
|
| 58 | + */ |
|
| 59 | + public function subtract($amount, $amount_to_subtract) |
|
| 60 | + { |
|
| 61 | + return (string) ($amount - $amount_to_subtract); |
|
| 62 | + } |
|
| 63 | + |
|
| 64 | + |
|
| 65 | + |
|
| 66 | + /** |
|
| 67 | + * multiplies this Money amount by the supplied $multiplier |
|
| 68 | + * and returns a new Money object |
|
| 69 | + * |
|
| 70 | + * @param float|int|string $amount |
|
| 71 | + * @param float|int|string $multiplier |
|
| 72 | + * @param int $precision |
|
| 73 | + * @param int $rounding_mode |
|
| 74 | + * @return string |
|
| 75 | + * @throws InvalidArgumentException |
|
| 76 | + */ |
|
| 77 | + public function multiply($amount, $multiplier, $precision = 3, $rounding_mode = Calculator::ROUND_HALF_UP) |
|
| 78 | + { |
|
| 79 | + return $this->round($amount * $multiplier, $precision, $rounding_mode); |
|
| 80 | + } |
|
| 81 | + |
|
| 82 | + |
|
| 83 | + |
|
| 84 | + /** |
|
| 85 | + * divides this Money amount by the supplied $divisor |
|
| 86 | + * and returns a new Money object |
|
| 87 | + * |
|
| 88 | + * @param float|int|string $amount |
|
| 89 | + * @param float|int|string $divisor |
|
| 90 | + * @param int $precision |
|
| 91 | + * @param int $rounding_mode |
|
| 92 | + * @return string |
|
| 93 | + * @throws InvalidArgumentException |
|
| 94 | + */ |
|
| 95 | + public function divide($amount, $divisor, $precision = 3, $rounding_mode = Calculator::ROUND_HALF_UP) |
|
| 96 | + { |
|
| 97 | + $this->validateDivisor($divisor); |
|
| 98 | + return $this->round($amount / $divisor, $precision, $rounding_mode); |
|
| 99 | + } |
|
| 100 | 100 | |
| 101 | 101 | |
| 102 | 102 | |
@@ -28,148 +28,148 @@ |
||
| 28 | 28 | class CreateCurrency |
| 29 | 29 | { |
| 30 | 30 | |
| 31 | - /** |
|
| 32 | - * @var EE_Country[] $countries |
|
| 33 | - */ |
|
| 34 | - protected static $countries_by_iso_code; |
|
| 35 | - |
|
| 36 | - /** |
|
| 37 | - * @var EE_Country[] $countries |
|
| 38 | - */ |
|
| 39 | - protected static $countries_by_currency; |
|
| 40 | - |
|
| 41 | - /** |
|
| 42 | - * @var string $site_country_iso |
|
| 43 | - */ |
|
| 44 | - protected static $site_country_iso; |
|
| 45 | - |
|
| 46 | - |
|
| 47 | - /** |
|
| 48 | - * @return EEM_Country |
|
| 49 | - * @throws InvalidArgumentException |
|
| 50 | - * @throws InvalidDataTypeException |
|
| 51 | - * @throws InvalidInterfaceException |
|
| 52 | - */ |
|
| 53 | - private static function countryModel() |
|
| 54 | - { |
|
| 55 | - return LoaderFactory::getLoader()->getShared('EEM_Country'); |
|
| 56 | - } |
|
| 57 | - |
|
| 58 | - |
|
| 59 | - /** |
|
| 60 | - * returns a Currency object for the supplied country code |
|
| 61 | - * |
|
| 62 | - * @param string $CNT_ISO |
|
| 63 | - * @return Currency |
|
| 64 | - * @throws InvalidDataTypeException |
|
| 65 | - * @throws InvalidInterfaceException |
|
| 66 | - * @throws EE_Error |
|
| 67 | - * @throws InvalidArgumentException |
|
| 68 | - */ |
|
| 69 | - public static function fromCountryCode($CNT_ISO = null) |
|
| 70 | - { |
|
| 71 | - $CNT_ISO = $CNT_ISO !== null ? $CNT_ISO : CreateCurrency::getSiteCountryIso(); |
|
| 72 | - if(isset(CreateCurrency::$countries_by_iso_code[$CNT_ISO])) { |
|
| 73 | - $country = CreateCurrency::$countries_by_iso_code[ $CNT_ISO ]; |
|
| 74 | - } else { |
|
| 75 | - /** @var EE_Country $country */ |
|
| 76 | - $country = CreateCurrency::countryModel()->get_one_by_ID($CNT_ISO); |
|
| 77 | - if (! $country instanceof EE_Country) { |
|
| 78 | - throw new InvalidArgumentException( |
|
| 79 | - sprintf( |
|
| 80 | - esc_html__( |
|
| 81 | - 'A valid country could not be found for the "%1$s" country code;', |
|
| 82 | - 'event_espresso' |
|
| 83 | - ), |
|
| 84 | - $CNT_ISO |
|
| 85 | - ) |
|
| 86 | - ); |
|
| 87 | - } |
|
| 88 | - CreateCurrency::$countries_by_iso_code[ $CNT_ISO ] = $country; |
|
| 89 | - CreateCurrency::$countries_by_currency[ $country->currency_code() ] = $country; |
|
| 90 | - } |
|
| 91 | - return new Currency( |
|
| 92 | - $country->currency_code(), |
|
| 93 | - new Label( |
|
| 94 | - $country->currency_name_single(), |
|
| 95 | - $country->currency_name_plural() |
|
| 96 | - ), |
|
| 97 | - $country->currency_sign(), |
|
| 98 | - $country->currency_sign_before(), |
|
| 99 | - $country->currency_decimal_places(), |
|
| 100 | - $country->currency_decimal_mark(), |
|
| 101 | - $country->currency_thousands_separator() |
|
| 102 | - ); |
|
| 103 | - } |
|
| 104 | - |
|
| 105 | - |
|
| 106 | - |
|
| 107 | - /** |
|
| 108 | - * returns a Currency object for the supplied currency code |
|
| 109 | - * PLZ NOTE: variations may exist between how different countries display the same currency, |
|
| 110 | - * so it may be necessary to use CreateCurrency::fromCountryCode() to achieve the desired results |
|
| 111 | - * |
|
| 112 | - * @param string $code |
|
| 113 | - * @return Currency |
|
| 114 | - * @throws InvalidInterfaceException |
|
| 115 | - * @throws InvalidDataTypeException |
|
| 116 | - * @throws InvalidArgumentException |
|
| 117 | - * @throws EE_Error |
|
| 118 | - */ |
|
| 119 | - public static function fromCode($code) |
|
| 120 | - { |
|
| 121 | - if (isset(CreateCurrency::$countries_by_currency[ $code ])) { |
|
| 122 | - $country = CreateCurrency::$countries_by_currency[ $code ]; |
|
| 123 | - } else { |
|
| 124 | - /** @var EE_Country $country */ |
|
| 125 | - $country = CreateCurrency::countryModel()->get_one(array(array('CNT_cur_code' => $code))); |
|
| 126 | - if (! $country instanceof EE_Country) { |
|
| 127 | - throw new InvalidArgumentException( |
|
| 128 | - sprintf( |
|
| 129 | - esc_html__( |
|
| 130 | - 'A valid currency could not be found for the "%1$s" currency code;', |
|
| 131 | - 'event_espresso' |
|
| 132 | - ), |
|
| 133 | - $code |
|
| 134 | - ) |
|
| 135 | - ); |
|
| 136 | - } |
|
| 137 | - CreateCurrency::$countries_by_iso_code[ $country->ID() ] = $country; |
|
| 138 | - CreateCurrency::$countries_by_currency[$code ] = $country; |
|
| 139 | - } |
|
| 140 | - return new Currency( |
|
| 141 | - $country->currency_code(), |
|
| 142 | - new Label( |
|
| 143 | - $country->currency_name_single(), |
|
| 144 | - $country->currency_name_plural() |
|
| 145 | - ), |
|
| 146 | - $country->currency_sign(), |
|
| 147 | - $country->currency_sign_before(), |
|
| 148 | - $country->currency_decimal_places(), |
|
| 149 | - $country->currency_decimal_mark(), |
|
| 150 | - $country->currency_thousands_separator() |
|
| 151 | - ); |
|
| 152 | - } |
|
| 153 | - |
|
| 154 | - |
|
| 155 | - |
|
| 156 | - /** |
|
| 157 | - * @return string |
|
| 158 | - * @throws InvalidArgumentException |
|
| 159 | - * @throws InvalidDataTypeException |
|
| 160 | - * @throws InvalidInterfaceException |
|
| 161 | - */ |
|
| 162 | - public static function getSiteCountryIso() |
|
| 163 | - { |
|
| 164 | - if (empty(CreateCurrency::$site_country_iso)) { |
|
| 165 | - $config = LoaderFactory::getLoader()->getShared('EE_Config'); |
|
| 166 | - CreateCurrency::$site_country_iso = $config->organization !== null |
|
| 167 | - && $config->organization instanceof EE_Organization_Config |
|
| 168 | - ? $config->organization->CNT_ISO |
|
| 169 | - : 'US'; |
|
| 170 | - } |
|
| 171 | - return CreateCurrency::$site_country_iso; |
|
| 172 | - } |
|
| 31 | + /** |
|
| 32 | + * @var EE_Country[] $countries |
|
| 33 | + */ |
|
| 34 | + protected static $countries_by_iso_code; |
|
| 35 | + |
|
| 36 | + /** |
|
| 37 | + * @var EE_Country[] $countries |
|
| 38 | + */ |
|
| 39 | + protected static $countries_by_currency; |
|
| 40 | + |
|
| 41 | + /** |
|
| 42 | + * @var string $site_country_iso |
|
| 43 | + */ |
|
| 44 | + protected static $site_country_iso; |
|
| 45 | + |
|
| 46 | + |
|
| 47 | + /** |
|
| 48 | + * @return EEM_Country |
|
| 49 | + * @throws InvalidArgumentException |
|
| 50 | + * @throws InvalidDataTypeException |
|
| 51 | + * @throws InvalidInterfaceException |
|
| 52 | + */ |
|
| 53 | + private static function countryModel() |
|
| 54 | + { |
|
| 55 | + return LoaderFactory::getLoader()->getShared('EEM_Country'); |
|
| 56 | + } |
|
| 57 | + |
|
| 58 | + |
|
| 59 | + /** |
|
| 60 | + * returns a Currency object for the supplied country code |
|
| 61 | + * |
|
| 62 | + * @param string $CNT_ISO |
|
| 63 | + * @return Currency |
|
| 64 | + * @throws InvalidDataTypeException |
|
| 65 | + * @throws InvalidInterfaceException |
|
| 66 | + * @throws EE_Error |
|
| 67 | + * @throws InvalidArgumentException |
|
| 68 | + */ |
|
| 69 | + public static function fromCountryCode($CNT_ISO = null) |
|
| 70 | + { |
|
| 71 | + $CNT_ISO = $CNT_ISO !== null ? $CNT_ISO : CreateCurrency::getSiteCountryIso(); |
|
| 72 | + if(isset(CreateCurrency::$countries_by_iso_code[$CNT_ISO])) { |
|
| 73 | + $country = CreateCurrency::$countries_by_iso_code[ $CNT_ISO ]; |
|
| 74 | + } else { |
|
| 75 | + /** @var EE_Country $country */ |
|
| 76 | + $country = CreateCurrency::countryModel()->get_one_by_ID($CNT_ISO); |
|
| 77 | + if (! $country instanceof EE_Country) { |
|
| 78 | + throw new InvalidArgumentException( |
|
| 79 | + sprintf( |
|
| 80 | + esc_html__( |
|
| 81 | + 'A valid country could not be found for the "%1$s" country code;', |
|
| 82 | + 'event_espresso' |
|
| 83 | + ), |
|
| 84 | + $CNT_ISO |
|
| 85 | + ) |
|
| 86 | + ); |
|
| 87 | + } |
|
| 88 | + CreateCurrency::$countries_by_iso_code[ $CNT_ISO ] = $country; |
|
| 89 | + CreateCurrency::$countries_by_currency[ $country->currency_code() ] = $country; |
|
| 90 | + } |
|
| 91 | + return new Currency( |
|
| 92 | + $country->currency_code(), |
|
| 93 | + new Label( |
|
| 94 | + $country->currency_name_single(), |
|
| 95 | + $country->currency_name_plural() |
|
| 96 | + ), |
|
| 97 | + $country->currency_sign(), |
|
| 98 | + $country->currency_sign_before(), |
|
| 99 | + $country->currency_decimal_places(), |
|
| 100 | + $country->currency_decimal_mark(), |
|
| 101 | + $country->currency_thousands_separator() |
|
| 102 | + ); |
|
| 103 | + } |
|
| 104 | + |
|
| 105 | + |
|
| 106 | + |
|
| 107 | + /** |
|
| 108 | + * returns a Currency object for the supplied currency code |
|
| 109 | + * PLZ NOTE: variations may exist between how different countries display the same currency, |
|
| 110 | + * so it may be necessary to use CreateCurrency::fromCountryCode() to achieve the desired results |
|
| 111 | + * |
|
| 112 | + * @param string $code |
|
| 113 | + * @return Currency |
|
| 114 | + * @throws InvalidInterfaceException |
|
| 115 | + * @throws InvalidDataTypeException |
|
| 116 | + * @throws InvalidArgumentException |
|
| 117 | + * @throws EE_Error |
|
| 118 | + */ |
|
| 119 | + public static function fromCode($code) |
|
| 120 | + { |
|
| 121 | + if (isset(CreateCurrency::$countries_by_currency[ $code ])) { |
|
| 122 | + $country = CreateCurrency::$countries_by_currency[ $code ]; |
|
| 123 | + } else { |
|
| 124 | + /** @var EE_Country $country */ |
|
| 125 | + $country = CreateCurrency::countryModel()->get_one(array(array('CNT_cur_code' => $code))); |
|
| 126 | + if (! $country instanceof EE_Country) { |
|
| 127 | + throw new InvalidArgumentException( |
|
| 128 | + sprintf( |
|
| 129 | + esc_html__( |
|
| 130 | + 'A valid currency could not be found for the "%1$s" currency code;', |
|
| 131 | + 'event_espresso' |
|
| 132 | + ), |
|
| 133 | + $code |
|
| 134 | + ) |
|
| 135 | + ); |
|
| 136 | + } |
|
| 137 | + CreateCurrency::$countries_by_iso_code[ $country->ID() ] = $country; |
|
| 138 | + CreateCurrency::$countries_by_currency[$code ] = $country; |
|
| 139 | + } |
|
| 140 | + return new Currency( |
|
| 141 | + $country->currency_code(), |
|
| 142 | + new Label( |
|
| 143 | + $country->currency_name_single(), |
|
| 144 | + $country->currency_name_plural() |
|
| 145 | + ), |
|
| 146 | + $country->currency_sign(), |
|
| 147 | + $country->currency_sign_before(), |
|
| 148 | + $country->currency_decimal_places(), |
|
| 149 | + $country->currency_decimal_mark(), |
|
| 150 | + $country->currency_thousands_separator() |
|
| 151 | + ); |
|
| 152 | + } |
|
| 153 | + |
|
| 154 | + |
|
| 155 | + |
|
| 156 | + /** |
|
| 157 | + * @return string |
|
| 158 | + * @throws InvalidArgumentException |
|
| 159 | + * @throws InvalidDataTypeException |
|
| 160 | + * @throws InvalidInterfaceException |
|
| 161 | + */ |
|
| 162 | + public static function getSiteCountryIso() |
|
| 163 | + { |
|
| 164 | + if (empty(CreateCurrency::$site_country_iso)) { |
|
| 165 | + $config = LoaderFactory::getLoader()->getShared('EE_Config'); |
|
| 166 | + CreateCurrency::$site_country_iso = $config->organization !== null |
|
| 167 | + && $config->organization instanceof EE_Organization_Config |
|
| 168 | + ? $config->organization->CNT_ISO |
|
| 169 | + : 'US'; |
|
| 170 | + } |
|
| 171 | + return CreateCurrency::$site_country_iso; |
|
| 172 | + } |
|
| 173 | 173 | |
| 174 | 174 | |
| 175 | 175 | |
@@ -69,12 +69,12 @@ discard block |
||
| 69 | 69 | public static function fromCountryCode($CNT_ISO = null) |
| 70 | 70 | { |
| 71 | 71 | $CNT_ISO = $CNT_ISO !== null ? $CNT_ISO : CreateCurrency::getSiteCountryIso(); |
| 72 | - if(isset(CreateCurrency::$countries_by_iso_code[$CNT_ISO])) { |
|
| 73 | - $country = CreateCurrency::$countries_by_iso_code[ $CNT_ISO ]; |
|
| 72 | + if (isset(CreateCurrency::$countries_by_iso_code[$CNT_ISO])) { |
|
| 73 | + $country = CreateCurrency::$countries_by_iso_code[$CNT_ISO]; |
|
| 74 | 74 | } else { |
| 75 | 75 | /** @var EE_Country $country */ |
| 76 | 76 | $country = CreateCurrency::countryModel()->get_one_by_ID($CNT_ISO); |
| 77 | - if (! $country instanceof EE_Country) { |
|
| 77 | + if ( ! $country instanceof EE_Country) { |
|
| 78 | 78 | throw new InvalidArgumentException( |
| 79 | 79 | sprintf( |
| 80 | 80 | esc_html__( |
@@ -85,8 +85,8 @@ discard block |
||
| 85 | 85 | ) |
| 86 | 86 | ); |
| 87 | 87 | } |
| 88 | - CreateCurrency::$countries_by_iso_code[ $CNT_ISO ] = $country; |
|
| 89 | - CreateCurrency::$countries_by_currency[ $country->currency_code() ] = $country; |
|
| 88 | + CreateCurrency::$countries_by_iso_code[$CNT_ISO] = $country; |
|
| 89 | + CreateCurrency::$countries_by_currency[$country->currency_code()] = $country; |
|
| 90 | 90 | } |
| 91 | 91 | return new Currency( |
| 92 | 92 | $country->currency_code(), |
@@ -118,12 +118,12 @@ discard block |
||
| 118 | 118 | */ |
| 119 | 119 | public static function fromCode($code) |
| 120 | 120 | { |
| 121 | - if (isset(CreateCurrency::$countries_by_currency[ $code ])) { |
|
| 122 | - $country = CreateCurrency::$countries_by_currency[ $code ]; |
|
| 121 | + if (isset(CreateCurrency::$countries_by_currency[$code])) { |
|
| 122 | + $country = CreateCurrency::$countries_by_currency[$code]; |
|
| 123 | 123 | } else { |
| 124 | 124 | /** @var EE_Country $country */ |
| 125 | 125 | $country = CreateCurrency::countryModel()->get_one(array(array('CNT_cur_code' => $code))); |
| 126 | - if (! $country instanceof EE_Country) { |
|
| 126 | + if ( ! $country instanceof EE_Country) { |
|
| 127 | 127 | throw new InvalidArgumentException( |
| 128 | 128 | sprintf( |
| 129 | 129 | esc_html__( |
@@ -134,8 +134,8 @@ discard block |
||
| 134 | 134 | ) |
| 135 | 135 | ); |
| 136 | 136 | } |
| 137 | - CreateCurrency::$countries_by_iso_code[ $country->ID() ] = $country; |
|
| 138 | - CreateCurrency::$countries_by_currency[$code ] = $country; |
|
| 137 | + CreateCurrency::$countries_by_iso_code[$country->ID()] = $country; |
|
| 138 | + CreateCurrency::$countries_by_currency[$code] = $country; |
|
| 139 | 139 | } |
| 140 | 140 | return new Currency( |
| 141 | 141 | $country->currency_code(), |
@@ -19,14 +19,14 @@ |
||
| 19 | 19 | class CurrencyCodeMoneyFormatter implements MoneyFormatter |
| 20 | 20 | { |
| 21 | 21 | |
| 22 | - /** |
|
| 23 | - * @param string $money_amount |
|
| 24 | - * @param Currency $currency |
|
| 25 | - * @return string |
|
| 26 | - */ |
|
| 27 | - public function format($money_amount, Currency $currency) |
|
| 28 | - { |
|
| 29 | - return $money_amount . ' ' . $currency->code(); |
|
| 30 | - } |
|
| 22 | + /** |
|
| 23 | + * @param string $money_amount |
|
| 24 | + * @param Currency $currency |
|
| 25 | + * @return string |
|
| 26 | + */ |
|
| 27 | + public function format($money_amount, Currency $currency) |
|
| 28 | + { |
|
| 29 | + return $money_amount . ' ' . $currency->code(); |
|
| 30 | + } |
|
| 31 | 31 | |
| 32 | 32 | } |
@@ -23,190 +23,190 @@ |
||
| 23 | 23 | class CreateMoney |
| 24 | 24 | { |
| 25 | 25 | |
| 26 | - /** |
|
| 27 | - * @var Calculator $calculator |
|
| 28 | - */ |
|
| 29 | - protected static $calculator; |
|
| 30 | - |
|
| 31 | - /** |
|
| 32 | - * @var MoneyFormatter[] $formatters |
|
| 33 | - */ |
|
| 34 | - protected static $formatters; |
|
| 35 | - |
|
| 36 | - |
|
| 37 | - |
|
| 38 | - /** |
|
| 39 | - * factory method that returns a Money object using amount specified in the currency's subunits |
|
| 40 | - * example: for $12.50 USD use CreateMoney::fromSubUnits(1250, 'USD') |
|
| 41 | - * |
|
| 42 | - * @param int $subunits_amount money amount IN THE SUBUNITS FOR THE CURRENCY ie: cents |
|
| 43 | - * example: $12.50 USD would equate to a subunits amount of 1250 |
|
| 44 | - * @param string $currency_code |
|
| 45 | - * @return Money |
|
| 46 | - * @throws EE_Error |
|
| 47 | - * @throws InvalidArgumentException |
|
| 48 | - * @throws InvalidDataTypeException |
|
| 49 | - * @throws InvalidInterfaceException |
|
| 50 | - */ |
|
| 51 | - public static function fromSubUnits($subunits_amount, $currency_code = '') |
|
| 52 | - { |
|
| 53 | - $currency = CreateCurrency::fromCode($currency_code); |
|
| 54 | - return new Money( |
|
| 55 | - // shift decimal BACK by number of places for currency |
|
| 56 | - $subunits_amount * pow(10, $currency->decimalPlaces() * -1), |
|
| 57 | - $currency, |
|
| 58 | - CreateMoney::calculator(), |
|
| 59 | - CreateMoney::formatters() |
|
| 60 | - ); |
|
| 61 | - } |
|
| 62 | - |
|
| 63 | - |
|
| 64 | - |
|
| 65 | - /** |
|
| 66 | - * factory method that returns a Money object using the currency corresponding to the site's country |
|
| 67 | - * example: CreateMoney::forSite(12.5) |
|
| 68 | - * |
|
| 69 | - * @param float|int|string $amount money amount IN THE STANDARD UNIT FOR THE CURRENCY ie: dollars, Euros, etc |
|
| 70 | - * example: $12.5 USD would equate to a standard amount of 12.50 |
|
| 71 | - * @return Money |
|
| 72 | - * @throws EE_Error |
|
| 73 | - * @throws InvalidArgumentException |
|
| 74 | - * @throws InvalidDataTypeException |
|
| 75 | - * @throws InvalidInterfaceException |
|
| 76 | - */ |
|
| 77 | - public static function forSite($amount) |
|
| 78 | - { |
|
| 79 | - return new Money( |
|
| 80 | - $amount, |
|
| 81 | - CreateCurrency::fromCountryCode(), |
|
| 82 | - CreateMoney::calculator(), |
|
| 83 | - CreateMoney::formatters() |
|
| 84 | - ); |
|
| 85 | - } |
|
| 86 | - |
|
| 87 | - |
|
| 88 | - |
|
| 89 | - /** |
|
| 90 | - * factory method that returns a Money object using the currency as specified by the supplied ISO country code |
|
| 91 | - * example: CreateMoney::forCountry(12.5,'US') |
|
| 92 | - * |
|
| 93 | - * @param float|int|string $amount money amount IN THE STANDARD UNIT FOR THE CURRENCY ie: dollars, Euros, etc |
|
| 94 | - * example: $12.5 USD would equate to a value amount of 12.50 |
|
| 95 | - * @param string $CNT_ISO |
|
| 96 | - * @return Money |
|
| 97 | - * @throws EE_Error |
|
| 98 | - * @throws InvalidArgumentException |
|
| 99 | - * @throws InvalidDataTypeException |
|
| 100 | - * @throws InvalidInterfaceException |
|
| 101 | - */ |
|
| 102 | - public static function forCountry($amount, $CNT_ISO) |
|
| 103 | - { |
|
| 104 | - return new Money( |
|
| 105 | - $amount, |
|
| 106 | - CreateCurrency::fromCountryCode($CNT_ISO), |
|
| 107 | - CreateMoney::calculator(), |
|
| 108 | - CreateMoney::formatters() |
|
| 109 | - ); |
|
| 110 | - } |
|
| 111 | - |
|
| 112 | - |
|
| 113 | - |
|
| 114 | - /** |
|
| 115 | - * factory method that returns a Money object using the currency as specified by the supplied currency code |
|
| 116 | - * example: CreateMoney::forCurrency(12.5, 'USD') |
|
| 117 | - * |
|
| 118 | - * @param float|int|string $amount money amount IN THE STANDARD UNIT FOR THE CURRENCY ie: dollars, Euros, etc |
|
| 119 | - * example: $12.5 USD would equate to a value amount of 12.50 |
|
| 120 | - * @param string $currency_code |
|
| 121 | - * @return Money |
|
| 122 | - * @throws EE_Error |
|
| 123 | - * @throws InvalidArgumentException |
|
| 124 | - * @throws InvalidDataTypeException |
|
| 125 | - * @throws InvalidInterfaceException |
|
| 126 | - */ |
|
| 127 | - public static function forCurrency($amount, $currency_code) |
|
| 128 | - { |
|
| 129 | - return new Money( |
|
| 130 | - $amount, |
|
| 131 | - CreateCurrency::fromCode($currency_code), |
|
| 132 | - CreateMoney::calculator(), |
|
| 133 | - CreateMoney::formatters() |
|
| 134 | - ); |
|
| 135 | - } |
|
| 136 | - |
|
| 137 | - |
|
| 138 | - |
|
| 139 | - |
|
| 140 | - /** |
|
| 141 | - * @return Calculator |
|
| 142 | - */ |
|
| 143 | - public static function calculator() |
|
| 144 | - { |
|
| 145 | - CreateMoney::initializeCalculators(); |
|
| 146 | - return CreateMoney::$calculator; |
|
| 147 | - } |
|
| 148 | - |
|
| 149 | - |
|
| 150 | - |
|
| 151 | - /** |
|
| 152 | - * loops through a filterable array of Calculator services |
|
| 153 | - * and selects the first one that is supported by the current server |
|
| 154 | - */ |
|
| 155 | - protected static function initializeCalculators() |
|
| 156 | - { |
|
| 157 | - if (CreateMoney::$calculator instanceof Calculator) { |
|
| 158 | - return; |
|
| 159 | - } |
|
| 160 | - $calculators = apply_filters( |
|
| 161 | - 'FHEE__EventEspresso\core\services\currency\MoneyFactory__initializeCalculators__Calculator_array', |
|
| 162 | - array( |
|
| 163 | - '\EventEspresso\core\services\currency\DefaultCalculator', |
|
| 164 | - ) |
|
| 165 | - ); |
|
| 166 | - foreach ($calculators as $calculator) { |
|
| 167 | - if (! class_exists($calculator)) { |
|
| 168 | - continue; |
|
| 169 | - } |
|
| 170 | - $calculator = new $calculator(); |
|
| 171 | - if ($calculator instanceof Calculator && $calculator->isSupported()) { |
|
| 172 | - CreateMoney::$calculator = $calculator; |
|
| 173 | - break; |
|
| 174 | - } |
|
| 175 | - } |
|
| 176 | - } |
|
| 177 | - |
|
| 178 | - |
|
| 179 | - |
|
| 180 | - /** |
|
| 181 | - * @return MoneyFormatter[] |
|
| 182 | - */ |
|
| 183 | - public static function formatters() |
|
| 184 | - { |
|
| 185 | - CreateMoney::initializeFormatters(); |
|
| 186 | - return CreateMoney::$formatters; |
|
| 187 | - } |
|
| 188 | - |
|
| 189 | - |
|
| 190 | - |
|
| 191 | - /** |
|
| 192 | - * initializes a filterable array of MoneyFormatter services |
|
| 193 | - */ |
|
| 194 | - protected static function initializeFormatters() |
|
| 195 | - { |
|
| 196 | - if (! empty(CreateMoney::$formatters)) { |
|
| 197 | - return; |
|
| 198 | - } |
|
| 199 | - CreateMoney::$formatters = apply_filters( |
|
| 200 | - 'FHEE__EventEspresso\core\services\currency\MoneyFactory__initializeFormatters__MoneyFormatters_array', |
|
| 201 | - array( |
|
| 202 | - 1 => new DecimalMoneyFormatter(), |
|
| 203 | - 2 => new ThousandsMoneyFormatter(), |
|
| 204 | - 3 => new CurrencySignMoneyFormatter(), |
|
| 205 | - 4 => new CurrencyCodeMoneyFormatter(), |
|
| 206 | - 5 => new InternationalMoneyFormatter(), |
|
| 207 | - ) |
|
| 208 | - ); |
|
| 209 | - } |
|
| 26 | + /** |
|
| 27 | + * @var Calculator $calculator |
|
| 28 | + */ |
|
| 29 | + protected static $calculator; |
|
| 30 | + |
|
| 31 | + /** |
|
| 32 | + * @var MoneyFormatter[] $formatters |
|
| 33 | + */ |
|
| 34 | + protected static $formatters; |
|
| 35 | + |
|
| 36 | + |
|
| 37 | + |
|
| 38 | + /** |
|
| 39 | + * factory method that returns a Money object using amount specified in the currency's subunits |
|
| 40 | + * example: for $12.50 USD use CreateMoney::fromSubUnits(1250, 'USD') |
|
| 41 | + * |
|
| 42 | + * @param int $subunits_amount money amount IN THE SUBUNITS FOR THE CURRENCY ie: cents |
|
| 43 | + * example: $12.50 USD would equate to a subunits amount of 1250 |
|
| 44 | + * @param string $currency_code |
|
| 45 | + * @return Money |
|
| 46 | + * @throws EE_Error |
|
| 47 | + * @throws InvalidArgumentException |
|
| 48 | + * @throws InvalidDataTypeException |
|
| 49 | + * @throws InvalidInterfaceException |
|
| 50 | + */ |
|
| 51 | + public static function fromSubUnits($subunits_amount, $currency_code = '') |
|
| 52 | + { |
|
| 53 | + $currency = CreateCurrency::fromCode($currency_code); |
|
| 54 | + return new Money( |
|
| 55 | + // shift decimal BACK by number of places for currency |
|
| 56 | + $subunits_amount * pow(10, $currency->decimalPlaces() * -1), |
|
| 57 | + $currency, |
|
| 58 | + CreateMoney::calculator(), |
|
| 59 | + CreateMoney::formatters() |
|
| 60 | + ); |
|
| 61 | + } |
|
| 62 | + |
|
| 63 | + |
|
| 64 | + |
|
| 65 | + /** |
|
| 66 | + * factory method that returns a Money object using the currency corresponding to the site's country |
|
| 67 | + * example: CreateMoney::forSite(12.5) |
|
| 68 | + * |
|
| 69 | + * @param float|int|string $amount money amount IN THE STANDARD UNIT FOR THE CURRENCY ie: dollars, Euros, etc |
|
| 70 | + * example: $12.5 USD would equate to a standard amount of 12.50 |
|
| 71 | + * @return Money |
|
| 72 | + * @throws EE_Error |
|
| 73 | + * @throws InvalidArgumentException |
|
| 74 | + * @throws InvalidDataTypeException |
|
| 75 | + * @throws InvalidInterfaceException |
|
| 76 | + */ |
|
| 77 | + public static function forSite($amount) |
|
| 78 | + { |
|
| 79 | + return new Money( |
|
| 80 | + $amount, |
|
| 81 | + CreateCurrency::fromCountryCode(), |
|
| 82 | + CreateMoney::calculator(), |
|
| 83 | + CreateMoney::formatters() |
|
| 84 | + ); |
|
| 85 | + } |
|
| 86 | + |
|
| 87 | + |
|
| 88 | + |
|
| 89 | + /** |
|
| 90 | + * factory method that returns a Money object using the currency as specified by the supplied ISO country code |
|
| 91 | + * example: CreateMoney::forCountry(12.5,'US') |
|
| 92 | + * |
|
| 93 | + * @param float|int|string $amount money amount IN THE STANDARD UNIT FOR THE CURRENCY ie: dollars, Euros, etc |
|
| 94 | + * example: $12.5 USD would equate to a value amount of 12.50 |
|
| 95 | + * @param string $CNT_ISO |
|
| 96 | + * @return Money |
|
| 97 | + * @throws EE_Error |
|
| 98 | + * @throws InvalidArgumentException |
|
| 99 | + * @throws InvalidDataTypeException |
|
| 100 | + * @throws InvalidInterfaceException |
|
| 101 | + */ |
|
| 102 | + public static function forCountry($amount, $CNT_ISO) |
|
| 103 | + { |
|
| 104 | + return new Money( |
|
| 105 | + $amount, |
|
| 106 | + CreateCurrency::fromCountryCode($CNT_ISO), |
|
| 107 | + CreateMoney::calculator(), |
|
| 108 | + CreateMoney::formatters() |
|
| 109 | + ); |
|
| 110 | + } |
|
| 111 | + |
|
| 112 | + |
|
| 113 | + |
|
| 114 | + /** |
|
| 115 | + * factory method that returns a Money object using the currency as specified by the supplied currency code |
|
| 116 | + * example: CreateMoney::forCurrency(12.5, 'USD') |
|
| 117 | + * |
|
| 118 | + * @param float|int|string $amount money amount IN THE STANDARD UNIT FOR THE CURRENCY ie: dollars, Euros, etc |
|
| 119 | + * example: $12.5 USD would equate to a value amount of 12.50 |
|
| 120 | + * @param string $currency_code |
|
| 121 | + * @return Money |
|
| 122 | + * @throws EE_Error |
|
| 123 | + * @throws InvalidArgumentException |
|
| 124 | + * @throws InvalidDataTypeException |
|
| 125 | + * @throws InvalidInterfaceException |
|
| 126 | + */ |
|
| 127 | + public static function forCurrency($amount, $currency_code) |
|
| 128 | + { |
|
| 129 | + return new Money( |
|
| 130 | + $amount, |
|
| 131 | + CreateCurrency::fromCode($currency_code), |
|
| 132 | + CreateMoney::calculator(), |
|
| 133 | + CreateMoney::formatters() |
|
| 134 | + ); |
|
| 135 | + } |
|
| 136 | + |
|
| 137 | + |
|
| 138 | + |
|
| 139 | + |
|
| 140 | + /** |
|
| 141 | + * @return Calculator |
|
| 142 | + */ |
|
| 143 | + public static function calculator() |
|
| 144 | + { |
|
| 145 | + CreateMoney::initializeCalculators(); |
|
| 146 | + return CreateMoney::$calculator; |
|
| 147 | + } |
|
| 148 | + |
|
| 149 | + |
|
| 150 | + |
|
| 151 | + /** |
|
| 152 | + * loops through a filterable array of Calculator services |
|
| 153 | + * and selects the first one that is supported by the current server |
|
| 154 | + */ |
|
| 155 | + protected static function initializeCalculators() |
|
| 156 | + { |
|
| 157 | + if (CreateMoney::$calculator instanceof Calculator) { |
|
| 158 | + return; |
|
| 159 | + } |
|
| 160 | + $calculators = apply_filters( |
|
| 161 | + 'FHEE__EventEspresso\core\services\currency\MoneyFactory__initializeCalculators__Calculator_array', |
|
| 162 | + array( |
|
| 163 | + '\EventEspresso\core\services\currency\DefaultCalculator', |
|
| 164 | + ) |
|
| 165 | + ); |
|
| 166 | + foreach ($calculators as $calculator) { |
|
| 167 | + if (! class_exists($calculator)) { |
|
| 168 | + continue; |
|
| 169 | + } |
|
| 170 | + $calculator = new $calculator(); |
|
| 171 | + if ($calculator instanceof Calculator && $calculator->isSupported()) { |
|
| 172 | + CreateMoney::$calculator = $calculator; |
|
| 173 | + break; |
|
| 174 | + } |
|
| 175 | + } |
|
| 176 | + } |
|
| 177 | + |
|
| 178 | + |
|
| 179 | + |
|
| 180 | + /** |
|
| 181 | + * @return MoneyFormatter[] |
|
| 182 | + */ |
|
| 183 | + public static function formatters() |
|
| 184 | + { |
|
| 185 | + CreateMoney::initializeFormatters(); |
|
| 186 | + return CreateMoney::$formatters; |
|
| 187 | + } |
|
| 188 | + |
|
| 189 | + |
|
| 190 | + |
|
| 191 | + /** |
|
| 192 | + * initializes a filterable array of MoneyFormatter services |
|
| 193 | + */ |
|
| 194 | + protected static function initializeFormatters() |
|
| 195 | + { |
|
| 196 | + if (! empty(CreateMoney::$formatters)) { |
|
| 197 | + return; |
|
| 198 | + } |
|
| 199 | + CreateMoney::$formatters = apply_filters( |
|
| 200 | + 'FHEE__EventEspresso\core\services\currency\MoneyFactory__initializeFormatters__MoneyFormatters_array', |
|
| 201 | + array( |
|
| 202 | + 1 => new DecimalMoneyFormatter(), |
|
| 203 | + 2 => new ThousandsMoneyFormatter(), |
|
| 204 | + 3 => new CurrencySignMoneyFormatter(), |
|
| 205 | + 4 => new CurrencyCodeMoneyFormatter(), |
|
| 206 | + 5 => new InternationalMoneyFormatter(), |
|
| 207 | + ) |
|
| 208 | + ); |
|
| 209 | + } |
|
| 210 | 210 | |
| 211 | 211 | |
| 212 | 212 | } |
@@ -164,7 +164,7 @@ discard block |
||
| 164 | 164 | ) |
| 165 | 165 | ); |
| 166 | 166 | foreach ($calculators as $calculator) { |
| 167 | - if (! class_exists($calculator)) { |
|
| 167 | + if ( ! class_exists($calculator)) { |
|
| 168 | 168 | continue; |
| 169 | 169 | } |
| 170 | 170 | $calculator = new $calculator(); |
@@ -193,7 +193,7 @@ discard block |
||
| 193 | 193 | */ |
| 194 | 194 | protected static function initializeFormatters() |
| 195 | 195 | { |
| 196 | - if (! empty(CreateMoney::$formatters)) { |
|
| 196 | + if ( ! empty(CreateMoney::$formatters)) { |
|
| 197 | 197 | return; |
| 198 | 198 | } |
| 199 | 199 | CreateMoney::$formatters = apply_filters( |
@@ -21,20 +21,20 @@ |
||
| 21 | 21 | |
| 22 | 22 | |
| 23 | 23 | |
| 24 | - /** |
|
| 25 | - * @param string $money_amount |
|
| 26 | - * @param Currency $currency |
|
| 27 | - * @return string |
|
| 28 | - */ |
|
| 29 | - public function format($money_amount, Currency $currency) |
|
| 30 | - { |
|
| 31 | - // add currency sign |
|
| 32 | - if ($currency->signB4()) { |
|
| 33 | - if ($money_amount >= 0) { |
|
| 34 | - return $currency->sign() . $money_amount; |
|
| 35 | - } |
|
| 36 | - return '-' . $currency->sign() . str_replace('-', '', $money_amount); |
|
| 37 | - } |
|
| 38 | - return $money_amount . $currency->sign(); |
|
| 39 | - } |
|
| 24 | + /** |
|
| 25 | + * @param string $money_amount |
|
| 26 | + * @param Currency $currency |
|
| 27 | + * @return string |
|
| 28 | + */ |
|
| 29 | + public function format($money_amount, Currency $currency) |
|
| 30 | + { |
|
| 31 | + // add currency sign |
|
| 32 | + if ($currency->signB4()) { |
|
| 33 | + if ($money_amount >= 0) { |
|
| 34 | + return $currency->sign() . $money_amount; |
|
| 35 | + } |
|
| 36 | + return '-' . $currency->sign() . str_replace('-', '', $money_amount); |
|
| 37 | + } |
|
| 38 | + return $money_amount . $currency->sign(); |
|
| 39 | + } |
|
| 40 | 40 | } |
@@ -31,10 +31,10 @@ |
||
| 31 | 31 | // add currency sign |
| 32 | 32 | if ($currency->signB4()) { |
| 33 | 33 | if ($money_amount >= 0) { |
| 34 | - return $currency->sign() . $money_amount; |
|
| 34 | + return $currency->sign().$money_amount; |
|
| 35 | 35 | } |
| 36 | - return '-' . $currency->sign() . str_replace('-', '', $money_amount); |
|
| 36 | + return '-'.$currency->sign().str_replace('-', '', $money_amount); |
|
| 37 | 37 | } |
| 38 | - return $money_amount . $currency->sign(); |
|
| 38 | + return $money_amount.$currency->sign(); |
|
| 39 | 39 | } |
| 40 | 40 | } |
@@ -19,182 +19,182 @@ |
||
| 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 | - * |
|
| 52 | - * @var int $decimal_places |
|
| 53 | - */ |
|
| 54 | - private $decimal_places; |
|
| 55 | - |
|
| 56 | - /** |
|
| 57 | - * Symbol to use for decimal mark |
|
| 58 | - * |
|
| 59 | - * @var string $decimal_mark |
|
| 60 | - * eg '.' |
|
| 61 | - */ |
|
| 62 | - private $decimal_mark; |
|
| 63 | - |
|
| 64 | - /** |
|
| 65 | - * Symbol to use for thousands |
|
| 66 | - * |
|
| 67 | - * @var string $thousands |
|
| 68 | - * eg ',' |
|
| 69 | - */ |
|
| 70 | - private $thousands; |
|
| 71 | - |
|
| 72 | - |
|
| 73 | - |
|
| 74 | - /** |
|
| 75 | - * Currency constructor. |
|
| 76 | - * |
|
| 77 | - * @param string $code |
|
| 78 | - * @param Label $label |
|
| 79 | - * @param string $sign |
|
| 80 | - * @param bool $sign_b4 |
|
| 81 | - * @param int $decimal_places |
|
| 82 | - * @param string $decimal_mark |
|
| 83 | - * @param string $thousands |
|
| 84 | - */ |
|
| 85 | - public function __construct($code, Label $label, $sign, $sign_b4, $decimal_places, $decimal_mark, $thousands) |
|
| 86 | - { |
|
| 87 | - $this->code = $code; |
|
| 88 | - $this->label = $label; |
|
| 89 | - $this->sign = $sign; |
|
| 90 | - $this->sign_b4 = $sign_b4; |
|
| 91 | - $this->decimal_places = $decimal_places; |
|
| 92 | - $this->decimal_mark = $decimal_mark; |
|
| 93 | - $this->thousands = $thousands; |
|
| 94 | - } |
|
| 95 | - |
|
| 96 | - |
|
| 97 | - |
|
| 98 | - /** |
|
| 99 | - * returns true if this currency is the same as the supplied currency |
|
| 100 | - * |
|
| 101 | - * @param Currency $other |
|
| 102 | - * @return bool |
|
| 103 | - */ |
|
| 104 | - public function equals(Currency $other) |
|
| 105 | - { |
|
| 106 | - return $this->code() === $other->code(); |
|
| 107 | - } |
|
| 108 | - |
|
| 109 | - |
|
| 110 | - |
|
| 111 | - /** |
|
| 112 | - * @return string |
|
| 113 | - */ |
|
| 114 | - public function code() |
|
| 115 | - { |
|
| 116 | - return $this->code; |
|
| 117 | - } |
|
| 118 | - |
|
| 119 | - |
|
| 120 | - |
|
| 121 | - /** |
|
| 122 | - * @return string |
|
| 123 | - */ |
|
| 124 | - public function name() |
|
| 125 | - { |
|
| 126 | - return $this->label->singular(); |
|
| 127 | - } |
|
| 128 | - |
|
| 129 | - |
|
| 130 | - |
|
| 131 | - /** |
|
| 132 | - * @return string |
|
| 133 | - */ |
|
| 134 | - public function plural() |
|
| 135 | - { |
|
| 136 | - return $this->label->plural(); |
|
| 137 | - } |
|
| 138 | - |
|
| 139 | - |
|
| 140 | - |
|
| 141 | - /** |
|
| 142 | - * @return string |
|
| 143 | - */ |
|
| 144 | - public function sign() |
|
| 145 | - { |
|
| 146 | - return $this->sign; |
|
| 147 | - } |
|
| 148 | - |
|
| 149 | - |
|
| 150 | - |
|
| 151 | - /** |
|
| 152 | - * @return bool |
|
| 153 | - */ |
|
| 154 | - public function signB4() |
|
| 155 | - { |
|
| 156 | - return $this->sign_b4; |
|
| 157 | - } |
|
| 158 | - |
|
| 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 | + * |
|
| 52 | + * @var int $decimal_places |
|
| 53 | + */ |
|
| 54 | + private $decimal_places; |
|
| 55 | + |
|
| 56 | + /** |
|
| 57 | + * Symbol to use for decimal mark |
|
| 58 | + * |
|
| 59 | + * @var string $decimal_mark |
|
| 60 | + * eg '.' |
|
| 61 | + */ |
|
| 62 | + private $decimal_mark; |
|
| 63 | + |
|
| 64 | + /** |
|
| 65 | + * Symbol to use for thousands |
|
| 66 | + * |
|
| 67 | + * @var string $thousands |
|
| 68 | + * eg ',' |
|
| 69 | + */ |
|
| 70 | + private $thousands; |
|
| 71 | + |
|
| 72 | + |
|
| 73 | + |
|
| 74 | + /** |
|
| 75 | + * Currency constructor. |
|
| 76 | + * |
|
| 77 | + * @param string $code |
|
| 78 | + * @param Label $label |
|
| 79 | + * @param string $sign |
|
| 80 | + * @param bool $sign_b4 |
|
| 81 | + * @param int $decimal_places |
|
| 82 | + * @param string $decimal_mark |
|
| 83 | + * @param string $thousands |
|
| 84 | + */ |
|
| 85 | + public function __construct($code, Label $label, $sign, $sign_b4, $decimal_places, $decimal_mark, $thousands) |
|
| 86 | + { |
|
| 87 | + $this->code = $code; |
|
| 88 | + $this->label = $label; |
|
| 89 | + $this->sign = $sign; |
|
| 90 | + $this->sign_b4 = $sign_b4; |
|
| 91 | + $this->decimal_places = $decimal_places; |
|
| 92 | + $this->decimal_mark = $decimal_mark; |
|
| 93 | + $this->thousands = $thousands; |
|
| 94 | + } |
|
| 95 | + |
|
| 96 | + |
|
| 97 | + |
|
| 98 | + /** |
|
| 99 | + * returns true if this currency is the same as the supplied currency |
|
| 100 | + * |
|
| 101 | + * @param Currency $other |
|
| 102 | + * @return bool |
|
| 103 | + */ |
|
| 104 | + public function equals(Currency $other) |
|
| 105 | + { |
|
| 106 | + return $this->code() === $other->code(); |
|
| 107 | + } |
|
| 108 | + |
|
| 109 | + |
|
| 110 | + |
|
| 111 | + /** |
|
| 112 | + * @return string |
|
| 113 | + */ |
|
| 114 | + public function code() |
|
| 115 | + { |
|
| 116 | + return $this->code; |
|
| 117 | + } |
|
| 118 | + |
|
| 119 | + |
|
| 120 | + |
|
| 121 | + /** |
|
| 122 | + * @return string |
|
| 123 | + */ |
|
| 124 | + public function name() |
|
| 125 | + { |
|
| 126 | + return $this->label->singular(); |
|
| 127 | + } |
|
| 128 | + |
|
| 129 | + |
|
| 130 | + |
|
| 131 | + /** |
|
| 132 | + * @return string |
|
| 133 | + */ |
|
| 134 | + public function plural() |
|
| 135 | + { |
|
| 136 | + return $this->label->plural(); |
|
| 137 | + } |
|
| 138 | + |
|
| 139 | + |
|
| 140 | + |
|
| 141 | + /** |
|
| 142 | + * @return string |
|
| 143 | + */ |
|
| 144 | + public function sign() |
|
| 145 | + { |
|
| 146 | + return $this->sign; |
|
| 147 | + } |
|
| 148 | + |
|
| 149 | + |
|
| 150 | + |
|
| 151 | + /** |
|
| 152 | + * @return bool |
|
| 153 | + */ |
|
| 154 | + public function signB4() |
|
| 155 | + { |
|
| 156 | + return $this->sign_b4; |
|
| 157 | + } |
|
| 158 | + |
|
| 159 | 159 | |
| 160 | 160 | |
| 161 | - /** |
|
| 162 | - * @return int |
|
| 163 | - */ |
|
| 164 | - public function decimalPlaces() |
|
| 165 | - { |
|
| 166 | - return $this->decimal_places; |
|
| 167 | - } |
|
| 161 | + /** |
|
| 162 | + * @return int |
|
| 163 | + */ |
|
| 164 | + public function decimalPlaces() |
|
| 165 | + { |
|
| 166 | + return $this->decimal_places; |
|
| 167 | + } |
|
| 168 | 168 | |
| 169 | 169 | |
| 170 | 170 | |
| 171 | - /** |
|
| 172 | - * @return string |
|
| 173 | - */ |
|
| 174 | - public function decimalMark() |
|
| 175 | - { |
|
| 176 | - return $this->decimal_mark; |
|
| 177 | - } |
|
| 171 | + /** |
|
| 172 | + * @return string |
|
| 173 | + */ |
|
| 174 | + public function decimalMark() |
|
| 175 | + { |
|
| 176 | + return $this->decimal_mark; |
|
| 177 | + } |
|
| 178 | 178 | |
| 179 | 179 | |
| 180 | 180 | |
| 181 | - /** |
|
| 182 | - * @return string |
|
| 183 | - */ |
|
| 184 | - public function thousands() |
|
| 185 | - { |
|
| 186 | - return $this->thousands; |
|
| 187 | - } |
|
| 181 | + /** |
|
| 182 | + * @return string |
|
| 183 | + */ |
|
| 184 | + public function thousands() |
|
| 185 | + { |
|
| 186 | + return $this->thousands; |
|
| 187 | + } |
|
| 188 | 188 | |
| 189 | 189 | |
| 190 | 190 | |
| 191 | - /** |
|
| 192 | - * @return string |
|
| 193 | - */ |
|
| 194 | - public function __toString() |
|
| 195 | - { |
|
| 196 | - return $this->code(); |
|
| 197 | - } |
|
| 191 | + /** |
|
| 192 | + * @return string |
|
| 193 | + */ |
|
| 194 | + public function __toString() |
|
| 195 | + { |
|
| 196 | + return $this->code(); |
|
| 197 | + } |
|
| 198 | 198 | |
| 199 | 199 | |
| 200 | 200 | |