| 1 |
<?php |
| 2 |
|
| 3 |
namespace FluentBooking\Framework\Foundation; |
| 4 |
|
| 5 |
use FluentBooking\Framework\Foundation\App; |
| 6 |
use FluentBooking\Framework\Validator\ValidationException; |
| 7 |
|
| 8 |
abstract class RequestGuard |
| 9 |
{ |
| 10 |
/** |
| 11 |
* Retrive the validation rules |
| 12 |
* @return array |
| 13 |
*/ |
| 14 |
public function rules() |
| 15 |
{ |
| 16 |
return []; |
| 17 |
} |
| 18 |
|
| 19 |
/** |
| 20 |
* Retrive the validation messages set by the developer |
| 21 |
* @return array |
| 22 |
*/ |
| 23 |
public function messages() |
| 24 |
{ |
| 25 |
return []; |
| 26 |
} |
| 27 |
|
| 28 |
/** |
| 29 |
* Allow the developer tinker with data before the validation. |
| 30 |
* @return array |
| 31 |
*/ |
| 32 |
public function beforeValidation() |
| 33 |
{ |
| 34 |
return []; |
| 35 |
} |
| 36 |
|
| 37 |
/** |
| 38 |
* Allow the developer tinker with data after the validation. |
| 39 |
* @return array |
| 40 |
*/ |
| 41 |
public function afterValidation($validator) |
| 42 |
{ |
| 43 |
return []; |
| 44 |
} |
| 45 |
|
| 46 |
/** |
| 47 |
* Validate the request. |
| 48 |
* |
| 49 |
* @param array $rules Optional |
| 50 |
* @param array $messages Optional |
| 51 |
* @return array Request Data |
| 52 |
* @throws FluentBooking\Framework\Validator\ValidationException |
| 53 |
*/ |
| 54 |
public function validate($rules = [], $messages = []) |
| 55 |
{ |
| 56 |
try { |
| 57 |
return App::make('request')->validate( |
| 58 |
$rules ?: (array) $this->rules(), |
| 59 |
$messages ?: (array) $this->messages() |
| 60 |
); |
| 61 |
} catch (ValidationException $e) { |
| 62 |
|
| 63 |
$validator = App::make('validator'); |
| 64 |
|
| 65 |
$validator->addError($e->errors()); |
| 66 |
|
| 67 |
$after = $this->afterValidation($validator); |
| 68 |
|
| 69 |
if (!($errors = $validator->errors())) { |
| 70 |
return; |
| 71 |
} |
| 72 |
|
| 73 |
$e = new ValidationException( |
| 74 |
'Unprocessable Entity!', 422, null, $e->errors() |
| 75 |
); |
| 76 |
|
| 77 |
if ($this->shouldThrowException()) { |
| 78 |
throw $e; |
| 79 |
} else { |
| 80 |
App::make()->doCustomAction('handle_exception', $e); |
| 81 |
} |
| 82 |
} |
| 83 |
} |
| 84 |
|
| 85 |
/** |
| 86 |
* Check if exceptions should be thrown. |
| 87 |
* |
| 88 |
* @return bool |
| 89 |
*/ |
| 90 |
protected function shouldThrowException() |
| 91 |
{ |
| 92 |
$isTrue = $this->isRest(); |
| 93 |
$isTrue = $isTrue || str_contains('test', App::make()->env()); |
| 94 |
$isTrue = $isTrue || str_contains(strtolower(php_sapi_name()), 'cli'); |
| 95 |
return $isTrue; |
| 96 |
} |
| 97 |
|
| 98 |
/** |
| 99 |
* Handles validation including before and after calls |
| 100 |
* |
| 101 |
* @throws FluentBooking\Framework\Validator\ValidationException |
| 102 |
* @return null |
| 103 |
*/ |
| 104 |
public static function applyValidation() |
| 105 |
{ |
| 106 |
$instance = new static; |
| 107 |
|
| 108 |
$request = App::getInstance('request'); |
| 109 |
|
| 110 |
$request->merge($instance->beforeValidation()); |
| 111 |
|
| 112 |
$instance->validate(); |
| 113 |
|
| 114 |
$request->merge($instance->afterValidation()); |
| 115 |
} |
| 116 |
|
| 117 |
/** |
| 118 |
* Get an input element from the request. |
| 119 |
* |
| 120 |
* @param string $key |
| 121 |
* @return mixed |
| 122 |
*/ |
| 123 |
public function __get($key) |
| 124 |
{ |
| 125 |
return $this->get($key); |
| 126 |
} |
| 127 |
|
| 128 |
/** |
| 129 |
* Handle the dynamic method calls |
| 130 |
* @param string $method |
| 131 |
* @param array $params |
| 132 |
* @return mixed |
| 133 |
*/ |
| 134 |
public function __call($method, $params) |
| 135 |
{ |
| 136 |
return call_user_func_array( |
| 137 |
[App::make('request'), $method], $params |
| 138 |
); |
| 139 |
} |
| 140 |
} |
| 141 |
|