woocommerce-paypal-payments/modules/ppcp-api-client/src/Entity/Money.php
Anton Ukhanev d4c8282518 Use Composer modules and convert modules to PSR-4
PSR-4 is much more robust and predictable. But to do this,
a source root dir must be specified for every module.
This could be done in the root file, but this is not very modular.
Instead, now every module declares its own source root by using
the amazing Composer Merge Plugin. This approach allows each module
to also declare its own dependencies. Together, these changes allow
modules to be easily extractable to separate pacakges when the need
arises, and in general improves modularity significantly.
2021-08-26 11:01:20 +02:00

80 lines
1.3 KiB
PHP

<?php
/**
* The money object.
*
* @package WooCommerce\PayPalCommerce\ApiClient\Entity
*/
declare(strict_types=1);
namespace WooCommerce\PayPalCommerce\ApiClient\Entity;
/**
* Class Money
*/
class Money {
/**
* The currency code.
*
* @var string
*/
private $currency_code;
/**
* The value.
*
* @var float
*/
private $value;
/**
* Currencies that does not support decimals.
*
* @var array
*/
private $currencies_without_decimals = array( 'HUF', 'JPY', 'TWD' );
/**
* Money constructor.
*
* @param float $value The value.
* @param string $currency_code The currency code.
*/
public function __construct( float $value, string $currency_code ) {
$this->value = $value;
$this->currency_code = $currency_code;
}
/**
* The value.
*
* @return float
*/
public function value(): float {
return $this->value;
}
/**
* The currency code.
*
* @return string
*/
public function currency_code(): string {
return $this->currency_code;
}
/**
* Returns the object as array.
*
* @return array
*/
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, '.', '' ),
);
}
}