<?php
 namespace App\Config; class CustomersFlags extends XmlDynamicRecordsAbstract { protected $fileName=CONFIG_FILE_CUSTOMERS_FLAGS; private $allowedCodes= ['Blue', 'Green', 'Orange', 'Pink', 'Purple', 'Red', 'Yellow']; protected function getAll_rawData_extractDataFromFile_list(\SimpleXMLElement $xml) { $return=[]; foreach($xml->custFlag 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('Customer flag name must be set as a string.'); if (!$code) $this->addErrorMessage('The customer flag `code` was not defined.'); if (!$itemDetails) $this->addErrorMessage('The customer flag `name` was not defined.'); if (mb_strlen($itemDetails) > 50) $this->addErrorMessage('The customer flag `name` length must be smaller than 50 chars.'); if (htmlClean($itemDetails) != $itemDetails) $this->addErrorMessage('The customer flag `name` cannot contain html special characters.'); foreach($this->itemsList as $c=>$n) { if ($c!=$code and $itemDetails == $n) { $this->addErrorMessage('This customer flag `name` already exists.'); } } if (!in_array($code, $this->allowedCodes)) $this->addErrorMessage('This customer 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"?><customerFlagList/>'); foreach($this->itemsList as $code => $name) { $servObj = $xml->addChild('custFlag ', $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; } } 