diff --git a/classes/CSVXMLValidator.php b/classes/CSVXMLValidator.php new file mode 100644 index 0000000..118c2d2 --- /dev/null +++ b/classes/CSVXMLValidator.php @@ -0,0 +1,27 @@ + + */ +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 = []; + } +} diff --git a/public/index3.php b/public/index3.php index 57dceb9..0a38ee1 100644 --- a/public/index3.php +++ b/public/index3.php @@ -66,10 +66,10 @@ echo ''; ///// Check #1 //echo '
';print_r($allowed);echo '
'; 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 '
Not allowed tags found !
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 '
There are dublicate column names !'; +if (count($zeile) != count($compare)) { + echo '
There are duplicate column names !'; $error = $error + 1; } else { - echo '
No dublicate column names !'; + echo '
No duplicate column names !'; } @@ -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 '

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 '

5: Dependent colums observed ?
'; // 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 '

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 {$columnName} on row {$i}: " + . $value . " (allowed values: " + . implode(", ", $fieldsWithAllowedValueSet[$columnName]) + . ")"; + } + + } } - $errormessage[] = "Disallowed value in column {$columnName} on row {$i}: " . $value . " (allowed values: " . implode(", ", $fieldsWithAllowedValueSet[$columnName]) . ")"; } - } }