mirror of
https://github.com/woocommerce/woocommerce-paypal-payments.git
synced 2025-09-06 13:44:42 +08:00
Refactor money value formatting
Do not duplicate code and send string value for JPY
This commit is contained in:
parent
67cdf9be8e
commit
82adf1933f
5 changed files with 84 additions and 18 deletions
|
@ -64,6 +64,15 @@ class Amount {
|
|||
return $this->money->value();
|
||||
}
|
||||
|
||||
/**
|
||||
* The value formatted as string for API requests.
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public function value_str(): string {
|
||||
return $this->money->value_str();
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the breakdown.
|
||||
*
|
||||
|
@ -79,12 +88,7 @@ class Amount {
|
|||
* @return array
|
||||
*/
|
||||
public function to_array(): array {
|
||||
$amount = array(
|
||||
'currency_code' => $this->currency_code(),
|
||||
'value' => in_array( $this->currency_code(), $this->currencies_without_decimals, true )
|
||||
? round( $this->value(), 0 )
|
||||
: number_format( $this->value(), 2, '.', '' ),
|
||||
);
|
||||
$amount = $this->money->to_array();
|
||||
if ( $this->breakdown() && count( $this->breakdown()->to_array() ) ) {
|
||||
$amount['breakdown'] = $this->breakdown()->to_array();
|
||||
}
|
||||
|
|
|
@ -9,6 +9,8 @@ declare(strict_types=1);
|
|||
|
||||
namespace WooCommerce\PayPalCommerce\ApiClient\Entity;
|
||||
|
||||
use WooCommerce\PayPalCommerce\ApiClient\Helper\MoneyFormatter;
|
||||
|
||||
/**
|
||||
* Class Money
|
||||
*/
|
||||
|
@ -29,11 +31,11 @@ class Money {
|
|||
private $value;
|
||||
|
||||
/**
|
||||
* Currencies that does not support decimals.
|
||||
* The MoneyFormatter.
|
||||
*
|
||||
* @var array
|
||||
* @var MoneyFormatter
|
||||
*/
|
||||
private $currencies_without_decimals = array( 'HUF', 'JPY', 'TWD' );
|
||||
private $money_formatter;
|
||||
|
||||
/**
|
||||
* Money constructor.
|
||||
|
@ -44,6 +46,8 @@ class Money {
|
|||
public function __construct( float $value, string $currency_code ) {
|
||||
$this->value = $value;
|
||||
$this->currency_code = $currency_code;
|
||||
|
||||
$this->money_formatter = new MoneyFormatter();
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -55,6 +59,15 @@ class Money {
|
|||
return $this->value;
|
||||
}
|
||||
|
||||
/**
|
||||
* The value formatted as string for API requests.
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public function value_str(): string {
|
||||
return $this->money_formatter->format( $this->value, $this->currency_code );
|
||||
}
|
||||
|
||||
/**
|
||||
* The currency code.
|
||||
*
|
||||
|
@ -72,9 +85,7 @@ class Money {
|
|||
public function to_array(): array {
|
||||
return array(
|
||||
'currency_code' => $this->currency_code(),
|
||||
'value' => in_array( $this->currency_code(), $this->currencies_without_decimals, true )
|
||||
? round( $this->value(), 0 )
|
||||
: number_format( $this->value(), 2, '.', '' ),
|
||||
'value' => $this->value_str(),
|
||||
);
|
||||
}
|
||||
}
|
||||
|
|
36
modules/ppcp-api-client/src/Helper/MoneyFormatter.php
Normal file
36
modules/ppcp-api-client/src/Helper/MoneyFormatter.php
Normal file
|
@ -0,0 +1,36 @@
|
|||
<?php
|
||||
/**
|
||||
* Class MoneyFormatter.
|
||||
*
|
||||
* @package WooCommerce\PayPalCommerce\ApiClient\Entity
|
||||
*/
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace WooCommerce\PayPalCommerce\ApiClient\Helper;
|
||||
|
||||
/**
|
||||
* Class MoneyFormatter
|
||||
*/
|
||||
class MoneyFormatter {
|
||||
/**
|
||||
* Currencies that does not support decimals.
|
||||
*
|
||||
* @var array
|
||||
*/
|
||||
private $currencies_without_decimals = array( 'HUF', 'JPY', 'TWD' );
|
||||
|
||||
/**
|
||||
* Returns the value formatted as string for API requests.
|
||||
*
|
||||
* @param float $value The value.
|
||||
* @param string $currency The 3-letter currency code.
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public function format( float $value, string $currency ): string {
|
||||
return in_array( $currency, $this->currencies_without_decimals, true )
|
||||
? (string) round( $value, 0 )
|
||||
: number_format( $value, 2, '.', '' );
|
||||
}
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue