| 1 |
<?php |
| 2 |
|
| 3 |
namespace FluentSupport\Framework\Foundation; |
| 4 |
|
| 5 |
use FluentSupport\Framework\Foundation\App; |
| 6 |
use FluentSupport\Framework\Validator\Validator; |
| 7 |
use FluentSupport\Framework\Validator\ValidationException; |
| 8 |
|
| 9 |
abstract class RequestGuard |
| 10 |
{ |
| 11 |
public function rules() |
| 12 |
{ |
| 13 |
return []; |
| 14 |
} |
| 15 |
|
| 16 |
public function messages() |
| 17 |
{ |
| 18 |
return []; |
| 19 |
} |
| 20 |
|
| 21 |
public function validate(Validator $validator) |
| 22 |
{ |
| 23 |
try { |
| 24 |
$rules = (array) $this->rules(); |
| 25 |
|
| 26 |
if (!$rules) return; |
| 27 |
|
| 28 |
$validator = $validator->make($this->all(), $rules, (array) $this->messages()); |
| 29 |
|
| 30 |
if ($validator->validate()->fails()) { |
| 31 |
throw new ValidationException('Unprocessable Entity!', 422, null, $validator->errors()); |
| 32 |
} |
| 33 |
} catch (ValidationException $e) { |
| 34 |
|
| 35 |
if (defined('REST_REQUEST') && REST_REQUEST) { |
| 36 |
throw $e; |
| 37 |
} else { |
| 38 |
App::getInstance()->doCustomAction('handle_exception', $e); |
| 39 |
} |
| 40 |
} |
| 41 |
} |
| 42 |
|
| 43 |
/** |
| 44 |
* Get an input element from the request. |
| 45 |
* |
| 46 |
* @param string $key |
| 47 |
* @return mixed |
| 48 |
*/ |
| 49 |
public function __get($key) |
| 50 |
{ |
| 51 |
return $this->get($key); |
| 52 |
} |
| 53 |
|
| 54 |
public function __call($method, $params) |
| 55 |
{ |
| 56 |
return call_user_func_array( |
| 57 |
[App::getInstance('request'), $method], $params |
| 58 |
); |
| 59 |
} |
| 60 |
} |
| 61 |
|