<?php
namespace Engine\Soap;

/**
 * Soap client options
 * @author oana
 *
 */
class SoapClientOptions
{

	public $trace = 							1;
	public $exceptions = 						1;
	public $features = 							SOAP_SINGLE_ELEMENT_ARRAYS;
	public $encoding  =							'utf-8';
	public $connection_timeout =				10;
	public $socket_timeout = 					NULL;

	public $proxy_host = 						NULL;
	public $proxy_port =						NULL;
	public $proxy_login =						NULL;
	public $proxy_password	=					NULL;

	public $verify_peer = 						NULL;
	public $HTTPHeaders = 							[];

	public $localCertificate = 					[	// these need to be PEM-encoded files!
		'use' => 					false,
		'certificatePath' =>		null,			// @var string
		'certificatePassword' => 	null,			// @var string
		'privateKeyPath' =>			null,			// @var string
	];

	protected $classMap = [];

	/**
	 * Empty constructor - do not delete it as child classes call parent::__construct (at least in PHP 5.3)
	 */
	public function __construct()
	{
	}


	/**
	 * Transform all options into array used by SoapClient
	 * @return Array
	 */
	public function getOptions()
	{
		$options = [];

		if ($this->trace !== NULL) {
			$options['trace'] = 		$this->trace;
		}

		if ($this->exceptions !== NULL) {
			$options['exceptions'] = 	$this->exceptions;
		}

		if ($this->features !== NULL) {
			$options['features'] =		$this->features;
		}

		if ($this->encoding !== NULL) {
			$options['enconding'] = $this->encoding;
		}

		if ($this->connection_timeout !== NULL) {
			$options['connection_timeout'] = $this->connection_timeout;
		}

		if ($this->proxy_host !== NULL) {
			$options['proxy_host'] = $this->proxy_host;
		}

		if ($this->proxy_port !== NULL) {
			$options['proxy_port'] = $this->proxy_port;
		}

		if ($this->proxy_login !== NULL) {
			$options['proxy_login'] = $this->proxy_login;
		}

		if ($this->proxy_password !== NULL) {
			$options['proxy_password'] = $this->proxy_password;
		}

		if ($this->verify_peer !== NULL) {
			$options['verify_peer'] = $this->verify_peer;
		}

		$options['HTTPHeaders'] = $this->HTTPHeaders;

		if ($this->classMap) {
			$options['classmap'] = $this->classMap;
		}

		$options['localCertificate'] = null;
		if ($this->localCertificate && $this->localCertificate['use']) {
			$options['localCertificate'] = $this->localCertificate;
		}

		return $options;
	}

	/**
	 * Map classes
	 * @param string $xmlClass
	 * @param string $appClass
	 * @return \Engine\Soap\SoapClientOptions
	 */

	public function addClassMap($xmlClass, $appClass)
	{
		$this->classMap[$xmlClass] = $appClass;
		return $this;
	}

	/**
	 * Map classes
	 * @param array $map
	 * @return \Engine\Soap\SoapClientOptions
	 */

	public function setClassMap(Array $map)
	{
		foreach ($map as $xmlClass => $appClass) {
			$this->addClassMap($xmlClass, $appClass);
		}

		return $this;
	}

	/**
	 * Add a custom header
	 * @param string $key
	 * @param string $value
	 * @return \Engine\Soap\SoapClientOptions
	 */
	public function addHTTPHeader($key, $value)
	{
		$this->HTTPHeaders[]= [
			'key' => 	$key,
			'value' => 	$value,
		];

		return $this;
	}

}