1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889 |
- <?php
-
- namespace Illuminate\Validation;
-
- /**
- * 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 (! $instance->passes()) {
- $this->failedValidation($instance);
- }
- }
-
- /**
- * Prepare the data for validation.
- *
- * @return void
- */
- protected function prepareForValidation()
- {
- // no default action
- }
-
- /**
- * Get the validator instance for the request.
- *
- * @return \Illuminate\Validation\Validator
- */
- protected function getValidatorInstance()
- {
- return $this->validator();
- }
-
- /**
- * Handle a failed validation attempt.
- *
- * @param \Illuminate\Validation\Validator $validator
- * @return void
- *
- * @throws \Illuminate\Validation\ValidationException
- */
- protected function failedValidation(Validator $validator)
- {
- throw new ValidationException($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;
- }
- }
|