<?php
namespace Fifa\ConnectId\Sdk\Exception;

use Exception;
use Fifa\ConnectId\Sdk\Model\XmlValidationError;

/**
 * Class XmlDeserializationException
 * @package Fifa\ConnectId\Sdk\Exception
 */
class XmlDeserializationException extends Exception
{
    /**
     * @var array|XmlValidationError[]
     */
    private $xmlValidationErrors;

    /**
     * @var string
     */
    private $xml;

    /**
     * XmlDeserializationException constructor.
     * @param string $xml
     * @param string $message
     * @param Exception|null $innerException
     * @param XmlValidationError[]|null $xmlValidationErrors
     */
    public function __construct($xml, $message, Exception $innerException = null, array $xmlValidationErrors = null)
    {
        if($innerException)
        {
            $message .=  $innerException->getMessage();
        }

        if($xmlValidationErrors){
            $message .= XmlExceptionHelper::flattenErrorMessages($xmlValidationErrors);
            $this->xmlValidationErrors = $xmlValidationErrors;
        }

        parent::__construct($message, 0, $innerException);
        $this->xml = $xml;
    }

    /**
     * @return string
     */
    public function __toString()
    {
        return $this->getMessage();
    }

    /**
     * @return string
     */
    public function getXml(): string
    {
        return $this->xml;
    }

    /**
     * @return array|XmlValidationError[]
     */
    public function getXmlValidationErrors(): array
    {
        return $this->xmlValidationErrors;
    }
}