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

/**
 * The exporter class for tree structure like SimpleXmlElement
 * In order to generate a tree with the following structure:
 *
 * 	<XDF attr1='ATTR1' attr2='ATTR2'>
 *		<chld1></chld1>
 *		<chld2 attr1='ATTR1'  attr2='ATTR2'  attr3='ATTR3'>
 *			<chld21>CHLD21</chld21>
 *			<chld22>CHLD22</chld22>
 *			<chld23>
 *				VAL-CHDL23
 *				<chld233>CHLD2331</chld233>
 *				<chld233>CHLD2332</chld233>
 *			</chld23>
 *      </chld2>
 *
 *      <chld3>
 *      	<row>
 *      		<chld31>CHLD31</chld31>
 *      		<chld32>CHLD32</chld32>
 *      	</row>
 *
 *      	<row>
 *      		<chld31>CHLD31</chld31>
 *      		<chld32>CHLD32</chld32>
 *      	</row>
 *		</chld3>
 * </XDF>
 *
 * The $fieldTypes in the local exporter must be defined as follwing:
 *
 * 		protected $fieldTypes = Array(
 * 			'XDF' => Array(
 * 				'children' => Array(
 * 					'attr1' 	=>  	Array ('type' => 'S', 'isAttribute' => true,),
 * 					'attr11' 	=>		Array ('type' => 'S', 'isAttribute' => true, 'alias' => 'attr2'),
 *
 * 					'chld1' =>			Array ('forceVisible' => true,),
 * 					'chld2' =>			Array (
 * 						'children' => Array(
 * 							'attr1' 	=>  	Array ('type' => 'S', 'isAttribute' => true,),
 * 							'attr2' 	=>		Array ('type' => 'S', 'isAttribute' => true,),
 * 							'attr3' 	=>		Array ('type' => 'S', 'isAttribute' => true,),
 *
 * 							'chld21' => Array( 'type' => 'S', ),
 * 							'chld22' => Array( 'type' => 'S', ),
 * 							'chld23' => Array( 'type' => 'S',
 * 								'children' => Array(
 * 									'undef1' 	=> Array( 'type' => 'S', 'isValue' => true,),
 * 									'chld233' 	=> Array( 'type' => 'S', 'repeatable' => true,), // 'repeatable' indicates that the element chld233 should appear multiple times
 * 								),
 * 							),
 * 						),
 * 					),
 *
 * 					'chld3' =>	Array(
 * 						'children' =>		Array(
 * 							'row' =>		Array('repeatable' => true,
 * 								'children' => Array(
 * 									'chld31'  	=> Array( 'type' => 'S', ),
 * 									'chld32'	=> Array( 'type' => 'S', ),
 * 								),
 * 							),
 * 						),
 * 					),
 * 				),
 * 			),
 * 		);
 *
 * For the structure defined previously the builder will be configured as follow:
 * 		$ret= [];
 *		$ret['_report'] = 'XDF';
 *
 *		$ret['XDF']['attr1'] = 	'ATTR1';
 *		$ret['XDF']['attr11'] = 'ATTR2';
 *
 *		$ret['XDF']['chld1'] = '';
 *
 *		// chld 2
 *		$ret['XDF']['chld2']['attr1'] = 'ATTR1';
 *		$ret['XDF']['chld2']['attr2'] = 'ATTR2';
 *		$ret['XDF']['chld2']['attr3'] = 'ATTR3';
 *
 *		$ret['XDF']['chld2']['chld21'] = 'CHLD21';
 *		$ret['XDF']['chld2']['chld22'] = 'CHLD22';
 *
 *		// child 23
 *		$ret['XDF']['chld2']['chld23']['undef1'] = 'v-chld23';
 *		$ret['XDF']['chld2']['chld23']['chld233'][0] = 'chld3331';
 *		$ret['XDF']['chld2']['chld23']['chld233'][1] = 'chld3332';
 *
 *	 	$ret['XDF']['chld3']['row'][0]['chld31'] = 'value-31-0';
 *		$ret['XDF']['chld3']['row'][0]['chld32'] = 'value-32-0';
 *
 *		$ret['XDF']['chld3']['row'][1]['chld31'] = 'value-31-1';
 *		$ret['XDF']['chld3']['row'][1]['chld32'] = 'value-32-1';
 *
 *
 * @author Oana Stan
 *
 * This was used for XDFReport plugin. Unused for now
 */

abstract class ExporterTreeSimpleXml extends ExporterAutoFileManagement
{
	protected function node_setAttribute($node, $key, $value)
	{
		\Engine\Debug\ParserCallTrace\ParserCallTrace::getInstance()->logCallStart($this, __METHOD__, []);
		\Engine\Debug\ParserCallTrace\ParserCallTrace::getInstance()->logInput([
			'node' => $node,
			'key' => $key,
			'value' => $value,
		]);

		$ret = $node->addAttribute($key, $value);

		\Engine\Debug\ParserCallTrace\ParserCallTrace::getInstance()->logCallEnd();

		return $ret;
	}


	protected function node_setValue($node, $key, $value)
	{
		\Engine\Debug\ParserCallTrace\ParserCallTrace::getInstance()->logCallStart($this, __METHOD__, []);
		\Engine\Debug\ParserCallTrace\ParserCallTrace::getInstance()->logInput([
			'node' => $node,
			'key' => $key,
			'value' => $value,
		]);

		$ret = $node->addChild($key, $value);

		\Engine\Debug\ParserCallTrace\ParserCallTrace::getInstance()->logCallEnd();

		return $ret;
	}

	protected function node_appendValue($node, $value)
	{
		\Engine\Debug\ParserCallTrace\ParserCallTrace::getInstance()->logCallStart($this, __METHOD__, []);
		\Engine\Debug\ParserCallTrace\ParserCallTrace::getInstance()->logInput([
			'node' => $node,
			'value' => $value,
		]);

		$ret = $node->addChild($value);

		\Engine\Debug\ParserCallTrace\ParserCallTrace::getInstance()->logCallEnd();

		return $ret;
	}

	protected function node_setNewNode($node, $value)
	{
		\Engine\Debug\ParserCallTrace\ParserCallTrace::getInstance()->logCallStart($this, __METHOD__, []);
		\Engine\Debug\ParserCallTrace\ParserCallTrace::getInstance()->logInput([
			'node' => $node,
			'value' => $value,
		]);

		$ret = $node->addChild($value);

		\Engine\Debug\ParserCallTrace\ParserCallTrace::getInstance()->logCallEnd();

		return $ret;
	}
}
