diff --git a/composer.json b/composer.json index a802a65..08c0911 100644 --- a/composer.json +++ b/composer.json @@ -20,6 +20,8 @@ "ext-iconv": "*", "ext-curl": "*", "ext-json": "*", - "ext-openssl": "*" + "ext-openssl": "*", + "ext-simplexml": "*", + "ext-dom": "*" } } diff --git a/core/lib/Core/Helpers/SystemFunctions.class.php b/core/lib/Core/Helpers/SystemFunctions.class.php index d468870..a0364c4 100644 --- a/core/lib/Core/Helpers/SystemFunctions.class.php +++ b/core/lib/Core/Helpers/SystemFunctions.class.php @@ -603,6 +603,50 @@ } + /** + * Хелпер для метода преобразования массива в XML + * + * @param array $data Массив данных + * @param \SimpleXmlElement $xml_data Объект SimpleXmlElement + * + * @return void + */ + public static function arrayToXmlHelper(array $data, \SimpleXmlElement &$xml_data): void + { + foreach ($data as $key => $value) { + if (is_numeric($key)) { + $key = 'element_' . $key; + } + if (is_array($value)) { + $subnode = $xml_data->addChild($key); + static::arrayToXmlHelper($value, $subnode); + } else { + $xml_data->addChild($key, htmlspecialchars($value)); + } + } + } + + /** + * Преобразование массива в XML + * + * @param array $arData Массив данных + * + * @return void + */ + public static function arrayToXml(array $arData = [], bool $format = false): string + { + $xml = new \SimpleXMLElement(''); + static::arrayToXmlHelper($arData ,$xml); + + if($format){ + $dom = dom_import_simplexml($xml)->ownerDocument; + $dom->preserveWhiteSpace = false; + $dom->formatOutput = true; + return $dom->saveXML(); + } + return $xml->asXML(); + } + public static function showPage() { $loader = new \Twig\Loader\FilesystemLoader(PATH_TO_TEMPLATES); diff --git a/test.php b/test.php index 9df41f1..61d820a 100644 --- a/test.php +++ b/test.php @@ -2,7 +2,5 @@ $_SERVER['DOCUMENT_ROOT'] = '/home/rsgrinko/sites/dev.it-stories.ru'; require_once $_SERVER['DOCUMENT_ROOT'] . '/core/bootstrap.php'; - print_r($_REQUEST); - print_r($_SERVER); use Core\MQTasks; echo MQTasks::sendJokeToTelegram();