123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280 |
- <?php
- namespace Dingo\Api\Http;
- use Illuminate\Http\Request;
- use Illuminate\Http\JsonResponse;
- use Illuminate\Contracts\Container\Container;
- use Illuminate\Contracts\Validation\Validator;
- use Dingo\Api\Exception\ValidationHttpException;
- use Illuminate\Validation\ValidatesWhenResolvedTrait;
- use Symfony\Component\HttpKernel\Exception\HttpException;
- use Illuminate\Contracts\Validation\ValidatesWhenResolved;
- use Illuminate\Contracts\Validation\Factory as ValidationFactory;
- use Symfony\Component\HttpKernel\Exception\AccessDeniedHttpException;
- class BaRequest extends Request implements ValidatesWhenResolved
- {
- use ValidatesWhenResolvedTrait;
- /**
- * The container instance.
- *
- * @var \Illuminate\Contracts\Container\Container
- */
- protected $container;
- /**
- * The redirector instance.
- *
- * @var \Illuminate\Routing\Redirector
- */
- protected $redirector;
- /**
- * The URI to redirect to if validation fails.
- *
- * @var string
- */
- protected $redirect;
- /**
- * The route to redirect to if validation fails.
- *
- * @var string
- */
- protected $redirectRoute;
- /**
- * The controller action to redirect to if validation fails.
- *
- * @var string
- */
- protected $redirectAction;
- /**
- * The key to be used for the view error bag.
- *
- * @var string
- */
- protected $errorBag = 'default';
- /**
- * The input keys that should not be flashed on redirect.
- *
- * @var array
- */
- protected $dontFlash = [
- 'password',
- 'password_confirmation',
- ];
- /**
- * Validate the request.
- */
- public function validate()
- {
- if ($this->authorize() === false) {
- throw new AccessDeniedHttpException();
- }
- $validator = app('validator')->make($this->all(), $this->rules(), $this->messages());
- if ($validator->fails()) {
- throw new ValidationHttpException($validator->errors());
- }
- }
- /**
- * Get the validator instance for the request.
- *
- * @return \Illuminate\Contracts\Validation\Validator
- *
- * @SuppressWarnings(PHPMD.ElseExpression)
- */
- protected function getValidatorInstance()
- {
- $factory = $this->container->make(ValidationFactory::class);
- if (method_exists($this, 'validator')) {
- $validator = $this->container->call([$this, 'validator'], compact('factory'));
- } else {
- $validator = $this->createDefaultValidator($factory);
- }
- if (method_exists($this, 'withValidator')) {
- $this->withValidator($validator);
- }
- return $validator;
- }
- /**
- * Create the default validator instance.
- *
- * @param \Illuminate\Contracts\Validation\Factory $factory
- *
- * @return \Illuminate\Contracts\Validation\Validator
- */
- protected function createDefaultValidator(ValidationFactory $factory)
- {
- return $factory->make(
- $this->validationData(),
- $this->container->call([$this, 'rules']),
- $this->messages(),
- $this->attributes()
- );
- }
- /**
- * Get data to be validated from the request.
- *
- * @return array
- */
- protected function validationData()
- {
- return $this->all();
- }
- /**
- * Handle a failed validation attempt.
- *
- * @param \Illuminate\Contracts\Validation\Validator $validator
- *
- * @return void
- */
- protected function failedValidation(Validator $validator)
- {
- if ($this->container['request'] instanceof Request) {
- throw new ValidationHttpException($validator->errors());
- }
- parent::failedValidation($validator);
- }
- /**
- * Get the proper failed validation response for the request.
- *
- * @param array $errors
- *
- * @return \Symfony\Component\HttpFoundation\Response
- */
- public function response(array $errors)
- {
- if ($this->expectsJson()) {
- return new JsonResponse($errors, 422);
- }
- return $this->redirector->to($this->getRedirectUrl())
- ->withInput($this->except($this->dontFlash))
- ->withErrors($errors, $this->errorBag);
- }
- /**
- * Format the errors from the given Validator instance.
- *
- * @param \Illuminate\Contracts\Validation\Validator $validator
- *
- * @return array
- */
- protected function formatErrors(Validator $validator)
- {
- return $validator->getMessageBag()->toArray();
- }
- /**
- * Get the URL to redirect to on a validation error.
- *
- * @return string
- */
- protected function getRedirectUrl()
- {
- $url = $this->redirector->getUrlGenerator();
- if ($this->redirect) {
- return $url->to($this->redirect);
- } elseif ($this->redirectRoute) {
- return $url->route($this->redirectRoute);
- } elseif ($this->redirectAction) {
- return $url->action($this->redirectAction);
- }
- return $url->previous();
- }
- /**
- * Determine if the request passes the authorization check.
- *
- * @return bool
- */
- protected function passesAuthorization()
- {
- if (method_exists($this, 'authorize')) {
- return $this->container->call([$this, 'authorize']);
- }
- return false;
- }
- /**
- * Handle a failed authorization attempt.
- *
- * @return void
- */
- protected function failedAuthorization()
- {
- if ($this->container['request'] instanceof Request) {
- throw new HttpException(403);
- }
- parent::failedAuthorization();
- }
- /**
- * Get custom messages for validator errors.
- *
- * @return array
- */
- public function messages()
- {
- return [];
- }
- /**
- * Get custom attributes for validator errors.
- *
- * @return array
- */
- public function attributes()
- {
- return [];
- }
- /**
- * Set the Redirector instance.
- *
- * @param \Laravel\Lumen\Http\Redirector|\Illuminate\Routing\Redirector $redirector
- *
- * @return $this
- */
- public function setRedirector($redirector)
- {
- $this->redirector = $redirector;
- return $this;
- }
- /**
- * Set the container implementation.
- *
- * @param \Illuminate\Contracts\Container\Container $container
- *
- * @return $this
- */
- public function setContainer(Container $container)
- {
- $this->container = $container;
- return $this;
- }
- }
|