improved error reporting

This commit is contained in:
Nathan Eikermann 2021-06-21 23:33:02 +02:00
parent 95468c609d
commit 562ff50bb0
3 changed files with 92 additions and 44 deletions

View File

@ -24,4 +24,26 @@ final class CSVXMLValidator
$inv_errors = [];
$depcon_errors = [];
}
public function addError(String $error)
{
$this->error_msgs[] = $error;
}
public function addInvError(String $error)
{
$this->inv_errors[] = $error;
}
public function addDepconError(String $error)
{
$this->depcon_errors[] = $error;
}
public function overallErrorCount(): int
{
return count($this->error_msgs)
+ count($this->inv_errors)
+ count($this->depcon_errors);
}
}

View File

@ -27,6 +27,7 @@ $csv_datei = MD_STD::realpath(__DIR__ . '/../csv/' . $filename);
$fieldNoMultiplicator = 10;
$fieldsGetter = new CsvxmlAvailableFields($lang);
$availableFields = $fieldsGetter->getFields();
$validator = new CSVXMLValidator();
$allowed = $eventpart = $eventpartsure = $fieldsWithDependency = $fieldsWithAllowedValueSet = [];
@ -68,7 +69,6 @@ echo '</form>';
echo '1: Only allowed tags (column names) used?';
$fp = fopen($csv_datei, 'r');
$y = 1;
$error = 0;
$zeile = fgetcsv($fp, 100000, ';');
$maxLoopLen = count($zeile);
for ($x = 0; $x < $maxLoopLen; $x++) {
@ -76,22 +76,29 @@ for ($x = 0; $x < $maxLoopLen; $x++) {
$zeile[$x] = str_replace("\xEF\xBB\xBF", "", $zeile[$x]);
$inhalt[$y][$x] = $zeile[$x];
if (!in_array($inhalt[1][$x], $allowed)) {
echo '<br><i style="font-style:normal;color:#990000;">ERROR in column ' . $x . ' created by value: ' . $inhalt[1][$x] . '</i>';
$error = $error + 1;
$validator->addError(
'<br><i style="font-style:normal;color:#990000;">ERROR in column ' . $x
. ' created by value: ' . $inhalt[1][$x] . '</i>'
);
}
//echo '<br/>';var_dump($inhalt[1][$x]);
}
fclose($fp);
$erstezeile = $zeile;
if ($error != 0) echo '<br><b style="color:#990000;">Not allowed tags found !</b>'; else echo '<br><i style="font-style:normal;color:#009900;">Only allowed tags used !</i>';
if (count($validator->error_msgs) != 0) {
echo '<br><b style="color:#990000;">Not allowed tags found !</b>';
} else {
echo '<br><i style="font-style:normal;color:#009900;">Only allowed tags used !</i>';
}
//// Check #2
echo '<br><br>2: Not allowed multiple use of tags (column names)?';
$compare = array_unique($zeile);
if (count($zeile) != count($compare)) {
echo '<br><b style="color:#990000;">There are duplicate column names !</b>';
$error = $error + 1;
$validator->addError(
'<br><b style="color:#990000;">There are duplicate column names !</b>'
);
} else {
echo '<br><i style="font-style:normal;color:#009900;">No duplicate column names !</i>';
}
@ -114,7 +121,6 @@ fclose($fp);
///// Check #3
echo '<br><br>3: Mandatory tags available and always filled in?';
unset($inv_array);
$inv_error = 0;
/**
* Function for finding duplicates?.
@ -129,20 +135,21 @@ function Get_duplicates(array $array):array
}
//TODO: function for returning the error strings & counting errors
$mandatory = ['inventory_number','object_type','object_title','object_description'];
foreach ($mandatory as $tMandatoryField) {
if (!in_array($tMandatoryField, $erstezeile)) {
echo '<br><i style="font-style:normal;color:#990000;">Mandatory: Column <b>' . $tMandatoryField . '</b> missing</i>';
$error = $error + 1;
$inv_error = $inv_error + 1;
$validator->addInvError(
'<br><i style="font-style:normal;color:#990000;">Mandatory: Column <b>'
. $tMandatoryField . '</b> missing</i>'
);
} else {
$spaltenr = array_search($tMandatoryField, $erstezeile);
for ($j = 0; $j < $y; $j++) {
if ($inhalt[$j + 1][$spaltenr] == '') {
echo '<br><i style="font-style:normal;color:#990000;">Missing value for <b>' . $tMandatoryField . '</b> in row ' . ($j + 1) . '</i>';
$error = $error + 1;
$inv_error = $inv_error + 1;
$validator->addInvError(
'<br><i style="font-style:normal;color:#990000;">Missing value for <b>'
. $tMandatoryField . '</b> in row ' . ($j + 1) . '</i>'
);
}
if ($tMandatoryField == 'inventory_number') {
$inv_array[] = $inhalt[$j + 1][$spaltenr];
@ -151,7 +158,7 @@ foreach ($mandatory as $tMandatoryField) {
}
}
if ($inv_error == 0) {
if (count($validator->inv_errors) == 0) {
echo '<br><i style="font-style:normal;color:#009900;">All mandatory tags available and with values !</i>';
}
@ -163,15 +170,18 @@ if (in_array('inventory_number', $erstezeile)) {
$doppelte_inv = array_values($doppelte_inv);
if (!empty($doppelte_inv)) {
foreach ($doppelte_inv as $tDublicateInvNo) {
echo '<br><i style="font-style:normal;color:#990000;">Multiple use of inventory_number <b>' . $tDublicateInvNo . '</b></i>';
$error = $error + 1;
$validator->addError(
'<br><i style="font-style:normal;color:#990000;">Multiple use of inventory_number <b>'
. $tDublicateInvNo . '</b></i>'
);
}
} else {
echo '<br><i style="font-style:normal;color:#009900;">All inventory_numbers are unique !</i>';
}
} else {
echo '<br><b style="font-style:normal;color:#990000;">Aborted, column inventory_number is missing</b>';
$error = $error + 1;
$validator->addError(
'<br><b style="font-style:normal;color:#990000;">Aborted, column inventory_number is missing</b>'
);
}
///// Check #5
@ -192,8 +202,7 @@ foreach ($fieldsWithDependency as $tField => $tDependentFields) {
if (!empty($depencymessage)) {
echo '<b style="color:#990000;">Dependent columns were not observed !</b>';
foreach ($depencymessage as $tDepMsg) {
echo '<br>' . $tDepMsg;
$error = $error + 1;
$validator->addError('<br>' . $tDepMsg)
}
} else {
echo '<i style="font-style:normal;color:#009900;">Dependent columns were observed !</i>';
@ -207,15 +216,16 @@ echo '<br><br>6: Dependency of content observed?';
$crosscheck1 = ['object_other_title','detailed_description','detailed_description','inscription','inscription','dimensions_separate_length_value', 'dimensions_separate_width_value', 'dimensions_separate_height_value', 'dimensions_separate_diameter_value', 'dimensions_separate_wall_thickness_value', 'dimensions_separate_weight_value','closer_location','bought_for','worth_value','worth_insurance_value'];
$crosscheck2 = ['object_other_title_kind_of','detailed_description_md','detailed_description_extern','inscription_md','inscription_extern','dimensions_separate_length_unit', 'dimensions_separate_width_unit', 'dimensions_separate_height_unit', 'dimensions_separate_diameter_unit', 'dimensions_separate_wall_thickness_unit', 'dimensions_separate_weight_unit','closer_location_as','bought_for_currency','worth_unit','worth_insurance_unit'];
$depcon_error = 0;
foreach ($crosscheck1 as $l => $tCrossCheck) {
if (in_array($tCrossCheck, $erstezeile)) {
for ($j = 1; $j < ($y + 1); $j++) {
if ($inhalt[$j][array_search($crosscheck2[$l], $erstezeile)] !== '' and $inhalt[$j][array_search($tCrossCheck, $erstezeile)] == '') {
echo '<br>Tag <b>' . $crosscheck2[$l] . '</b> given but no entry for <b>' . $tCrossCheck . '</b> (row ' . $j . ')';
$depcon_error++;
$validator->addDepconError(
'<br>Tag <b>' . $crosscheck2[$l] . '</b> given but no entry for <b>'
. $tCrossCheck . '</b> (row ' . $j . ')'
);
}
}
@ -227,8 +237,10 @@ foreach ($eventpart as $l => $tEventPart) {
if (in_array($tEventPart, $erstezeile)) {
for ($j = 1; $j < ($y + 1); $j++) {
if ($inhalt[$j][array_search($eventpartsure[$l], $erstezeile)] !== '' and $inhalt[$j][array_search($tEventPart, $erstezeile)] == '') {
echo '<br>Tag <b>' . $eventpartsure[$l] . '</b> given but no entry for <b>' . $tEventPart . '</b> (row ' . $j . ')';
$depcon_error++;
$validator->addDepconError(
'<br>Tag <b>' . $eventpartsure[$l] . '</b> given but no entry for <b>'
. $tEventPart . '</b> (row ' . $j . ')'
);
}
}
}
@ -237,8 +249,10 @@ foreach ($eventpart as $l => $tEventPart) {
if (in_array('dimensions_separate_show_md', $erstezeile)) {
for ($j = 1; $j < ($y + 1); $j++) {
if ($inhalt[$j][array_search('dimensions_separate_show_md', $erstezeile)] !== '' and ($inhalt[$j][array_search('dimensions_separate_length_value', $erstezeile)] == '') and $inhalt[$j][array_search('dimensions_separate_width_value', $erstezeile)] == '' and $inhalt[$j][array_search('dimensions_separate_heigt_value', $erstezeile)] == '' and $inhalt[$j][array_search('dimensions_separate_weight_value', $erstezeile)] == '' and $inhalt[$j][array_search('dimensions_separate_diameter_value', $erstezeile)] == '' and $inhalt[$j][array_search('dimensions_separate_wall_thickness_value', $erstezeile)] == '') {
echo '<br>Tag <b>dimensions_separate_show_md</b> given but no separate values available (row ' . $j . ')';
$depcon_error = $depcon_error + 1;
$validator->addDepconError(
'<br>Tag <b>dimensions_separate_show_md</b> given but no separate values available (row '
. $j . ')'
);
}
}
}
@ -246,8 +260,10 @@ if (in_array('dimensions_separate_show_md', $erstezeile)) {
if (in_array('dimensions_separate_show_extern', $erstezeile)) {
for ($j = 1; $j < ($y + 1); $j++) {
if ($inhalt[$j][array_search('dimensions_separate_show_extern', $erstezeile)] !== '' and ($inhalt[$j][array_search('dimensions_separate_length_value', $erstezeile)] == '') and $inhalt[$j][array_search('dimensions_separate_width_value', $erstezeile)] == '' and $inhalt[$j][array_search('dimensions_separate_heigt_value', $erstezeile)] == '' and $inhalt[$j][array_search('dimensions_separate_weight_value', $erstezeile)] == '' and $inhalt[$j][array_search('dimensions_separate_diameter_value', $erstezeile)] == '' and $inhalt[$j][array_search('dimensions_separate_wall_thickness_value', $erstezeile)] == '') {
echo '<br>Tag <b>dimensions_separate_show_extern</b> given but no separate values available (row ' . $j . ')';
$depcon_error = $depcon_error + 1;
$validator->addDepconError(
'<br>Tag <b>dimensions_separate_show_extern</b> given but no separate values available (row '
. $j . ')'
);
}
}
}
@ -272,7 +288,9 @@ for ($im=1;$im<11;$im++)
}
}
*/
if ($depcon_error == 0) echo '<br><i style="font-style:normal;color:#009900;">Dependency of content was observed !</i>';
if (count($validator->depcon_errors) == 0) {
echo '<br><i style="font-style:normal;color:#009900;">Dependency of content was observed !</i>';
}
///// Check #7
echo '<br><br>7: Not allowed values in controlled lists?<br>';
@ -316,11 +334,10 @@ for ($i = 2; $i <= $y; $i++) {
}
}
if (isset($errormessage) and $errormessage != '') {
if (!empty($errormessage)) {
echo '<b style="color:#990000;">Columns with controlled values contain invalid values !</b>';
foreach ($errormessage as $tMsg) {
echo '<br>' . $tMsg;
$error++;
$validator->addError('<br>' . $tMsg);
}
} else {
echo '<i style="font-style:normal;color:#009900;">Values in controlled fields are all valid !</i>';
@ -378,8 +395,7 @@ if ($hasanyimage > 0) {
if (!empty($errormessage)) {
echo '<b style="color:#990000;">There is not one main image for each object !</b>';
foreach ($errormessage as $tMsg) {
echo '<br>' . $tMsg;
$error++;
$validator->addError('<br>' . $tMsg);
}
} else {
echo '<i style="font-style:normal;color:#009900;">For each object that has images attached exactly one main image is given !</i>';
@ -389,9 +405,19 @@ if ($hasanyimage > 0) {
}
echo '<hr>';
if ($error + $depcon_error > 0) {
if ($validator->overallErrorCount() > 0) {
echo '
<p>Error(s) found: ' . ($error + $depcon_error) . '</p>';
<p>Error(s) found: ' . $validator->overallErrorCount() . '</p>';
foreach ($validator->error_msgs as $msg) {
echo $msg;
}
foreach ($validator->inv_errors as $msg) {
echo $msg;
}
foreach ($validator->depcon_errors as $msg) {
echo $msg;
}
echo '<a href="index6.php?fnam=' . htmlspecialchars($_GET['fnam']) . '" class="buttonLike">Create XML for md:import (utf8)</a><br>';
} else {
echo '<a href="index6.php?fnam=' . htmlspecialchars($_GET['fnam']) . '" class="buttonLike">Create XML for md:import (utf8)</a><br>';