<?php
/**
 * IRIX PHP Library
 * PHP Version 5.3+.
 *
 * @author Jonathan Beliën <jbe@geo6.be>
 * @copyright 2016 Jonathan Beliën
 * @note The IRIX (International Radiological Information Exchange) format is developed and maintained by IAEA (International Atomic Energy Agency) <https://www.iaea.org/>
 */

namespace IRIX\EventInformation;

/**
 * IRIX PHP Library - EventInformation Section.
 *
 * @author Jonathan Beliën <jbe@geo6.be>
 */
class PlantStatus
{
    public $core_state = null;
    public $spent_fuel_state = null;
    public $trend_in_conditions = null;
    public $description = null;

    private $_xml = null;

    public function toXML()
    {
        $this->_xml = new \DOMDocument('1.0', 'UTF-8');
        $this->_xml->formatOutput = \IRIX\Report::_PRETTY;

        $plant_status = $this->_xml->createElementNS(\IRIX\Report::_NAMESPACE.'/EventInformation', 'PlantStatus');
        $this->_xml->appendChild($plant_status);

        if (!is_null($this->core_state)) {
            $plant_status->appendChild($this->_xml->importNode($this->core_state->getXMLElement(), true));
        }
        if (!is_null($this->spent_fuel_state)) {
            $spent_fuel_state = $this->_xml->createElementNS(\IRIX\Report::_NAMESPACE.'/EventInformation', 'SpentFuelState', $this->spent_fuel_state);
            $plant_status->appendChild($spent_fuel_state);
        }
        if (!is_null($this->trend_in_conditions)) {
            $trend_in_conditions = $this->_xml->createElementNS(\IRIX\Report::_NAMESPACE.'/EventInformation', 'TrendInConditions', $this->trend_in_conditions);
            $plant_status->appendChild($trend_in_conditions);
        }
        if (!is_null($this->description)) {
            $description = $this->_xml->createElementNS(\IRIX\Report::_NAMESPACE.'/EventInformation', 'Description', $this->description);
            $plant_status->appendChild($description);
        }

        return $this->_xml;
    }

    public function getXMLElement()
    {
        $this->toXML();

        return $this->_xml->getElementsByTagNameNS(\IRIX\Report::_NAMESPACE.'/EventInformation', 'EmergencyClassification')->item(0);
    }

    public static function readXMLElement($domelement)
    {
        $plant_status = new self();

        $core_state = $event_information->getElementsByTagNameNS(\IRIX\Report::_NAMESPACE.'/EventInformation', 'CoreState')->item(0);
        if (!is_null($core_state)) {
            $plant_status->core_state = \IRIX\EventInformation\PlantStatus\CoreState::readXMLElement($core_state);
        }

        $item = $domelement->getElementsByTagNameNS(\IRIX\Report::_NAMESPACE.'/EventInformation', 'SpentFuelState')->item(0);
        if (!is_null($item)) {
            $plant_status->spent_fuel_state = $item->textContent;
        }
        $item = $domelement->getElementsByTagNameNS(\IRIX\Report::_NAMESPACE.'/EventInformation', 'TrendInConditions')->item(0);
        if (!is_null($item)) {
            $plant_status->trend_in_conditions = $item->textContent;
        }
        $item = $domelement->getElementsByTagNameNS(\IRIX\Report::_NAMESPACE.'/EventInformation', 'Description')->item(0);
        if (!is_null($item)) {
            $plant_status->description = $item->textContent;
        }

        return $plant_status;
    }
}

