Permalink
Browse files

first commit

  • Loading branch information...
0 parents commit 37b7c1af6a98883f14418ab24bed370736519aee @NARKOZ committed Aug 20, 2010
Showing with 169 additions and 0 deletions.
  1. +59 −0 README.markdown
  2. +46 −0 classes/controller/currency.php
  3. +52 −0 classes/model/currency.php
  4. +12 −0 init.php
@@ -0,0 +1,59 @@
+# AZN Rates module for Kohana 3 (KO3) PHP Framework
+
+An open source module to fetch and format AZN exchange rates from www.cbr.ru (The Central Bank of the Russian Federation) for [Kohana Framework](http://kohanaphp.com/) (KohanaPHP v3).
+Kohana AZNrates is licensed under the [New BSD License](http://creativecommons.org/licenses/BSD/).
+
+## Quick Start
+
+### Download and install
+Download the module from [downloads section](http://github.com/NARKOZ/kohana-aznrates/downloads) and place into `modules` folder.
+
+### Download and install using Git
+First, add the submodule to your Git application:
+
+ git submodule add git://github.com/NARKOZ/kohana-aznrates.git modules/aznrates
+ git submodule update --init
+
+Or clone the the module separately:
+
+ cd modules
+ git clone git://github.com/NARKOZ/kohana-aznrates.git aznrates
+
+### Update module using Git
+
+ cd modules/aznrates
+ git submodule update --init
+
+### Modify the Config File
+
+Add to `modules` setting in the `application/bootstrap.php` this line of code:
+
+ Kohana::modules(array(
+ ...
+ 'aznrates' => MODPATH.'aznrates',
+ ...
+ ));
+
+### See a result
+Go to `/aznxrates` or `/aznxrates/date` (date formatted as dd.mm.yyyy, for example: 14.06.2010).
+
+## Sample Output
+<pre>
+Array
+(
+ [AZN] => 1
+ [RUR] => 0.0376
+ [USD] => 1.4727
+ [EUR] => 1.7426
+ [AUD] => 1.7426
+ [JPY] => 1.7426
+ [GBP] => 1.7426
+ [BYR] => 1.7426
+ [LVL] => 1.7426
+ [TRY] => 1.7426
+ [UAH] => 1.7426
+ [EEK] => 1.7426
+)
+</pre>
+
+You can add more currency valutes in the `classes/model/currency.php` file.
@@ -0,0 +1,46 @@
+<?php defined('SYSPATH') OR die('No direct access.');
+/**
+ * Currency aznrates controller.
+ *
+ * @author Nihad Abbasov <mail@narkoz.me>
+ * @copyright Copyright (c) 2010, Nihad Abbasov
+ * @license http://creativecommons.org/licenses/BSD/ New BSD License
+ */
+class Controller_Currency extends Controller {
+
+ /**
+ * Gets currency data for specified date
+ *
+ * @return void
+ */
+ public function action_index($date)
+ {
+ $date = !empty($date) ? $date : date('d.m.Y');
+
+ $currency = $this->_get_currency_model();
+ $currency_data = $currency->get_currency_from_server($date);
+
+ print_r($currency_data);
+ }
+
+ /**
+ * Outputs the currency data
+ *
+ * @return object
+ * @access protected
+ */
+ protected function _get_currency_model()
+ {
+ if ($this->currency === NULL)
+ {
+ $this->currency = new Model_Currency;
+ }
+
+ return $this->currency;
+ }
+
+ protected $currency;
+
+} // End Currency aznrates Controller
+
+?>
@@ -0,0 +1,52 @@
+<?php defined('SYSPATH') OR die('No direct access.');
+/**
+ * Currency aznrates model.
+ *
+ * @author Nihad Abbasov <mail@narkoz.me>
+ * @copyright Copyright (c) 2010, Nihad Abbasov
+ * @license http://creativecommons.org/licenses/BSD/ New BSD License
+ */
+class Model_Currency extends Model {
+
+ /**
+ * Parsing and formatting data from CBR (cbr.ru)
+ *
+ * @param string $date date formatted as dd.mm.yyyy
+ * @return string
+ */
+ public function get_currency_from_server($date)
+ {
+ $allow_currency = array('AZN', 'USD', 'EUR', 'AUD', 'JPY', 'GBP', 'BYR', 'LVL', 'TRY', 'UAH', 'EEK'); // 'AUD', 'AZN', 'GBP', 'BYR', 'BGN', 'BRL', 'HUF', 'DKK', 'USD', 'INR', 'KZT', 'CAD', 'KGS', 'CNY', 'LVL', 'LTL', 'MDL', 'NOK', 'PLN', 'RON', 'XDR', 'SGD', 'TJS', 'TRY', 'TMT', 'UZS', 'UAH', 'CZK', 'SEK', 'CHF', 'EEK', 'ZAR', 'KRW', 'JPY'
+ $link = "http://www.cbr.ru/scripts/XML_daily.asp?date_req=$date";
+ $text = @file_get_contents($link);
+
+ $xml = new SimpleXMLElement($text);
+
+ foreach ($xml->Valute as $exchange)
+ {
+ if(in_array($exchange->CharCode, $allow_currency))
+ {
+ $currency[] = UTF8::str_ireplace(',', '.', (string)$exchange->Value);
+ }
+ }
+
+ $AZN = $currency[0];
+ $RUR = round(1/$AZN, 4);
+ $USD = round($currency[1]/$AZN, 4);
+ $EUR = round($currency[2]/$AZN, 4);
+ $AUD = round($currency[2]/$AZN, 4);
+ $JPY = round($currency[2]/$AZN, 4);
+ $GBP = round($currency[2]/$AZN, 4);
+ $BYR = round($currency[2]/$AZN, 4);
+ $LVL = round($currency[2]/$AZN, 4);
+ $TRY = round($currency[2]/$AZN, 4);
+ $UAH = round($currency[2]/$AZN, 4);
+ $EEK = round($currency[2]/$AZN, 4);
+
+ $rates = array("AZN" => $AZN/$AZN, "RUR" => $RUR, "USD" => $USD, "EUR" => $EUR, "AUD" => $AUD, "JPY" => $JPY, "GBP" => $GBP, "BYR" => $BYR, "LVL" => $LVL, "TRY" => $TRY, "UAH" => $UAH, "EEK" => $EEK);
+ return $rates;
+
+ }
+} // End Currency aznrates Model
+
+?>
@@ -0,0 +1,12 @@
+<?php defined('SYSPATH') OR die('No direct script access.');
+
+// Catch-all route for aznrates classes to run
+Route::set('currency', 'aznxrates(/<date>)', array('date'=>'([0-9\.]+)'))
+ ->defaults(array(
+ 'controller' => 'currency',
+ 'action' => 'index',
+ 'date' => NULL,
+ )
+);
+
+?>

0 comments on commit 37b7c1a

Please sign in to comment.