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