| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108 |
- <?php
- namespace Illuminate\Validation;
- use Illuminate\Foundation\Precognition;
- /**
- * Provides default implementation of ValidatesWhenResolved contract.
- */
- trait ValidatesWhenResolvedTrait
- {
- /**
- * Validate the class instance.
- *
- * @return void
- */
- public function validateResolved()
- {
- $this->prepareForValidation();
- if (! $this->passesAuthorization()) {
- $this->failedAuthorization();
- }
- $instance = $this->getValidatorInstance();
- if ($this->isPrecognitive()) {
- $instance->after(Precognition::afterValidationHook($this));
- }
- if ($instance->fails()) {
- $this->failedValidation($instance);
- }
- $this->passedValidation();
- }
- /**
- * Prepare the data for validation.
- *
- * @return void
- */
- protected function prepareForValidation()
- {
- //
- }
- /**
- * Get the validator instance for the request.
- *
- * @return \Illuminate\Validation\Validator
- */
- protected function getValidatorInstance()
- {
- return $this->validator();
- }
- /**
- * Handle a passed validation attempt.
- *
- * @return void
- */
- protected function passedValidation()
- {
- //
- }
- /**
- * Handle a failed validation attempt.
- *
- * @param \Illuminate\Validation\Validator $validator
- * @return void
- *
- * @throws \Illuminate\Validation\ValidationException
- */
- protected function failedValidation(Validator $validator)
- {
- $exception = $validator->getException();
- throw new $exception($validator);
- }
- /**
- * Determine if the request passes the authorization check.
- *
- * @return bool
- */
- protected function passesAuthorization()
- {
- if (method_exists($this, 'authorize')) {
- return $this->authorize();
- }
- return true;
- }
- /**
- * Handle a failed authorization attempt.
- *
- * @return void
- *
- * @throws \Illuminate\Validation\UnauthorizedException
- */
- protected function failedAuthorization()
- {
- throw new UnauthorizedException;
- }
- }
|