<?php
namespace ModulesAbstract\PluginReports\Report\Model\Exporter;

class MergeExporter extends ExporterWithStorageAbstract
{
	/**
	 * List with all exporteds
	 * @var array
	 */
	protected $exporters = [];

	/**
	 * Add exported
	 * @param String $builderKeySource
	 * @param \ModulesAbstract\PluginReports\Report\Model\Exporter\ExporterAbstract $exporter
	 * @return \ModulesAbstract\PluginReports\Report\Model\Exporter\MergeExporter
	 */
	public function addExporter(\ModulesAbstract\PluginReports\Report\Model\Exporter\ExporterAbstract $exporter)
	{
		$exporter->setParentExporter($this);
		$this->exporters[] = $exporter;
		return $this;
	}

	/**
	 * (non-PHPdoc)
	 * @see \ModulesAbstract\PluginReports\Report\Model\Builder\BuilderAbstract::getRequiredFilters()
	 */
	public function getRequiredFilters()
	{
		$requiredFilters= [];

		foreach ($this->exporters as $exporter) {
			$requiredFilters = array_merge($requiredFilters, $exporter->getRequiredFilters());
		}

		return array_unique($requiredFilters);
	}

	/**
	 * Add a filter param
	 * @param string $key
	 * @param mixed $value
	 * @return \ModulesAbstract\PluginReports\Report\Model\Gatherer\GathererAbstract
	 */
	public function addFilter($key, $value)
	{
		$this->filters[$key] = $value;
		foreach ($this->exporters as $exporter) {
			$exporter->addFilter($key, $value);
		}

		return $this;
	}

	/**
	 * (non-PHPdoc)
	 * @see \ModulesAbstract\PluginReports\Report\Model\Builder\BuilderAbstract::setErrorReport()
	 */
	public function setErrorReport(\ModulesAbstract\PluginReports\Report\Model\ErrorReport $errorReport)
	{
		foreach ($this->exporters as $exporter) {
			$exporter->setErrorReport($errorReport);
		}

		return $this;
	}

	/**
	 * Initialize all the gatherer associated
	 * @return void
	 */
	protected function objectInitialize()
	{
		foreach ($this->exporters as $exporter) {
			$exporter->objectInitialize();
		}
	}

	/**
	 * Set report base path for all the gatherer associated
	 * @param String $basePath
	 * @return \ModulesAbstract\PluginReports\Report\Model\Exporter\ExporterAbstract
	 */
	public function setReportBasePath($basePath)
	{
		foreach ($this->exporters as $exporter) {
			$exporter->setReportBasePath($basePath);
		}
		return $this;
	}

	/**
	 * Called for every row that should get exported
	 * If not implemented, then exportReport() should be implemented and able to do a "block" export, with a bigger memory footprint
	 * @param Array $row
	 */
	public function exportRow($row)
	{
		foreach ($this->exporters as $exporter) {
			$exporter->exportRow($row);
		}

		return $this;
	}

	/**
	 * Export report
	 * @return void
	 */
	public function exportReport()
	{
		foreach ($this->exporters as $exporter) {
			$exporter->exportReport();
		}
	}
}
