<?php

namespace App\CurrencyExchange;

class System_TcmbTR extends SystemAbstract
{

	/**
	 * The reference currency.
	 * @var String
	 */
	protected $referenceCurrency='TRY';

	/**
	 * The system address.
	 * @var String
	 */
	protected $systemAddress='https://www.tcmb.gov.tr/kurlar/today.xml';

	/**
	 * Gets the exchange rates for this system.
	 * @return Array
	 */
	public function getRates()
	{

		// GET THE RESPONSE CONTENT ///////////////////////////////////////////////////////////////
		$responseString=$this->systemResponse();

		// PARSE RESPONSE CONTENT /////////////////////////////////////////////////////////////////
		$response=simplexml_load_string($responseString);
		if (!$response) throw new Exception("The external system's  reply was not a well formed XML.");

		// PARSE RESPONSE CONTENT /////////////////////////////////////////////////////////////////
		$result=[];
		if ($response->Currency) {
			foreach ($response->Currency as $thisCurr) {

				// get raw data
				$curr=(String)$thisCurr['CurrencyCode']; // the currency code
				$rate=(float)$thisCurr->ForexSelling;
				$mul=(float)$thisCurr->Unit; // the multiplier
				if ($mul>1) $realRate=$rate/$mul;
				else $realRate=$rate;

				// set currency exchange rates
				if ($curr and $realRate>0) $result[$curr]=$realRate;
			}
		}

		return $result;

	}

}

