Files
nuhodds_fi4/app/Console/Commands/Xml.php
T
2025-12-05 07:00:25 +01:00

77 lines
2.4 KiB
PHP

<?php
namespace App\Console\Commands;
use App\Utilities\Notice;
class Xml
{
public static $file;
public static function load($url)
{
$debug = NuhBash::$options['d'] ?? 0;
$save = NuhBash::$options['s'] ?? 0;
$read = NuhBash::$options['r'] ?? 0;
$filename = app_path().'/Console/Commands/TestFiles/'.strtolower(str_replace(' ', '_', NuhBash::$options['name'])).'.xml';
if ($debug) {
Notice::note("Loading URL\n".$url);
}
if ($save || $read) {
Notice::note("FILE NAME\n".$filename);
}
if ($read) {
if ($debug) {
Notice::note('READ LOCAL', 'r');
}
// return simplexml_load_file(self::$file, null, LIBXML_NOCDATA);
if (! file_exists($filename)) {
Notice::note("Error: File $filename does not exist.", 'r', 1);
} elseif (! is_readable($filename)) {
Notice::note("Error: File $filename is not readable.", 'r', 1);
} else {
$data = simplexml_load_file($url, 'SimpleXMLElement', LIBXML_NOCDATA);
if ($data === false) {
Notice::note("Error: Failed to load XML from $filename", 'r', 1);
foreach (libxml_get_errors() as $error) {
Notice::note("XML Error: {$error->message}\n", 'r');
}
} else {
// Process the XML object
return $data;
}
}
}
$data = simplexml_load_file($url, 'SimpleXMLElement', LIBXML_NOCDATA);
if ($data === false && $debug) {
Notice::note("Error: Failed to load XML from $url", 'r', 1);
foreach (libxml_get_errors() as $error) {
Notice::note("XML Error: {$error->message}\n", 'r');
}
}
if ($save) {
if (! file_exists($filename)) {
Notice::note("Error: File $filename does not exist.", 'r', 1);
} elseif (! is_readable($filename)) {
Notice::note("Error: File $filename is not readable.", 'r', 1);
} elseif (! is_writable($filename)) {
Notice::note("Error: File $filename is not writable.", 'r', 1);
} else {
$data->asXml($filename);
Notice::note('SAVED AND EXIT', 'b', 1);
}
}
return $data;
}
}