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

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

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

	/**
	 * Funkce pro stahnuti kurzovniho listku
	 * 	 	
	 * Funkce stahne kurzovni listek a ulozi ho do interni promenne $exchange_rates
	 *
	 * @throws  object  Exception
	 */	 	  	 	
	public function fetch()
	{
		$lines = file($this->url);
		if (!$lines) {
			throw new Exception('Soubor s kurzovym listkem Ceske sporitelny se nepodarilo nacist');
		}

		$i = 0;
		$rate = array(); 
		foreach ($lines as $line_num => $line) {
			$line = trim($line);
			if ($i > 1 && !empty($line)) { // hodnoty zacinaji az na 3. radku
				$line = str_replace('"', '', $line);
				list($country, $quota, $currency, $change_val, $sign, 
				     $purchase_dev, $sale_dev, $middle_dev, 
					 $purchase_val, $sale_val, $middle_val, $CNB_middle) = explode(',', $line);
					 
				$rate[$currency]['quota']        = (int) $quota;
				$rate[$currency]['purchase_dev'] = (float) $purchase_dev;
				$rate[$currency]['sale_dev']     = (float) $sale_dev;
			}
			$i++;
		}
		foreach ($rate as $name => $type) {
			if (in_array($name, $this->important_currencies)) {
				$exchange_rate_info = new ExchangeRateInfo($name, $type['quota'], $type['purchase_dev'], $type['sale_dev']);
				$this->exchange_rates[$name] = $exchange_rate_info;
			}
		}
	}

}