<?php

if ( ! defined( 'ABSPATH' ) ) {
	exit;
}

/**
 * This file is part of miniOrange SAML plugin.
 */

include 'Assertion.php';

/**
 * Class for SAML2 Response messages.
 *
 */
class HssoSAML2Response
{
	/**
	 * The assertions in this response.
	 */
	private $assertions;

	/**
	 * The destination URL in this response.
	 */
	private $destination;

	private $certificates;
	private $signatureData;

	/**
	 * Constructor for SAML 2 response messages.
	 *
	 * @param DOMElement|NULL $xml The input message.
	 */
	public function __construct( DOMElement $xml = NULL, $relay_state = '' ) {
		//parent::__construct('Response', $xml);

		$this->assertions = array();
		$this->certificates = array();

		if ($xml === NULL) {
			return;
		}

		$sig = HssoUtilities::validateElement($xml);
		if ($sig !== FALSE) {
			$this->certificates = $sig['Certificates'];
			$this->signatureData = $sig;
		}

		/* set the destination from saml response */
		if ($xml->hasAttribute('Destination')) {
			$this->destination = $xml->getAttribute('Destination');
		}

		for ($node = $xml->firstChild; $node !== NULL; $node = $node->nextSibling) {
			if ($node->namespaceURI !== 'urn:oasis:names:tc:SAML:2.0:assertion') {
				continue;
			}

			if ($node->localName === 'Assertion' || $node->localName === 'EncryptedAssertion') {
				$this->assertions[] = new HssoSAML2Assertion($node, $relay_state);
			}
		}
	}

	/**
	 * Retrieve the assertions in this response.
	 *
	 * @return HssoSAML2Assertion[]|SAML2_EncryptedAssertion[]
	 */
	public function getAssertions()
	{
		return $this->assertions;
	}

	/**
	 * Set the assertions that should be included in this response.
	 *
	 * @param HssoSAML2Assertion[]|SAML2_EncryptedAssertion[] The assertions.
	 */
	public function setAssertions(array $assertions)
	{
		$this->assertions = $assertions;
	}

	public function getDestination()
	{
		return $this->destination;
	}


	public function getCertificates()
	{
		return $this->certificates;
	}

	public function getSignatureData()
	{
		return $this->signatureData;
	}
}
