diff --git a/composer.json b/composer.json new file mode 100644 index 0000000..107740b --- /dev/null +++ b/composer.json @@ -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": {} +} diff --git a/composer.lock b/composer.lock new file mode 100644 index 0000000..e5408fb --- /dev/null +++ b/composer.lock @@ -0,0 +1,18 @@ +{ + "_readme": [ + "This file locks the dependencies of your project to a known state", + "Read more about it at https://getcomposer.org/doc/01-basic-usage.md#installing-dependencies", + "This file is @generated automatically" + ], + "content-hash": "5be9baa639ec484883f36ac5b9e4645d", + "packages": [], + "packages-dev": [], + "aliases": [], + "minimum-stability": "stable", + "stability-flags": [], + "prefer-stable": false, + "prefer-lowest": false, + "platform": [], + "platform-dev": [], + "plugin-api-version": "2.6.0" +} diff --git a/composer.phar b/composer.phar new file mode 100644 index 0000000..bdde5f5 Binary files /dev/null and b/composer.phar differ diff --git a/src/Forex/Forex.php b/src/Forex/Forex.php new file mode 100644 index 0000000..a9dcfb1 --- /dev/null +++ b/src/Forex/Forex.php @@ -0,0 +1,110 @@ +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; + } +} diff --git a/tests/currency.php b/tests/currency.php new file mode 100644 index 0000000..a37658b --- /dev/null +++ b/tests/currency.php @@ -0,0 +1,39 @@ + + +
From | +To | + + + +getRates() as $to=>$rate) +{ + ?> +
1 | +convert($to, 'IDR'), 2, ".", "");?> IDR | +