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