1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110
<?php
namespace SIW\Data;
use SIW\External\Exchange_Rates;
class Currency {
protected $iso_code;
protected $name;
protected $symbol;
public function __construct( array $currency ) {
$defaults = [
'iso' => '',
'name' => '',
'symbol' => '',
];
$currency = wp_parse_args( $currency, $defaults );
$this->iso_code = $currency['iso'];
$this->name = $currency['name'];
$this->symbol = $currency['symbol'];
}
public function get_iso_code() {
return $this->iso_code;
}
public function get_name() {
return $this->name;
}
public function get_symbol() {
return $this->symbol;
}
public function get_exchange_rate() {
$external_exchange_rates = new Exchange_Rates();
$exchange_rates = $external_exchange_rates->get_rates();
$exchange_rate = $exchange_rates[ $this->iso_code ] ?? false;
return $exchange_rate;
}
public function convert_to_euro( float $amount, int $decimals = 0 ) {
$exchange_rate = $this->get_exchange_rate();
if ( false == $exchange_rate ) {
return false;
}
$amount_in_euro = (float) $amount * (float) $exchange_rate;
$amount_in_euro = number_format_i18n( $amount_in_euro, $decimals );
return $amount_in_euro;
}
}