<?php

namespace App\CurrencyExchange;

class System_NbpPL extends SystemAbstract
{

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

	/**
	 * The system address.
	 * @var String
	 */
	protected $systemAddress='http://www.nbp.pl/kursy/xml/LastA.xml';

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


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

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

		// PARSE RESPONSE CONTENT /////////////////////////////////////////////////////////////////
		$result=[];
		if ($responseXml->pozycja) {
			foreach ($responseXml->pozycja as $thisCurrXml) {

				// get raw data
				$curr = (String)$thisCurrXml->kod_waluty; // the currency code
				$rate = (Float)str_replace(',', '.', (String)$thisCurrXml->kurs_sredni);
				$mul = (float)$thisCurrXml->przelicznik; // 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;

	}

}

