<?php


namespace App\Config;

class OrderServicesTypes extends XmlDynamicRecordsAbstract
{

	/**
	 * Stores the configuration file (relative to the "files/config/" dir). For config files stored
	 * inside subdirectories, the subdirectory is included (Ex: services/servicesList.xml).
	 * @var String
	 */
	protected $fileName=CONFIG_FILE_RESERVATIONS_SERVICETYPES;


	/**
	 * (non-PHPdoc)
	 * @see \App\Config\XmlDynamicRecordsAbstract::getAll_rawData_extractDataFromFile_list()
	 */
	protected function getAll_rawData_extractDataFromFile_list(\SimpleXMLElement $xml)
	{
		// gets the final list of items to be returned
		$return=[];
		foreach ($xml->serviceType as $rst) {
			$return [ (String)$rst['code'] ] = (String)$rst;
		}
		return $return;
	}

	/**
	 * (non-PHPdoc)
	 * @see \App\Config\XmlDynamicRecordsAbstract::getAll_rawData_extractDataFromFile_default()
	 */
	protected function getAll_rawData_extractDataFromFile_default(\SimpleXMLElement $xml)
	{
		return (String)$xml['default'].'';
	}

	/**
	 * (non-PHPdoc)
	 * @see \App\Config\XmlDynamicRecordsAbstract::getAll_rawData_extractDataFromFile_mandatory()
	 */
	protected function getAll_rawData_extractDataFromFile_mandatory(\SimpleXMLElement $xml)
	{
		return (Int)$xml['mandatory'];
	}


	/**
	 * (non-PHPdoc)
	 * @see \App\Config\XmlDynamicRecordsAbstract::itemCheck()
	 */
	protected function itemCheck($code, $itemDetails)
	{
		// check itemDetails
		if (!is_string($itemDetails)) throw new \Exception('The order service type name must be set as a string.');
		// check code
		if (!$code) $this->addErrorMessage('The order service type `code` was not defined.');
		// check name
		if (!$itemDetails) $this->addErrorMessage('The order service type `name` was not defined.');
		// check code length
		if (mb_strlen($code) > 20) $this->addErrorMessage('The order service type `code` length must be smaller than 20 chars.');
		// check name length
		if (mb_strlen($itemDetails) > 50) $this->addErrorMessage('The order service type `name` length must be smaller than 50 chars.');
		// check if it contains special chars
		if (htmlClean($itemDetails) != $itemDetails) $this->addErrorMessage('The order service type `name` cannot contain html special characters.');

		// check that the name dows not already exist
		foreach ($this->itemsList as $c => $n) {
			if ($c != $code and $itemDetails == $n) {
				$this->addErrorMessage('This order service type `name` already exists.');
			}
		}
	}

	/**
	 * (non-PHPdoc)
	 * @see \App\Config\BaseAbstract::save_check()
	 */
	protected function save_check()
	{ }

	/**
	 * (non-PHPdoc)
	 * @see \App\Config\XmlBasicAbstract::save_write_do_getXml()
	 */
	protected function save_write_do_getXml()
	{

		// start composing the XML object
		$xml = simplexml_load_string('<?xml version="1.0" encoding="UTF-8"?><reservationServiceTypes/>');

		// LOOP THROUGH EACH SERVICE CODE AVAILABLE IN THE APP ------------------------------------
		foreach($this->itemsList as $rstCode => $rstName) {
			$servObj = $xml->addChild('serviceType', $rstName);
			$servObj['code'] = $rstCode;
		}

		// set the mandatory and the default flag
		$xml['mandatory'] = $this->mandatory;
		$xml['default'] = $this->defaultItem;

		// return the simplexml object
		return $xml;

	}
}
