I have the below code being run:
class column_field_length_validation extends Controller
{
private $input_column_type;
private $input_column_field_length;
private $input_csv_line_data;
public function __construct ($recieved_input_column_type, $recieved_input_column_field_length, $received_input_csv_line_data)
{
$this->input_column_type = $recieved_input_column_type;
$this->input_column_field_length = $recieved_input_column_field_length;
$this->input_csv_line_data = $received_input_csv_line_data;
}
public function run ()
{
$type_check = $this->input_column_type;
$csv_line_data = $this->input_csv_line_data;
$column_field_length = $this->input_column_field_length;
$type_check_function = [
'string_check' => $this->string_length_check($column_field_length, $csv_line_data),
];
foreach ($type_check_function as $key => $value) {
if ($type_check == $key) {
return $value;
}
}
}
public function string_length_check ($column_field_length, $csv_line_data)
{
$max_string_length = $column_field_length / 3;
$length_check = (strlen($csv_line_data) <= $max_string_length) ? 1 : 0;
echo 'string check preformed = ';
return [
'errors' => $length_check,
];
}
}
The issue I'm having is when this class is called and $type_check doesn't equal to 'string_check' it seems the 'string_length_check' function doesn't run. However, the echo line is still being run.
The above class/code is being run via the class code below:
class check_csv_data_against_column_types extends Controller
{
public function run(Request $request)
{
$headings_to_check = (new columns_to_be_checked)->run($request);
$table_data = (new return_table_data)->run($request);
$table_name = $table_data['table_name'];
$column_types = $table_data['column_types'];
$column_lengths = $table_data['column_lengths'];
$csv_file = $_FILES['csvfile']['tmp_name'];
$handle = fopen($csv_file, "r");
$csv_data = fgetcsv($handle);
$heading_count = count($headings_to_check['headings']);
$db = DB::table($table_name)->get();
while (! feof($handle)) {
$csv_line = fgetcsv($handle);
for ($i=0; $i < $heading_count; $i++) {
$current_heading = $headings_to_check['headings'][$i];
$current_heading_position = $headings_to_check['positions'][$i];
$current_column_length = $column_lengths[$i];
$column_type_position = $column_types[$current_heading_position];
$line = $csv_line[$current_heading_position] ?? 'empty line break';
if ($line == 'empty line break') {
break;
}
$type_check = (new column_type_check_required($column_type_position))->run();
echo $type_check;
echo nl2br("\r\n");
print_r((new csv_line_data_column_type_validation($type_check, $line))->run());
echo nl2br("\r\n");
print_r((new column_field_length_validation($type_check, $current_column_length, $line))->run());
echo nl2br("\r\n");
}
}
fclose($handle);
}
}
The result or output looks like the below:
int_check
Array ( [errors] => 1 [csv_line_data] => 1 )
EXAMPLE --> string check preformed =
timestamp_check
Array ( [errors] => 1 [csv_line_data] => 2021-04-14 19:47:14 )
string check preformed =
timestamp_check
Array ( [errors] => 1 [csv_line_data] => 2021-04-14 19:47:14 )
string check preformed =
string_check
Array ( [errors] => 1 [csv_line_data] => for sale )
string check preformed = Array ( [errors] => 1 )
int_check
Array ( [errors] => 1 [csv_line_data] => 2 )
string check preformed =
timestamp_check
Array ( [errors] => 1 [csv_line_data] => 2021-04-14 19:47:14 )
string check preformed =
timestamp_check
Array ( [errors] => 1 [csv_line_data] => 2021-04-14 19:47:14 )
string check preformed =
string_check
Array ( [errors] => 1 [csv_line_data] => for rent )
string check preformed = Array ( [errors] => 1 )
int_check
Array ( [errors] => 1 [csv_line_data] => 3 )
string check preformed =
timestamp_check
Array ( [errors] => 1 [csv_line_data] => 2021-04-14 19:47:14 )
string check preformed =
timestamp_check
Array ( [errors] => 1 [csv_line_data] => 2021-04-14 19:47:14 )
string check preformed =
string_check
Array ( [errors] => 1 [csv_line_data] => off market )
string check preformed = Array ( [errors] => 1 )
int_check
Array ( [errors] => 1 [csv_line_data] => 4 )
string check preformed =
timestamp_check
Array ( [errors] => 1 [csv_line_data] => 2021-04-14 19:47:14 )
string check preformed =
timestamp_check
Array ( [errors] => 1 [csv_line_data] => 2021-04-14 19:47:14 )
string check preformed =
string_check
Array ( [errors] => 1 [csv_line_data] => sold (subject to contract) )
string check preformed = Array ( [errors] => 1 )
This isn't a major issue however it would be helpfull to know if I'm doing anything wrong or how to resolve this.
Thanks in advance.