| 1 |
<?php |
| 2 |
|
| 3 |
namespace FluentForm\App\Services\Settings\Validator; |
| 4 |
|
| 5 |
use FluentForm\Framework\Validator\Validator; |
| 6 |
use FluentForm\Framework\Validator\ValidationException; |
| 7 |
|
| 8 |
abstract class Validate |
| 9 |
{ |
| 10 |
/** |
| 11 |
* Validates confirmations settings data. |
| 12 |
* |
| 13 |
* @param array $data |
| 14 |
* |
| 15 |
* @return bool |
| 16 |
*/ |
| 17 |
public static function validate($data = []) |
| 18 |
{ |
| 19 |
// Prepare the validation rules & messages. |
| 20 |
[$rules, $messages] = static::validations(); |
| 21 |
|
| 22 |
// Make validator instance. |
| 23 |
$validator = wpFluentForm('validator')->make($data, $rules, $messages); |
| 24 |
|
| 25 |
// Add conditional validations if there's any. |
| 26 |
$validator = static::conditionalValidations($validator); |
| 27 |
|
| 28 |
// Validate and process response. |
| 29 |
if ($validator->validate()->fails()) { |
| 30 |
throw new ValidationException('Unprocessable Entity!', 422, null, $validator->errors()); // phpcs:ignore WordPress.Security.EscapeOutput.ExceptionNotEscaped |
| 31 |
} |
| 32 |
|
| 33 |
return true; |
| 34 |
} |
| 35 |
|
| 36 |
/** |
| 37 |
* Produce the necessary validation rules and corresponding messages |
| 38 |
* |
| 39 |
* @return array |
| 40 |
*/ |
| 41 |
public static function validations() |
| 42 |
{ |
| 43 |
return [[], []]; |
| 44 |
} |
| 45 |
|
| 46 |
/** |
| 47 |
* Add conditional validations to the validator. |
| 48 |
* |
| 49 |
* @param \FluentForm\Framework\Validator\Validator $validator |
| 50 |
* |
| 51 |
* @return \FluentForm\Framework\Validator\Validator |
| 52 |
*/ |
| 53 |
public static function conditionalValidations(Validator $validator) |
| 54 |
{ |
| 55 |
return $validator; |
| 56 |
} |
| 57 |
} |
| 58 |
|