【发布时间】:2025-12-12 08:05:02
【问题描述】:
我正在运行以下代码:
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,
];
}
}
我遇到的问题是,当调用此类并且 $type_check 不等于 'string_check' 时,似乎 'string_length_check' 函数没有运行。但是,回声线仍在运行中。
上面的类/代码正在通过下面的类代码运行:
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);
}
}
结果或输出如下所示:
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 )
这不是一个主要问题,但是知道我是否做错了什么或如何解决这个问题会很有帮助。
提前致谢。
【问题讨论】:
标签: php laravel function class