← All changes
|
vendor/wpfluent/framework/src/WPFluent/Foundation/RequestGuard.php
+38
-10
1.21
→
2.1.0
View file →
| @@ -4,11 +4,20 @@ | ||
| 4 | 4 | |
| 5 | 5 | use FluentBoards\Framework\Foundation\App; |
| 6 | 6 | use FluentBoards\Framework\Validator\ValidationException; |
| 7 | 7 | |
| 8 | +/** | |
| 9 | + * @property \FluentBoards\Framework\Http\Request\Request $request | |
| 10 | + */ | |
| 8 | 11 | abstract class RequestGuard |
| 9 | 12 | { |
| 10 | 13 | /** |
| 14 | + * The request instance. | |
| 15 | + * @var \FluentBoards\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 FluentBoards\Framework\Validator\ValidationException | |
| 61 | + * @throws \FluentBoards\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() |
| @@ -97,22 +104,22 @@ | ||
| 97 | 104 | |
| 98 | 105 | /** |
| 99 | 106 | * Handles validation including before and after calls |
| 100 | 107 | * |
| 101 | - * @throws FluentBoards\Framework\Validator\ValidationException | |
| 102 | - * @return null | |
| 108 | + * @throws \FluentBoards\Framework\Validator\ValidationException | |
| 109 | + * @return void | |
| 103 | 110 | */ |
| 104 | 111 | public static function applyValidation() |
| 105 | 112 | { |
| 106 | 113 | $instance = new static; |
| 107 | 114 | |
| 108 | - $request = App::getInstance('request'); | |
| 115 | + $request = App::make('request'); | |
| 109 | 116 | |
| 110 | 117 | $request->merge($instance->beforeValidation()); |
| 111 | 118 | |
| 112 | 119 | $instance->validate(); |
| 113 | 120 | |
| 114 | - $request->merge($instance->afterValidation()); | |
| 121 | + $request->merge($instance->afterValidation(App::make('validator'))); | |
| 115 | 122 | } |
| 116 | 123 | |
| 117 | 124 | /** |
| 118 | 125 | * Get an input element from the request. |
| @@ -125,8 +132,29 @@ | ||
| 125 | 132 | return $this->get($key); |
| 126 | 133 | } |
| 127 | 134 | |
| 128 | 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 \FluentBoards\Framework\Http\Request\Request $request | |
| 150 | + */ | |
| 151 | + public function setRequestInstance($request) | |
| 152 | + { | |
| 153 | + $this->request = $request; | |
| 154 | + } | |
| 155 | + | |
| 156 | + /** | |
| 129 | 157 | * Handle the dynamic method calls |
| 130 | 158 | * @param string $method |
| 131 | 159 | * @param array $params |
| 132 | 160 | * @return mixed |
| @@ -133,8 +161,8 @@ | ||
| 133 | 161 | */ |
| 134 | 162 | public function __call($method, $params) |
| 135 | 163 | { |
| 136 | 164 | return call_user_func_array( |
| 137 | - [App::make('request'), $method], $params | |
| 165 | + [$this->request, $method], $params | |
| 138 | 166 | ); |
| 139 | 167 | } |
| 140 | 168 | } |