My method counts the number of times andan error occurs.
I have a feeling my snippet isn't the best practice way of doing this. So when one of the validation methods fails I want to stop the method.
public static function validate($data)
{
$errors = [
'settings_mismatch' => 0,
'name_not_set' => 0,
'price_not_set' => 0,
'payment_qty_not_set' => 0,
'payment_qty_zero' => 0,
'payment_unit_not_set' => 0,
'payment_unit_not_allowed' => 0
];
if (!isset($data['name'])) {
$errors['name_not_set']++;
return $errors;
}
if (!isset($data['price'])) {
$errors['price_not_set']++;
return $errors;
}
if (!isset($data['payment_qty'])) {
$errors['payment_qty_not_set']++;
return $errors;
}
if (!isset($data['payment_unit'])) {
$errors['payment_unit_not_set']++;
return $errors;
}
if (isset($data['payment_qty']) && $data['payment_qty'] == 0) {
$errors['payment_qty_zero']++;
return $errors;
}
if (isset($data['payment_unit']) && !in_array(strtolower(self::transformUnit($data)['payment_unit']), self::$allowedMeasurements)) {
$errors['payment_unit_not_allowed']++;
return $errors;
}
return $errors;
}