diff --git a/classes/CSVXMLValidator.php b/classes/CSVXMLValidator.php index 118c2d2..484af01 100644 --- a/classes/CSVXMLValidator.php +++ b/classes/CSVXMLValidator.php @@ -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); + } } diff --git a/classes/FieldEntry.php b/classes/FieldEntry.php index 4a07442..9e33aa7 100644 --- a/classes/FieldEntry.php +++ b/classes/FieldEntry.php @@ -21,12 +21,12 @@ final class FieldEntry /** * Function for constructing a new FieldEntry Object. * - * @param bool $required True if the field is required. - * @param array $allowedValues Array of allowed values. - * @param array $dependsOn Array of fields the entry depends on. - * @param string $remark String variable. - * @param string $name_human_readable Human readable translation. - * @param string $explica String variable + * @param bool $required True if the field is required. + * @param array $allowedValues Array of allowed values. + * @param array $dependsOn Array of fields the entry depends on. + * @param string $remark String variable. + * @param string $name_human_readable Human readable translation. + * @param string $explica String variable */ public function __construct( bool $required, array $allowedValues, diff --git a/public/index3.php b/public/index3.php index 0a38ee1..6d19257 100644 --- a/public/index3.php +++ b/public/index3.php @@ -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 ''; 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 '
ERROR in column ' . $x . ' created by value: ' . $inhalt[1][$x] . ''; - $error = $error + 1; + $validator->addError( + '
ERROR in column ' . $x + . ' created by value: ' . $inhalt[1][$x] . '' + ); } //echo '
';var_dump($inhalt[1][$x]); } fclose($fp); $erstezeile = $zeile; -if ($error != 0) echo '
Not allowed tags found !'; else echo '
Only allowed tags used !'; +if (count($validator->error_msgs) != 0) { + echo '
Not allowed tags found !'; +} else { + echo '
Only allowed tags used !'; +} //// Check #2 echo '

2: Not allowed multiple use of tags (column names)?'; $compare = array_unique($zeile); if (count($zeile) != count($compare)) { - echo '
There are duplicate column names !'; - $error = $error + 1; + $validator->addError( + '
There are duplicate column names !' + ); } else { echo '
No duplicate column names !'; } @@ -114,7 +121,6 @@ fclose($fp); ///// Check #3 echo '

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 '
Mandatory: Column ' . $tMandatoryField . ' missing'; - $error = $error + 1; - $inv_error = $inv_error + 1; + $validator->addInvError( + '
Mandatory: Column ' + . $tMandatoryField . ' missing' + ); } else { $spaltenr = array_search($tMandatoryField, $erstezeile); for ($j = 0; $j < $y; $j++) { if ($inhalt[$j + 1][$spaltenr] == '') { - echo '
Missing value for ' . $tMandatoryField . ' in row ' . ($j + 1) . ''; - $error = $error + 1; - $inv_error = $inv_error + 1; + $validator->addInvError( + '
Missing value for ' + . $tMandatoryField . ' in row ' . ($j + 1) . '' + ); } 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 '
All mandatory tags available and with values !'; } @@ -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 '
Multiple use of inventory_number ' . $tDublicateInvNo . ''; - $error = $error + 1; + $validator->addError( + '
Multiple use of inventory_number ' + . $tDublicateInvNo . '' + ); } } else { echo '
All inventory_numbers are unique !'; } } else { - echo '
Aborted, column inventory_number is missing'; - $error = $error + 1; + $validator->addError( + '
Aborted, column inventory_number is missing' + ); } ///// Check #5 @@ -192,8 +202,7 @@ foreach ($fieldsWithDependency as $tField => $tDependentFields) { if (!empty($depencymessage)) { echo 'Dependent columns were not observed !'; foreach ($depencymessage as $tDepMsg) { - echo '
' . $tDepMsg; - $error = $error + 1; + $validator->addError('
' . $tDepMsg) } } else { echo 'Dependent columns were observed !'; @@ -207,15 +216,16 @@ echo '

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 '
Tag ' . $crosscheck2[$l] . ' given but no entry for ' . $tCrossCheck . ' (row ' . $j . ')'; - $depcon_error++; + $validator->addDepconError( + '
Tag ' . $crosscheck2[$l] . ' given but no entry for ' + . $tCrossCheck . ' (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 '
Tag ' . $eventpartsure[$l] . ' given but no entry for ' . $tEventPart . ' (row ' . $j . ')'; - $depcon_error++; + $validator->addDepconError( + '
Tag ' . $eventpartsure[$l] . ' given but no entry for ' + . $tEventPart . ' (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 '
Tag dimensions_separate_show_md given but no separate values available (row ' . $j . ')'; - $depcon_error = $depcon_error + 1; + $validator->addDepconError( + '
Tag dimensions_separate_show_md 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 '
Tag dimensions_separate_show_extern given but no separate values available (row ' . $j . ')'; - $depcon_error = $depcon_error + 1; + $validator->addDepconError( + '
Tag dimensions_separate_show_extern given but no separate values available (row ' + . $j . ')' + ); } } } @@ -272,7 +288,9 @@ for ($im=1;$im<11;$im++) } } */ -if ($depcon_error == 0) echo '
Dependency of content was observed !'; +if (count($validator->depcon_errors) == 0) { + echo '
Dependency of content was observed !'; +} ///// Check #7 echo '

7: Not allowed values in controlled lists?
'; @@ -316,11 +334,10 @@ for ($i = 2; $i <= $y; $i++) { } } -if (isset($errormessage) and $errormessage != '') { +if (!empty($errormessage)) { echo 'Columns with controlled values contain invalid values !'; foreach ($errormessage as $tMsg) { - echo '
' . $tMsg; - $error++; + $validator->addError('
' . $tMsg); } } else { echo 'Values in controlled fields are all valid !'; @@ -378,8 +395,7 @@ if ($hasanyimage > 0) { if (!empty($errormessage)) { echo 'There is not one main image for each object !'; foreach ($errormessage as $tMsg) { - echo '
' . $tMsg; - $error++; + $validator->addError('
' . $tMsg); } } else { echo 'For each object that has images attached exactly one main image is given !'; @@ -389,9 +405,19 @@ if ($hasanyimage > 0) { } echo '
'; -if ($error + $depcon_error > 0) { + +if ($validator->overallErrorCount() > 0) { echo ' -

Error(s) found: ' . ($error + $depcon_error) . '

'; +

Error(s) found: ' . $validator->overallErrorCount() . '

'; + 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 'Create XML for md:import (utf8)
'; } else { echo 'Create XML for md:import (utf8)
';