Created
October 12, 2015 05:44
-
-
Save tilhom/8d4ccda6f7bfda133d87 to your computer and use it in GitHub Desktop.
MyCurrencyGrabber
This file contains hidden or 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 | |
namespace Megagroup; | |
interface CurrencyGrabberInterface | |
{ | |
/** | |
* @brief Downloads currency rates from the remote server | |
* and saves in database. | |
* | |
* @return boolean | |
*/ | |
public function download(); | |
/** | |
* @brief Converts rate | |
* | |
* @param string $from Source currency symbol | |
* @param string $to Destination Currency symbol | |
* | |
* @return double | |
*/ | |
public function convert($from, $to); | |
/** | |
* @brief Retrieves currency from DB | |
* | |
* @param string $symbol | |
* | |
* @return array | |
*/ | |
public function get($symbol); | |
} | |
class MyCurrencyGrabber implements CurrencyGrabberInterface, \Iterator | |
{ | |
private $db; | |
private $position = 0; | |
private $arrayCurrency; | |
function __construct(Db $db=null) | |
{ | |
$this->db = $db; | |
} | |
public function current() { | |
return $this->arrayCurrency[$this->position]; | |
} | |
public function next() { | |
++$this->position; | |
} | |
public function valid() { | |
return isset($this->arrayCurrency[$this->position]); | |
} | |
public function key() { | |
return $this->position; | |
} | |
public function rewind() { | |
if($this->initArray()) $this->position = 0; | |
else trigger_error("No data!", E_USER_ERROR); | |
//var_dump($this->arrayCurrency); | |
} | |
public function download() | |
{ | |
if(!$this->db) return false; | |
//prepare and clear buffer | |
$this->db->query('truncate table currency_rates'); | |
$xml = new \DOMDocument(); | |
$url = 'http://www.cbr.ru/scripts/XML_daily.asp?date_req=' . date('d.m.Y'); | |
//download data from server | |
if (@$xml->load($url)) | |
{ | |
$root = $xml->documentElement; | |
$items = $root->getElementsByTagName('Valute'); | |
//save the data to database | |
foreach ($items as $item) | |
{ | |
$numcode = $this->esc($item->getElementsByTagName('NumCode')->item(0)->nodeValue); | |
$charcode = $this->esc($item->getElementsByTagName('CharCode')->item(0)->nodeValue); | |
$nominal = $this->esc($item->getElementsByTagName('Nominal')->item(0)->nodeValue); | |
$name = $this->esc($item->getElementsByTagName('Name')->item(0)->nodeValue); | |
$value = $this->esc($item->getElementsByTagName('Value')->item(0)->nodeValue); | |
$value = floatval(str_replace(',', '.', $value)); | |
$query="INSERT INTO `cbr`.`currency_rates` (`num_code`, `char_code`, `nominal`, `name`, `value`)" | |
." VALUES ($numcode, '$charcode', $nominal, '$name', $value)"; | |
$this->db->query($query); | |
} | |
return true; | |
} | |
else | |
return false; | |
} | |
public function convert($from=null, $to=null) | |
{ | |
$rate_from = null; | |
$rate_to = null; | |
//var_dump($from);var_dump($to); | |
if(!$from or !$to or !$this->db){ | |
return 0; | |
} | |
$from=$this->esc($from); | |
$to=$this->esc($to); | |
// get data from db | |
$query= | |
"select * from currency_rates where char_code = '$from' or char_code = '$to'"; | |
if ($result = $this->db->query($query) and $result->num_rows==2) { | |
//die('The query passed!'.$result->num_rows); | |
$rate_from=$result->fetch_object(); | |
$rate_to=$result->fetch_object(); | |
$result->close(); | |
} | |
else // if no data | |
return 0; | |
// calculate rate | |
// the formula to calculate exchange rate is | |
// nominalFrom*nominalTo*valueFrom/valueTo | |
$result = $rate_from->nominal*$rate_to->nominal*$rate_from->value/$rate_to->value; | |
return round($result,4); | |
} | |
public function get($symbol) | |
{ | |
$result = null; | |
if(!$symbol or !$this->db){ | |
return $result; | |
} | |
$symbol=$this->esc($symbol); | |
// get data from db | |
$query="select * from currency_rates where char_code = '$symbol'"; | |
if ($res = $this->db->query($query)) { | |
$result=$res->fetch_assoc(); | |
$res->close(); | |
} | |
return $result; | |
} | |
private function esc($value='') | |
{ | |
return $this->db->real_escape_string($value); | |
} | |
private function initArray() | |
{ | |
$xml = new \DOMDocument(); | |
$url = 'http://www.cbr.ru/scripts/XML_daily.asp?date_req=' . date('d.m.Y'); | |
//download data from server | |
if (@$xml->load($url)) | |
{ | |
$root = $xml->documentElement; | |
$items = $root->getElementsByTagName('Valute'); | |
//save the data to database | |
foreach ($items as $item) | |
{ | |
$numcode = htmlspecialchars($item->getElementsByTagName('NumCode')->item(0)->nodeValue); | |
$charcode = htmlspecialchars($item->getElementsByTagName('CharCode')->item(0)->nodeValue); | |
$nominal = htmlspecialchars($item->getElementsByTagName('Nominal')->item(0)->nodeValue); | |
$name = htmlspecialchars($item->getElementsByTagName('Name')->item(0)->nodeValue); | |
$value = htmlspecialchars($item->getElementsByTagName('Value')->item(0)->nodeValue); | |
$value = floatval(str_replace(',', '.', $value)); | |
//fill array | |
$this->arrayCurrency[]=[ | |
"numcode"=> $numcode, | |
"charcode"=> $charcode, | |
"nominal"=> $nominal, | |
"name"=> $name, | |
"value"=> $value, | |
"value"=> $value | |
]; | |
} | |
return true; | |
} | |
else | |
return false; | |
} | |
} | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment