<?php

namespace Fifa\ConnectId\Sdk\Util\MessageSerializers;

use Fifa\ConnectId\Sdk\Model\Transfer\FirstProRegistration;
use Fifa\ConnectId\Sdk\Util\PersonLocalXmlSerializer\PersonLocalXmlSerializer;
use Fifa\ConnectId\Sdk\Util\PersonLocalXmlSerializer\PersonLocalXmlSerializerInterface;
use Fifa\ConnectId\Sdk\Util\Serialization\Configurations\CommonXmlSerializerConfiguration;
use Fifa\ConnectId\Sdk\Util\Serialization\Configurations\FirstProRegistrationXmlSerializerConfiguration;
use Fifa\ConnectId\Sdk\Util\Serialization\ValidatingXmlSerializerBase;
use Fifa\ConnectId\Sdk\Util\XmlDeserializer\XmlDeserializer;
use XML_Serializer;

class FirstProRegistrationXmlSerializer extends ValidatingXmlSerializerBase
{
    /**
     * @var PersonLocalXmlSerializerInterface
     */
    private $personLocalSerializer;

    public function __construct()
    {
        $this->personLocalSerializer = new PersonLocalXmlSerializer();

        $xmlDeserializer = new XmlDeserializer(FirstProRegistration::class,
            FirstProRegistrationXmlSerializerConfiguration::getMappings(),
            CommonXmlSerializerConfiguration::getCustomConstructors()
        );

        $xmlSerializer = new XML_Serializer();
        $xmlSerializer->setOptions(CommonXmlSerializerConfiguration::buildOptions('FirstProRegistration',
            [
                XML_SERIALIZER_OPTION_SCALAR_AS_ATTRIBUTES => FirstProRegistrationXmlSerializerConfiguration::getScalarAsAttributes(),
                XML_SERIALIZER_OPTION_TAGMAP => FirstProRegistrationXmlSerializerConfiguration::getTagmap()
            ]
        ));

        parent::__construct($xmlSerializer, $xmlDeserializer);
    }

    /**
     * @inheritDoc
     * @param FirstProRegistration $firstProRegistration
     */
    protected function nullifyUnsetArrays($firstProRegistration)
    {
        if ($firstProRegistration->Player !== null) {
            $this->personLocalSerializer->nullifyUnsetArrays($firstProRegistration->Player);
        }
    }
}