50 * @assert(1, "USD", "JPY") < 130 */ public function convert($src, $from, $to) { $rate = $this->getRate($from, $to); if (!$rate) { throw new \RuntimeException('rate info not found'); } return $src * $rate; } /** * get exchange rates * * @assert("USD", "JPY") > 50 * @assert("USD", "JPY") < 130 */ public function getRate($from, $to) { $rate = $this->doGetRate($from, $to); return $rate; } /** * get exchange rates from fixer.io * * @assert("USD", "JPY") > 50 * @assert("USD", "JPY") < 130 */ public function doGetRate($from, $to) { if (isset($this->_cached[$from][$to])) { return $this->_cached[$from][$to]; } $url = "https://ratesapi.io/api/latest?base=$from&symbols=$to"; // local phpunit test may fail by not setting up cert properly. pass it only when test $options = []; if (defined('INSIDE_PHPUNIT_TEST')) { $options['ssl']['verify_peer']=false; $options['ssl']['verify_peer_name']=false; } if (array_key_exists('HTTP_PROXY', $_SERVER)) { $options['http'] = array( "proxy" => $_SERVER['HTTP_PROXY'], 'request_fulluri' => true, ); } $json = @file_get_contents($url, false, stream_context_create($options)); if ($json === false) { // dummy rate when API is down or unavailable if (strncmp(strtoupper($from), 'JPY', 3) === 0) { $this->_cached[$from][$to] = 0.00909; return 0.00909; } else { $this->_cached[$from][$to] = 110; return 110; } } else { $decoded = json_decode($json, true); $rate = $decoded['rates'][strtoupper($to)]; $this->_cached[$from][$to] = $rate; return $rate; } } }