<?php
namespace App\Sabre\Api\Soap\Operation;

/**
 * Read Sabre Profiles Objects
 * The Read Profile Objects (EPS_EXT_ProfileReadRQ) API is used to read Profiles, Templates, Filters, Formats, Metadata, Validators, and Associations.
 * https://developer.sabre.com/docs/soap_apis/profiles/profiles_management/Read_Profile_Objects
 *
 * @author Dan Baluta
 */
class ProfileRead extends \App\Sabre\Api\Soap\Operation\GeneralAbstract
{
	protected $Version = '6.55';

	protected $Service = 'EPS_EXT_ProfileReadRQ';

	protected $Action = 'EPS_EXT_ProfileReadRQ';


	public function __construct($ConversationId, $BinarySecurityToken, $UniqueID, $DomainID=null)
	{
		$this->params = [];
		$this->params['ConversationId'] = $ConversationId;
		$this->params['BinarySecurityToken'] = $BinarySecurityToken;

		$this->params['UniqueID'] = $UniqueID;

		if ($DomainID) {
			$this->params['DomainID'] = $DomainID;
		} else {
			$this->params['DomainID'] = '*';
		}
	}

	protected function getRequestBody()
	{
		$Sabre_OTA_ProfileReadRQ = new \Engine\SimpleXmlElement("<?xml version=\"1.0\" encoding=\"UTF-8\"?><Sabre_OTA_ProfileReadRQ
			xmlns=\"http://www.sabre.com/eps/schemas\"
			xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\"
			xsi:schemaLocation=\"http://www.sabre.com/eps/schemas ..\schemasWSDL\Sabre_OTA_ProfileReadRQ.xsd\"></Sabre_OTA_ProfileReadRQ>");

		$Sabre_OTA_ProfileReadRQ->addAttribute('Version', $this->Version);

		//<Filter FilterID="6499173" DomainID="B0DE" ClientCode="TN" ClientContextCode="TMP"/>

		$Profile = $Sabre_OTA_ProfileReadRQ->addChild('Profile');

		$TPA_Identity = $Profile->addChild('TPA_Identity');
		$TPA_Identity->addAttribute('UniqueID', $this->params['UniqueID']);
		$TPA_Identity->addAttribute('ClientCode', $this->ClientCode);
		$TPA_Identity->addAttribute('DomainID', $this->params['DomainID']);
		$TPA_Identity->addAttribute('ClientContextCode', $this->ClientContextCode);

		//debug($Sabre_OTA_ProfileReadRQ->asPretty()); die;

		/*
		// Validate XML against XSD
		$msgs = \App\MessagesCollector::create();
		$msgs = $Sabre_OTA_ProfileReadRQ->validateAgainstXsd(__DIR__.'/_xsd/Sabre_OTA_ProfileReadRQ.xsd', $msgs);

		if ($msgs->hasErrorOrWarning()) {
			throw new \App\MessagesException($msgs);
		}
		*/

		return $Sabre_OTA_ProfileReadRQ->asStr();
	}


	protected function validateResponse(\Engine\SimpleXmlElement $response)
	{
	    if ((String)$response->Body->Sabre_OTA_ProfileReadRS->ResponseMessage->Errors->ErrorMessage) {
	        throw new \Exception\Net\Ws\Client\Base( (String)$response->Body->Sabre_OTA_ProfileReadRS->ResponseMessage->Errors->ErrorMessage );
	    }
	}

}
