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

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

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

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

		preg_match('/<div class="text-X">KurzovnĂ­ lĂ­stek<\/div>(.*)<\/table>/is', $file, $match);
		
		preg_match_all('/<tr class="color-[^>]*>(.*)<\/tr>/isU', $match[1], $rate_lines);
		
		$row = array();
		$i = 0;
		foreach ($rate_lines[1] as $rate_line) {
			preg_match_all('/<td class="text-[^>]*>(.*)<\/td>/isU', $rate_line, $row[$i]);
			$i++;
		}
		
		$rate_lines = array();

		$i = 0;
		foreach ($row as $r) {						
			foreach($r[1] as $val) {	
				$rate_lines[$i][] = trim(strip_tags($val));
			}
			$i++;
		}

		foreach ($rate_lines as $row) {
			$name     = $row[0];
			$quota     = str_replace(',', '.', $row[1]);
			$purchase = str_replace(',', '.', $row[6]);
			$sale     = str_replace(',', '.', $row[7]); 
			if (in_array($name, $this->important_currencies)) {
				$exchange_rate_info = new ExchangeRateInfo($name, $quota, $purchase, $sale);
				$this->exchange_rates[$name] = $exchange_rate_info;
			}
		}
	}

}