Why not separate the tests ? i.e. do each test one by one, and throw an exception if a specific test fails ?
Copy-pasting from your code, it would probably look like this :
if (!strtotime($data[11])) {
throw new Exception("field 11 : {$data[11]} is not a valid date");
}
if (!strtotime($data[12])) {
throw new Exception("field 12 : {$data[12]} is not a valid date");
}
// Some more...
if (!ctype_alnum($data[8])) {
throw new Exception("field 8 : {$data[8]} is not a valid alnum");
}
// And so on...
// And when all is tested, you know the items
// in $data are all OK
This way :
- You can know which field caused a validation failure
- If you have several distinct tests on the same field, you can know which specific test failed
And, as a possibility, you could (if needed) thrown different kind of exceptions, depending on the test that failed (i.e. one kind of exception for dates, one for integers, ...) -- in some cases, that might be useful.
Edit after the comment : more full example
Yes, you can validate field by field, and still work line by line.
You just have to wrap your testing code in a try/catch block, that's inside the loop that goes line by line ; a bit like that :
$validData = array();
$errors = array();
while ($data = fgetcsv($f)) {
try {
// Validate the data of the current line
// And, if valid, insert it into the database
if (!strtotime($data[11])) {
throw new Exception("field 11 : {$data[11]} is not a valid date");
}
if (!strtotime($data[12])) {
throw new Exception("field 12 : {$data[12]} is not a valid date");
}
// Some more...
if (!ctype_alnum($data[8])) {
throw new Exception("field 8 : {$data[8]} is not a valid alnum");
}
// And so on...
// And when all is tested, you know the items
// in $data are all OK
// => which means it can be inserted into the DB
// Or you can just put the valid data into a "temporary" array, that will be used later :
$validData[] = $data;
} catch (Exception $e) {
// An error has occurend on the current line
// You can log it, if necessary :
$errors[] = $e->getMessage();
}
}
// Now that the whole file has been read, test if there's been an error :
if (empty($errors)) {
// No error
// => insert the data that's in $validData
} else {
// There's been an error
// => don't insert the data that's in $validData, if you don't want to insert anything
}
With that :
- If there is an exception on one line (i.e. validation fails), you'll jump to the catch block, to deal with the problem
- And the loop will then restart, for the next line.
EDIT: (By Yacoby)
Rather than having endless if statments, you could just define which elements should be checked by which function and then process them in a loop. That way it avoids having 16 if statements.
Code example:
foreach ( array(11,12,16) as $index ){
if ( !strtotime($data[$i]) ){
throw new Exception("field {$i} : {$data[$i]} is not a date");
}
}
foreach ( array(1,3,4,5,6,7,8) as $index ){
if ( !ctype_alnum($data[$i]) ){
throw new Exception("field {$i} : {$data[$i]} is not alphanumeric");
}
}
foreach ( array(2, 10) as $i ){
if ( !ctype_digit($data[$i]) ){
throw new Exception("field {$i} : {$data[$i]} is not a valid digit");
}
}
if ( !$this->_is_valid($data[14]) ){
throw new Exception("field {14} : {$data[14]} is not valid");
}