-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
184 additions
and
0 deletions.
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
{ | ||
"name": "planetbiru/forex", | ||
"description": "Foreign Exchange", | ||
"license": "MIT", | ||
"autoload": { | ||
"psr-4": { | ||
"Planetbiru\\": "src/" | ||
} | ||
}, | ||
"authors": [ | ||
{ | ||
"name": "Kamshory, MT", | ||
"email": "kamshory@yahoo.com" | ||
} | ||
], | ||
"require": {} | ||
} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Binary file not shown.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,110 @@ | ||
<?php | ||
|
||
namespace Planetbiru\Forex; | ||
|
||
class Forex | ||
{ | ||
/** | ||
* Currency rates | ||
* | ||
* @var array | ||
*/ | ||
private $rates = array(); | ||
|
||
/** | ||
* Daily rate URL | ||
* | ||
* @var string | ||
*/ | ||
private $rateDaily = 'http://www.ecb.europa.eu/stats/eurofxref/eurofxref-daily.xml'; | ||
|
||
/** | ||
* Constructor | ||
* | ||
* @param string $url | ||
*/ | ||
public function __construct($url = null) | ||
{ | ||
if(!isset($url)) | ||
{ | ||
$url = $this->rateDaily; | ||
} | ||
$this->rates = array(); | ||
if($url != null) | ||
{ | ||
$this->load($url); | ||
} | ||
} | ||
|
||
/** | ||
* Load currency rate | ||
* | ||
* @param string $url | ||
* @return self | ||
*/ | ||
public function load($url) | ||
{ | ||
$content = file_get_contents($url); | ||
$xml = simplexml_load_string($content, "SimpleXMLElement", LIBXML_NOCDATA ); | ||
foreach($xml->Cube->Cube->Cube as $rate) | ||
{ | ||
$key = (string) $rate['currency']; | ||
$value = floatval($rate['rate']); | ||
$this->set($key, $value); | ||
} | ||
return $this; | ||
} | ||
|
||
/** | ||
* Cet currency rate | ||
* | ||
* @param string $currency | ||
* @param float $rate | ||
* @return self | ||
*/ | ||
public function set($currency, $rate) | ||
{ | ||
$this->rates[$currency] = $rate; | ||
return $this; | ||
} | ||
|
||
/** | ||
* Convert currency value | ||
* | ||
* @param string $from | ||
* @param string $to | ||
* @return float | ||
*/ | ||
public function convert($from, $to) | ||
{ | ||
$to = str_replace('.', '.', $this->rates[$to]) * 1; | ||
$from = str_replace('.', '.', $this->rates[$from]) * 1; | ||
return $to/$from; | ||
} | ||
|
||
|
||
|
||
/** | ||
* Get currency rates | ||
* | ||
* @return array | ||
*/ | ||
public function getRates() | ||
{ | ||
return $this->rates; | ||
} | ||
|
||
/** | ||
* Set currency rates | ||
* | ||
* @param array $rates Currency rates | ||
* | ||
* @return self | ||
*/ | ||
public function setRates($rates) | ||
{ | ||
$this->rates = $rates; | ||
|
||
return $this; | ||
} | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
<?php | ||
|
||
use Planetbiru\Forex\Forex; | ||
|
||
require_once dirname(__DIR__)."/vendor/autoload.php"; | ||
|
||
$forex = new Forex(); | ||
|
||
?> | ||
<style> | ||
table{ | ||
border-collapse: collapse; | ||
} | ||
td{ | ||
padding: 4px 8px; | ||
} | ||
</style> | ||
<table border="1" style=""> | ||
<thead> | ||
<td>From</td> | ||
<td>To</td> | ||
</tr> | ||
</thead> | ||
<tbody> | ||
<?php | ||
foreach($forex->getRates() as $to=>$rate) | ||
{ | ||
?> | ||
<tr> | ||
<td>1 <?php echo $to;?></td> | ||
<td><?php echo number_format($forex->convert($to, 'IDR'), 2, ".", "");?> IDR</td> | ||
</tr> | ||
<?php | ||
} | ||
?> | ||
</tbody> | ||
</table> | ||
<?php | ||
|