<?php
 namespace App\Config; class OrderFlags extends XmlDynamicRecordsAbstract { protected $fileName=CONFIG_FILE_RESERVATIONS_FLAGS; private $allowedCodes= ['Blue', 'Green', 'Orange', 'Pink', 'Purple', 'Red', 'Yellow']; protected function getAll_rawData_extractDataFromFile_list(\SimpleXMLElement $xml) { $return = []; foreach($xml->resFlag as $flag) { $return [ (String)$flag['code'] ] = (String)$flag; } return $return; } protected function getAll_rawData_extractDataFromFile_default(\SimpleXMLElement $xml) { return (String)$xml['default'].''; } protected function getAll_rawData_extractDataFromFile_mandatory(\SimpleXMLElement $xml) { return (Int)$xml['mandatory']; } protected function itemCheck($code, $itemDetails) { if (!is_string($itemDetails)) throw new \Exception('Order flag name must be set as a string.'); if (!$code) $this->addErrorMessage('The order flag `code` was not defined.'); if (!$itemDetails) $this->addErrorMessage('The order flag `name` was not defined.'); if (mb_strlen($itemDetails)>50) $this->addErrorMessage('The order flag `name` length must be smaller than 50 chars.'); if (htmlClean($itemDetails) != $itemDetails) $this->addErrorMessage('The order flag `name` must not contain html special characters.'); foreach($this->itemsList as $c=>$n) { if ($c!=$code and $itemDetails==$n) { $this->addErrorMessage('This order flag `name` already exists.'); } } if (!in_array($code, $this->allowedCodes)) $this->addErrorMessage('This order flag `code` is not valid.'); } protected function save_check() { } protected function save_write_do_getXml() { $xml = simplexml_load_string('<?xml version="1.0" encoding="UTF-8"?><reservationFlagList/>'); foreach ($this->itemsList as $code=>$name) { $servObj = $xml->addChild('resFlag '); $servObj[0] = $name; $servObj['code'] = $code; } $xml['mandatory'] = $this->mandatory; $xml['default'] = $this->defaultItem; return $xml; } public function getAllowedCodes() { return $this->allowedCodes; } public function getAvailableCodes() { if ($this->constructType != 'admin') throw new \Exception("You can use the method `".get_called_class().'::'.__FUNCTION__."` only if the constructor type is `admin`."); $availCodes = $this->allowedCodes; foreach($this->itemsList as $c => $val) { $pos = array_search($c, $availCodes); if ($pos !== false) unset($availCodes[$pos]); } return $availCodes; } } 