Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/4.0' into 4.0
Browse files Browse the repository at this point in the history
  • Loading branch information
LuckyCyborg committed May 11, 2018
2 parents 13b1c27 + f578fc6 commit 86a6454
Show file tree
Hide file tree
Showing 3 changed files with 98 additions and 0 deletions.
11 changes: 11 additions & 0 deletions src/Validation/UnauthorizedException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?php

namespace Nova\Validation;

use RuntimeException;


class AuthorizationException extends RuntimeException
{
//
}
14 changes: 14 additions & 0 deletions src/Validation/ValidatesWhenResolvedInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<?php

namespace Nova\Validation;


interface ValidatesWhenResolvedInterface
{
/**
* Validate the given class instance.
*
* @return void
*/
public function validate();
}
73 changes: 73 additions & 0 deletions src/Validation/ValidatesWhenResolvedTrait.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
<?php

namespace Nova\Validation;

use Nova\Validation\ValidationException;
use Nova\Validation\UnauthorizedException;

/**
* Provides default implementation of ValidatesWhenResolvedInterface.
*/
trait ValidatesWhenResolvedTrait
{
/**
* Validate the class instance.
*
* @return void
*/
public function validate()
{
$instance = $this->getValidatorInstance();

if (! $this->passesAuthorization()) {
$this->failedAuthorization();
} else if (! $instance->passes()) {
$this->failedValidation($instance);
}
}

/**
* Get the validator instance for the request.
*
* @return \Nova\Validation\Validator
*/
protected function getValidatorInstance()
{
return $this->validator();
}

/**
* Handle a failed validation attempt.
*
* @param \Nova\Validation\Validator $validator
* @return mixed
*/
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 mixed
*/
protected function failedAuthorization()
{
throw new UnauthorizedException;
}
}

0 comments on commit 86a6454

Please sign in to comment.