<?php

namespace App\ExportData;

abstract class ConvertAbstract
{

	/**
	 * The current XML file exported by TINA.
	 * @var \SimpleXMLElement
	 */
	protected $xml;

	/**
	 * Attaches the current XML file object to the class.
	 * @param \SimpleXMLElement $xml
	 */
	public function __construct(\SimpleXMLElement $xml)
	{
		$this->xml = $xml;
	}

	/**
	 * Converts the current XML into the desired output (text, XML, object, etc).
	 * @param \App\ExportData\OutputAbstract $output
	 * @throws \Exception
	 */
	public function convert(\App\ExportData\OutputAbstract $output)
	{

		// does the actual conversion
		$this->process($output);

		// check that the "ok" flag was set
		if ($output->getOk()===null) {
			throw new \Exception("The conversion of an export data file in class `".get_called_class()."` should set the `ok` attribute as a true/false value; message thrown in output convert().");
		}

	}

	/**
	 * Does the actual processing of the conversion.
	 * @param \App\ExportData\OutputAbstract $output
	 */
	abstract protected function process(\App\ExportData\OutputAbstract $output);

}

