<?php
namespace App\Utils\BaseParser\Utils;

/**
 * Parser for the Hotel product code/multiplier/SIPPRM tag
 * @author Lucian Sirbu
 *
 * @see \App\Utils\BaseParser\Utils\HotelProductCodeRoomParser
 * @see \App\Utils\BaseParser\Utils\HotelProductCodeParser
 */
class HotelProductCodeRateParser
{
	/**
	 * The input text
	 * @var string
	 */
	protected $text = null;

	/**
	 *
	 * @var array
	 */
	protected $rates = [
		[ 'code3'=> 'RAC', 'code1'=> 'R', 'translation' => 'Standard - non-discounted rates', ],
		[ 'code3'=>	'ASC', 'code1'=> 'A', 'translation' => 'Association - Discounted rates for association members', ],
		[ 'code3'=> 'CLB', 'code1'=> 'B', 'translation' => 'Club - special rates for hotel club members', ],
		[ 'code3'=> 'CNV', 'code1'=> 'V', 'translation' => 'Convention - rates for convention group attendees', ],
		[ 'code3'=> 'COR', 'code1'=> 'C', 'translation' => 'Corporate - rates for corporate members', ],
		[ 'code3'=> 'FMP', 'code1'=> 'F', 'translation' => 'Family plan - Family plan rates', ],
		[ 'code3'=> 'GOV', 'code1'=> 'G', 'translation' => 'Government - rates for government employees', ],
		[ 'code3'=> 'MIL', 'code1'=> 'M', 'translation' => 'Military - rates for military personnel', ],
		[ 'code3'=> 'PKG', 'code1'=> 'P', 'translation' => 'Package - package tour rates', ],
		[ 'code3'=> 'SEN', 'code1'=> 'S', 'translation' => 'Senior citizen rates', ],
		[ 'code3'=> 'SPL', 'code1'=> 'L', 'translation' => 'Special - special discounted rates', ],
		[ 'code3'=> 'TUR', 'code1'=> 'T', 'translation' => 'Tour - rates available for tour groups', ],
		[ 'code3'=> 'TVL', 'code1'=> 'I', 'translation' => 'Travel industry - rates for travel industry employees', ],
		[ 'code3'=> 'WKD', 'code1'=> 'W', 'translation' => 'Weekend - rates for weekend stay', ],

		[ 'code3'=> 'PRO', 'code1'=>null, 'translation' => 'Promotional Rates', ],
		[ 'code3'=> 'SRS', 'code1'=>null, 'translation' => 'Senior citizen rates', ],
		[ 'code3'=> 'STP', 'code1'=>null, 'translation' => 'Stopover', ],
	];


	/**
	 * Internal class options
	 */
	protected $options = [
	];

	/**
	 * Set the text to parse
	 * @param string $text
	 * @return $this
	 */
	public function setText($text)
	{
		$this->text = $text;
		return $this;
	}

	/**
	 * Extract the passenger name from the input string
	 * @return array
	 */
	public function extract()
	{
		$text = $this->text;

		if (strlen($text)==1) {
			$matchingRates = array_filter($this->rates, function($r) use ($text) {
				return $r['code1']==$text;
			});
		}
		elseif (strlen($text)==3) {
			$matchingRates = array_filter($this->rates, function($r) use ($text) {
				return $r['code3']==$text;
			});
		}


		if ($matchingRates) {
			return reset($matchingRates);
		}

		if ($text) {
			return [
			];
		}

		return null;
	}
}
