Skip to content

Commit

Permalink
Update
Browse files Browse the repository at this point in the history
  • Loading branch information
kamshory committed Aug 30, 2024
1 parent 92ed061 commit 8007f21
Show file tree
Hide file tree
Showing 5 changed files with 184 additions and 0 deletions.
17 changes: 17 additions & 0 deletions composer.json
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": {}
}
18 changes: 18 additions & 0 deletions composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Binary file added composer.phar
Binary file not shown.
110 changes: 110 additions & 0 deletions src/Forex/Forex.php
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;
}
}
39 changes: 39 additions & 0 deletions tests/currency.php
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

0 comments on commit 8007f21

Please sign in to comment.