-
-
Save MaxSem/b3014252d56dc2b7bf7653633f0e9fb5 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
class CurrencyConverter { | |
private function connect_db($sql){ | |
$conn = mysqli_connect('host', 'user', 'password'); | |
mysqli_select_db($conn, 'database'); | |
$res=mysqli_query($sql, $conn); | |
mysqli_close($conn); | |
return $res; | |
} | |
public function get_rate($currency) { | |
$res = $this->connect_db("SELECT rate FROM rates WHERE currency = '$currency'"); | |
$row = mysqli_fetch_array($res); | |
return $row['rate']; | |
} | |
public function parse($input) { | |
$input = trim($input); | |
$pos = strpos($input, ' '); | |
if( $pos != strrpos($input, ' ')){ | |
return [ 'error' => 'true', 'msg' => 'invalid input: '.$input ]; | |
} | |
return [ 'error' => 'false', 'currency' => substr($input,0,$pos), 'amount' => substr($input,$pos+1) ]; | |
} | |
public function set_rate($currency, $rate) { | |
$currency = strtoupper($currency); | |
$res = $this->connect_db("SELECT rate FROM rates WHERE currency = '$currency'"); | |
$row = mysqli_fetch_array($res); | |
if($row['rate']){ | |
$this->connect_db("UPDATE rates SET rate=$rate WHERE currency='$currency'"); | |
}else{ | |
$this->connect_db("INSERT INTO rates (currency,rate) VALUES ('$currency',$rate)"); | |
} | |
} | |
public function convert_to_usd($input) { | |
$result = []; | |
if(gettype($input) == 'string'){ | |
$input = array( $input ); | |
} | |
if(gettype($input) != 'array'){ | |
return [ 'error' => 'true', 'msg' => 'invalid input' ]; | |
}else{ | |
foreach( $input as $item){ | |
$item_parsed = $this->parse($item); | |
$rate = $this->get_rate($item_parsed['currency']); | |
$usd = $rate * $item_parsed['amount']; | |
array_push($result,'USD '.$usd); | |
} | |
} | |
return $result; | |
} | |
public function convert_from_usd($currency, $amount) { | |
function walk_amount (&$item, $key, $rate){ | |
$item = number_format($item / $rate, 2, '.', ''); | |
} | |
if(!$currency || !$amount){ | |
$return = [ 'error' => 'true', 'msg' => 'invalid input argument' ]; | |
}else{ | |
$rate = $this->get_rate($currency); | |
$amounts = explode(',', $amount); | |
array_walk($amounts,'walk_amount', $rate); | |
$return = [ 'error' => 'false', 'content' => implode(',',$amounts)]; | |
} | |
return '{"error":'.$return['error'].',"content":"'.$return['content'].'"}'; | |
} | |
} | |
$converter = new CurrencyConverter(); | |
echo $converter->convert_from_usd($_GET['currency'], $_GET['amount']); | |
//for testing each function: | |
//echo $converter->get_rate($_GET['currency']); | |
//echo $converter->set_rate($_GET['currency'],$_GET['rate']); | |
//var_dump($converter->parse($_GET['input'])); | |
//var_dump($converter->convert_to_usd('JPY 10000')); | |
//echo '<br />'; | |
//var_dump($converter->convert_to_usd(array('JPY 10000','AUD 2000','CZK 4000'))); | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment