-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge remote-tracking branch 'origin/4.0' into 4.0
- Loading branch information
Showing
3 changed files
with
98 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
<?php | ||
|
||
namespace Nova\Validation; | ||
|
||
use RuntimeException; | ||
|
||
|
||
class AuthorizationException extends RuntimeException | ||
{ | ||
// | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |