PageRenderTime 50ms CodeModel.GetById 14ms RepoModel.GetById 0ms app.codeStats 0ms

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

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