Skip to content

Commit

Permalink
Overall improvements
Browse files Browse the repository at this point in the history
  • Loading branch information
LuckyCyborg committed May 11, 2018
1 parent 2333ee8 commit f578fc6
Show file tree
Hide file tree
Showing 4 changed files with 99 additions and 1 deletion.
2 changes: 1 addition & 1 deletion src/Foundation/Application.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ class Application extends Container implements ResponsePreparerInterface
*
* @var string
*/
const VERSION = '4.0.50';
const VERSION = '4.0.51';

/**
* Indicates if the application has "booted".
Expand Down
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 f578fc6

Please sign in to comment.