diff --git a/csv_check.php b/csv_check.php index 3bf6117..4be0060 100644 --- a/csv_check.php +++ b/csv_check.php @@ -64,7 +64,8 @@ $fp = fopen ( $csv_datei, 'r' ); $y = 1; $error = 0; $zeile = fgetcsv( $fp, 100000, ';' ); -for ($x = 0; $x < count($zeile); $x++) { +$maxLoopLen = count($zeile); +for ($x = 0; $x < $maxLoopLen; $x++) { $zeile[$x] = str_replace("\xEF\xBB\xBF", "", $zeile[$x]); $inhalt[$y][$x] = $zeile[$x]; @@ -98,7 +99,8 @@ $y = 0; while ($zeile = fgetcsv($fp, 100000, ';')) { $y++; - for ($x = 0; $x < count ( $zeile ); $x++) { + $maxLoopLen = count($zeile); + for ($x = 0; $x < $maxLoopLen; $x++) { $inhalt[$y][$x] = str_replace("'", "\'", $zeile[$x]); } } @@ -109,27 +111,34 @@ echo '

3: Mandatory tags available and always filled in?'; unset($inv_array); $inv_error = 0; -function get_duplicates($array) { - return array_unique( array_diff_assoc( $array, array_unique( $array ) ) ); +/** + * Function for finding duplicates?. + * + * @param array $array Input array. + * + * @return array + */ +function get_duplicates(array $array):array { + return array_unique(array_diff_assoc($array, array_unique($array))); } $mandatory = ['inventory_number','object_type','object_title','object_description']; -for ($i = 0; $i < count($mandatory); $i++) { - if (!in_array($mandatory[$i], $erstezeile)) { - echo '
Mandatory: Column ' . $mandatory[$i] . ' missing'; +foreach ($mandatory as $tMandatoryField) { + if (!in_array($tMandatoryField, $erstezeile)) { + echo '
Mandatory: Column ' . $tMandatoryField . ' missing'; $error = $error + 1; $inv_error = $inv_error + 1; } else { - $spaltenr = array_search($mandatory[$i], $erstezeile); + $spaltenr = array_search($tMandatoryField, $erstezeile); for ($j = 0; $j < $y; $j++) { if ($inhalt[$j + 1][$spaltenr] == '') { - echo '
Missing value for ' . $mandatory[$i] . ' in row ' . ($j + 1) . ''; + echo '
Missing value for ' . $tMandatoryField . ' in row ' . ($j + 1) . ''; $error = $error + 1; $inv_error = $inv_error + 1; } - if ($mandatory[$i] == 'inventory_number') { + if ($tMandatoryField == 'inventory_number') { $inv_array[] = $inhalt[$j + 1][$spaltenr]; } } @@ -143,17 +152,15 @@ echo '

4: Inventory_number unique ?'; if (in_array('inventory_number', $erstezeile)) { $doppelte_inv = get_duplicates($inv_array); $doppelte_inv = array_values($doppelte_inv); - if (count($doppelte_inv) > 0) { - for ($i = 0; $i < count($doppelte_inv); $i++) - { - echo '
Multiple use of inventory_number ' . $doppelte_inv[$i] . ''; + if (!empty($doppelte_inv)) { + foreach ($doppelte_inv as $tDublicateInvNo) { + echo '
Multiple use of inventory_number ' . $tDublicateInvNo . ''; $error = $error + 1; } } else echo '
All inventory_numbers are unique !'; } -else -{ +else { echo '
Aborted, column inventory_number is missing'; $error = $error + 1; } @@ -175,11 +182,10 @@ foreach ($fieldsWithDependency as $tField => $tDependentFields) { } -if (isset($depencymessage) and $depencymessage != '') { +if (!empty($depencymessage)) { echo 'Dependent columns were not observed !'; - for ($i = 0; $i < count ($depencymessage); $i++) - { - echo '
' . $depencymessage[$i]; + foreach ($depencymessage as $tDepMsg) { + echo '
' . $tDepMsg; $error = $error + 1; } } @@ -194,14 +200,14 @@ $crosscheck1 = ['object_other_title','detailed_description','detailed_descriptio $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; -for ($l = 0; $l < count($crosscheck1); $l++) { +foreach ($crosscheck1 as $tCrossCheck) { - if (in_array($crosscheck1[$l], $erstezeile)) { + 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($crosscheck1[$l], $erstezeile)] == '') { - echo '
Tag ' . $crosscheck2[$l] . ' given but no entry for ' . $crosscheck1[$l] . ' (row ' . $j . ')'; - $depcon_error = $depcon_error + 1; + 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++; } } @@ -214,7 +220,7 @@ for ($l = 0; $l < count($eventpart); $l++) { for ($j = 1; $j < ($y + 1); $j++) { if ($inhalt[$j][array_search($eventpartsure[$l], $erstezeile)] !== '' and $inhalt[$j][array_search($eventpart[$l], $erstezeile)] == '') { echo '
Tag ' . $eventpartsure[$l] . ' given but no entry for ' . $eventpart[$l] . ' (row ' . $j . ')'; - $depcon_error = $depcon_error + 1; + $depcon_error++; } } }