added a validator class

This commit is contained in:
Nathan Eikermann 2021-06-13 23:00:36 +02:00
parent 60c5265358
commit 95468c609d
2 changed files with 63 additions and 38 deletions

View File

@ -0,0 +1,27 @@
<?PHP
/**
* Contains a helper class for validation errors.
*
* @author Nathan Eikermann <nathan@museum-digital.de>
*/
declare(strict_types = 1);
/**
* Class holding the information about various errors
* that can occur during validation
*/
final class CSVXMLValidator
{
public array $error_msgs;
public array $inv_errors;
public array $depcon_errors;
/**
* Constructor
*/
public function __construct()
{
$error_msgs = [];
$inv_errors = [];
$depcon_errors = [];
}
}

View File

@ -66,10 +66,10 @@ echo '</form>';
///// Check #1
//echo '<pre>';print_r($allowed);echo '</pre>';
echo '1: Only allowed tags (column names) used?';
$fp = fopen ( $csv_datei, 'r' );
$fp = fopen($csv_datei, 'r');
$y = 1;
$error = 0;
$zeile = fgetcsv( $fp, 100000, ';' );
$zeile = fgetcsv($fp, 100000, ';');
$maxLoopLen = count($zeile);
for ($x = 0; $x < $maxLoopLen; $x++) {
@ -87,15 +87,13 @@ if ($error != 0) echo '<br><b style="color:#990000;">Not allowed tags found !</b
//// Check #2
echo '<br><br>2: Not allowed multiple use of tags (column names)?';
//TODO: only check if the sizes are the same
$compare = array_unique($zeile);
$result = identical_values($zeile, $compare);
if ($result == false) {
echo '<br><b style="color:#990000;">There are dublicate column names !</b>';
if (count($zeile) != count($compare)) {
echo '<br><b style="color:#990000;">There are duplicate column names !</b>';
$error = $error + 1;
} else {
echo '<br><i style="font-style:normal;color:#009900;">No dublicate column names !</i>';
echo '<br><i style="font-style:normal;color:#009900;">No duplicate column names !</i>';
}
@ -125,7 +123,7 @@ $inv_error = 0;
*
* @return array
*/
function get_duplicates(array $array):array
function Get_duplicates(array $array):array
{
return array_unique(array_diff_assoc($array, array_unique($array)));
@ -161,7 +159,7 @@ if ($inv_error == 0) {
///// Check #4
echo '<br><br>4: Inventory_number unique ?';
if (in_array('inventory_number', $erstezeile)) {
$doppelte_inv = get_duplicates($inv_array);
$doppelte_inv = Get_duplicates($inv_array);
$doppelte_inv = array_values($doppelte_inv);
if (!empty($doppelte_inv)) {
foreach ($doppelte_inv as $tDublicateInvNo) {
@ -182,18 +180,13 @@ echo '<br><br>5: Dependent colums observed ?<br>';
// Check for correct handling of dependent fields
foreach ($fieldsWithDependency as $tField => $tDependentFields) {
//TODO: change order
if (array_search($tField, $erstezeile) === false) {
continue;
}
foreach ($tDependentFields as $tDependentField) {
if (array_search($tDependentField, $erstezeile) === false) {
$depencymessage[] = "Dependency issue at column $tField: Corresponding column $tDependentField is missing";
if (array_search($tField, $erstezeile) !== false) {
foreach ($tDependentFields as $tDependentField) {
if (array_search($tDependentField, $erstezeile) === false) {
$depencymessage[] = "Dependency issue at column $tField: Corresponding column $tDependentField is missing";
}
}
}
}
if (!empty($depencymessage)) {
@ -210,6 +203,7 @@ if (!empty($depencymessage)) {
echo '<br><br>6: Dependency of content observed?';
//TODO: get the values for these arrays dynamically?
// JRE: Maybe we can merge them into availablefields
$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'];
@ -287,34 +281,38 @@ for ($i = 2; $i <= $y; $i++) {
foreach ($inhalt[$i] as $key => $value) {
$columnName = $inhalt[1][$key];
// If the field is not restricted, then continue
if (!isset($fieldsWithAllowedValueSet[$columnName])) continue;
// Only do the check if the field is not restricted
if (isset($fieldsWithAllowedValueSet[$columnName])) {
// For others: check if the value is from the list of allowed values.
if (!in_array($value, $fieldsWithAllowedValueSet[$columnName])) {
// For others: check if the value is from the list of allowed values.
if (!in_array($value, $fieldsWithAllowedValueSet[$columnName])) {
// It may be that the value is empty together with all dependent fields,
// because it's in a repeat field that was needed earlier.
if (empty($value) && !empty($fieldsWithDependency[$columnName])) {
// It may be that the value is empty together with all dependent fields,
// because it's in a repeat field that was needed earlier.
if (empty($value) && !empty($fieldsWithDependency[$columnName])) {
$allDependentsEmpty = true;
foreach ($fieldsWithDependency[$columnName] as $depField) {
// Find keys of dependent field
$depFieldKey = array_search($depField, $inhalt[1]);
if (!empty($inhalt[$i][$depFieldKey])) {
$allDependentsEmpty = false;
break;
$allDependentsEmpty = true;
foreach ($fieldsWithDependency[$columnName] as $depField) {
// Find keys of dependent field
$depFieldKey = array_search($depField, $inhalt[1]);
if (!empty($inhalt[$i][$depFieldKey])) {
$allDependentsEmpty = false;
break;
}
}
}
//TODO: reverse the IF and use it before the error message
if ($allDependentsEmpty === true) continue;
if ($allDependentsEmpty !== true) {
$errormessage[] = "Disallowed value in column <code>{$columnName}</code> on row <code>{$i}</code>: <em>"
. $value . "</em> (allowed values: <small>"
. implode(", ", $fieldsWithAllowedValueSet[$columnName])
. "</small>)";
}
}
}
$errormessage[] = "Disallowed value in column <code>{$columnName}</code> on row <code>{$i}</code>: <em>" . $value . "</em> (allowed values: <small>" . implode(", ", $fieldsWithAllowedValueSet[$columnName]) . "</small>)";
}
}
}