$filePath = __DIR__ . '/../../../app/logs/xml/';
$fileName = 'xmlLog_' . date('d.m.Y') . '.xml';
if (file_exists($filePath . $fileName)) {
// append data if file already exist
$xml = simplexml_load_file($filePath . $fileName);
$productList = $xml->productList;
$lastElement = count($productList->product);
foreach ($logArray as $key => $data) {
$rN = $productList->addChild('product');
$rN->addChild('id', $lastElement);
$rN->addChild('productId', $logArray[$key]['productId']);
$rN->addChild('type', $logArray[$key]['type']);
$rN->addChild('oldValue', $logArray[$key]['oldValue']);
$rN->addChild('adjustment', $logArray[$key]['adjustment']);
$rN->addChild('newValue', $logArray[$key]['newValue']);
$lastElement++;
}
$xml->asXML($filePath . $fileName);
} else {
// create new file if not exist
$mainNode = new \SimpleXMLElement('<?xml version="1.0" encoding="UTF-8" standalone="yes"?><inventory></inventory>');
$productNode = $mainNode->addChild('productList');
foreach ($logArray as $key => $data) {
$rN = $productNode->addChild('product');
$rN->addChild('id', $key + 1);
$rN->addChild('productId', $logArray[$key]['productId']);
$rN->addChild('type', $logArray[$key]['type']);
$rN->addChild('oldValue', $logArray[$key]['oldValue']);
$rN->addChild('adjustment', $logArray[$key]['adjustment']);
$rN->addChild('newValue', $logArray[$key]['newValue']);
}
$fs = new Filesystem();
$fs->mkdir($filePath);
$mainNode->asXML($filePath . $fileName);
}