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

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

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

	/**
	 * 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 E-banka se nepodarilo nacist');
		}

		preg_match('/Kurzovn..l.stek(.*)<\/form>/is', $file, $match);
		
		preg_match_all('/<tr[^>]*>(.*)<\/tr>/isU', $match[1], $rate_lines);
		
		$row = array();
		$i = 0;
		foreach ($rate_lines[1] as $rate_line) {
			preg_match_all('/<td[^>]*>(.*)<\/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[2]; 
			if (in_array($name, $this->important_currencies)) {
				$exchange_rate_info = new ExchangeRateInfo($name, $row[1], (float) $row[3], (float) $row[4]);
				$this->exchange_rates[$name] = $exchange_rate_info;
			}
		}
	}

}