PageRenderTime 52ms CodeModel.GetById 17ms RepoModel.GetById 0ms app.codeStats 0ms

/vendor/laravel/framework/src/Illuminate/Foundation/Http/FormRequest.php

https://gitlab.com/Pasantias/pasantiasASLG
PHP | 227 lines | 97 code | 30 blank | 100 comment | 5 complexity | 1628826d976a8b16fd7909431b5ac6a1 MD5 | raw file
  1. <?php
  2. namespace Illuminate\Foundation\Http;
  3. use Illuminate\Http\Request;
  4. use Illuminate\Http\Response;
  5. use Illuminate\Http\JsonResponse;
  6. use Illuminate\Routing\Redirector;
  7. use Illuminate\Container\Container;
  8. use Illuminate\Contracts\Validation\Validator;
  9. use Illuminate\Http\Exception\HttpResponseException;
  10. use Illuminate\Validation\ValidatesWhenResolvedTrait;
  11. use Illuminate\Contracts\Validation\ValidatesWhenResolved;
  12. use Illuminate\Contracts\Validation\Factory as ValidationFactory;
  13. class FormRequest extends Request implements ValidatesWhenResolved
  14. {
  15. use ValidatesWhenResolvedTrait;
  16. /**
  17. * The container instance.
  18. *
  19. * @var \Illuminate\Container\Container
  20. */
  21. protected $container;
  22. /**
  23. * The redirector instance.
  24. *
  25. * @var \Illuminate\Routing\Redirector
  26. */
  27. protected $redirector;
  28. /**
  29. * The URI to redirect to if validation fails.
  30. *
  31. * @var string
  32. */
  33. protected $redirect;
  34. /**
  35. * The route to redirect to if validation fails.
  36. *
  37. * @var string
  38. */
  39. protected $redirectRoute;
  40. /**
  41. * The controller action to redirect to if validation fails.
  42. *
  43. * @var string
  44. */
  45. protected $redirectAction;
  46. /**
  47. * The key to be used for the view error bag.
  48. *
  49. * @var string
  50. */
  51. protected $errorBag = 'default';
  52. /**
  53. * The input keys that should not be flashed on redirect.
  54. *
  55. * @var array
  56. */
  57. protected $dontFlash = ['password', 'password_confirmation'];
  58. /**
  59. * Get the validator instance for the request.
  60. *
  61. * @return \Illuminate\Contracts\Validation\Validator
  62. */
  63. protected function getValidatorInstance()
  64. {
  65. $factory = $this->container->make(ValidationFactory::class);
  66. if (method_exists($this, 'validator')) {
  67. return $this->container->call([$this, 'validator'], compact('factory'));
  68. }
  69. return $factory->make(
  70. $this->all(), $this->container->call([$this, 'rules']), $this->messages(), $this->attributes()
  71. );
  72. }
  73. /**
  74. * Handle a failed validation attempt.
  75. *
  76. * @param \Illuminate\Contracts\Validation\Validator $validator
  77. * @return mixed
  78. */
  79. protected function failedValidation(Validator $validator)
  80. {
  81. throw new HttpResponseException($this->response(
  82. $this->formatErrors($validator)
  83. ));
  84. }
  85. /**
  86. * Determine if the request passes the authorization check.
  87. *
  88. * @return bool
  89. */
  90. protected function passesAuthorization()
  91. {
  92. if (method_exists($this, 'authorize')) {
  93. return $this->container->call([$this, 'authorize']);
  94. }
  95. return false;
  96. }
  97. /**
  98. * Handle a failed authorization attempt.
  99. *
  100. * @return mixed
  101. */
  102. protected function failedAuthorization()
  103. {
  104. throw new HttpResponseException($this->forbiddenResponse());
  105. }
  106. /**
  107. * Get the proper failed validation response for the request.
  108. *
  109. * @param array $errors
  110. * @return \Symfony\Component\HttpFoundation\Response
  111. */
  112. public function response(array $errors)
  113. {
  114. if ($this->ajax() || $this->wantsJson()) {
  115. return new JsonResponse($errors, 422);
  116. }
  117. return $this->redirector->to($this->getRedirectUrl())
  118. ->withInput($this->except($this->dontFlash))
  119. ->withErrors($errors, $this->errorBag);
  120. }
  121. /**
  122. * Get the response for a forbidden operation.
  123. *
  124. * @return \Illuminate\Http\Response
  125. */
  126. public function forbiddenResponse()
  127. {
  128. return new Response('Forbidden', 403);
  129. }
  130. /**
  131. * Format the errors from the given Validator instance.
  132. *
  133. * @param \Illuminate\Contracts\Validation\Validator $validator
  134. * @return array
  135. */
  136. protected function formatErrors(Validator $validator)
  137. {
  138. return $validator->getMessageBag()->toArray();
  139. }
  140. /**
  141. * Get the URL to redirect to on a validation error.
  142. *
  143. * @return string
  144. */
  145. protected function getRedirectUrl()
  146. {
  147. $url = $this->redirector->getUrlGenerator();
  148. if ($this->redirect) {
  149. return $url->to($this->redirect);
  150. } elseif ($this->redirectRoute) {
  151. return $url->route($this->redirectRoute);
  152. } elseif ($this->redirectAction) {
  153. return $url->action($this->redirectAction);
  154. }
  155. return $url->previous();
  156. }
  157. /**
  158. * Set the Redirector instance.
  159. *
  160. * @param \Illuminate\Routing\Redirector $redirector
  161. * @return \Illuminate\Foundation\Http\FormRequest
  162. */
  163. public function setRedirector(Redirector $redirector)
  164. {
  165. $this->redirector = $redirector;
  166. return $this;
  167. }
  168. /**
  169. * Set the container implementation.
  170. *
  171. * @param \Illuminate\Container\Container $container
  172. * @return $this
  173. */
  174. public function setContainer(Container $container)
  175. {
  176. $this->container = $container;
  177. return $this;
  178. }
  179. /**
  180. * Set custom messages for validator errors.
  181. *
  182. * @return array
  183. */
  184. public function messages()
  185. {
  186. return [];
  187. }
  188. /**
  189. * Set custom attributes for validator errors.
  190. *
  191. * @return array
  192. */
  193. public function attributes()
  194. {
  195. return [];
  196. }
  197. }