<?php
/**
 * trida vyuziva tridu ExchangeRatesFetcher
 */   
require_once 'ExchangeRatesFetcher.php';

/**
 * trida vyuziva tridu ExchangeRatesInfo
 */   
require_once 'ExchangeRatesInfo.php';

/**
 * Trida pro stahovani kurzovnich listku ze stranek Unicreditbank
 *
 * @date 25-02-2008
 * @version 1.0
 */
class ExchangeRatesFetcherUcb extends ExchangeRatesFetcher
{
	/**
	 * konstruktor - nastaveni kodu banky 
	 */
	public function __construct($url)
	{
		parent::__construct($url);
		$this->bank_code = 'UCB';
	}

	/**
	 * Funkce pro stahnuti kurzovniho listku
	 * 	 	
	 * Funkce stahne kurzovni listek a ulozi ho do interni promenne $exchange_rates
	 *
	 * @throws  object  Exception
	 */	 	  	 	
	public function fetch()
	{
		$xml = @simplexml_load_file($this->url);
		if (!$xml) {
			throw new Exception('Soubor s kurzovym listkem UCB se nepodarilo nacist');
		}
		$info_array = array();
		foreach ($xml->children() as $exchange_rate) {
			foreach ($exchange_rate->children() as $currency) {
				$name = (string) $currency['name'];
				$type = (string) $exchange_rate['type'];
				$valid_from = (string) $exchange_rate['valid_from'];
				$purchase = 0;
				$sale = 0;
				if (in_array($name, $this->important_currencies)) {
					$info_array[$name]['quota'] = (int) $currency['quota'];
					// zajimaji nas jen devizy (prodej a nakup)
					if (strpos($type, '_PURCHASE_DEVIZA')) {
						$info_array[$name]['purchase'] = (float) $currency['rate'];
					}
					if (strpos($type, '_SALE_DEVIZA')) {
						$info_array[$name]['sale'] = (float) $currency['rate'];
					}
				}
			}
		}	
		foreach ($info_array as $name => $type) {
			$exchange_rate_info = new ExchangeRateInfo($name, $type['quota'], $type['purchase'], $type['sale']);
			$this->exchange_rates[$name] = $exchange_rate_info;
		}
	}

}