-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
* Create Validation Hashed Exist * Apply fixes from StyleCI (#18) Co-authored-by: Emad Rashdan <[email protected]> * Update readme.md Co-authored-by: Emad Rashdan <[email protected]>
- Loading branch information
Showing
10 changed files
with
145 additions
and
17 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
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,10 @@ | ||
<?php | ||
|
||
namespace Erashdan\Hashid\Exceptions; | ||
|
||
use Exception; | ||
|
||
class UndefinedKey extends Exception | ||
{ | ||
protected $message = 'Unable to define hashing key'; | ||
} |
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,10 @@ | ||
<?php | ||
|
||
namespace Erashdan\Hashid\Exceptions; | ||
|
||
use Exception; | ||
|
||
class UndefinedLength extends Exception | ||
{ | ||
protected $message = 'Unable to define hashing length'; | ||
} |
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
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,15 @@ | ||
<?php | ||
|
||
namespace Erashdan\Hashid; | ||
|
||
class HashedIdValidator | ||
{ | ||
public function validate($attribute, $value, $parameters, $validator) | ||
{ | ||
if (! isset($parameters[0])) { | ||
return false; | ||
} | ||
|
||
return $parameters[0]::FindHashed($value) !== null; | ||
} | ||
} |
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
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
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
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
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,38 @@ | ||
<?php | ||
|
||
namespace Erashdan\Hashid\Test; | ||
|
||
use Erashdan\Hashid\HashedIdValidator; | ||
use Illuminate\Support\Facades\Validator; | ||
|
||
class ValidationTest extends TestCase | ||
{ | ||
/** @test * */ | ||
public function it_can_validate_if_hashed_id_exist_on_database_using_model_base() | ||
{ | ||
$this->registerValidator(); | ||
|
||
$rules = [ | ||
'key' => 'hashed_exists:'.TestModel::class, | ||
]; | ||
|
||
$invalid_data = [ | ||
'key' => 'ANY KEY', | ||
]; | ||
|
||
$valid_data = [ | ||
'key' => 'N5zQE4', | ||
]; | ||
|
||
$failed_validation = $this->app['validator']->make($invalid_data, $rules); | ||
$this->assertFalse($failed_validation->passes()); | ||
|
||
$success_validation = $this->app['validator']->make($valid_data, $rules); | ||
$this->assertTrue($success_validation->passes()); | ||
} | ||
|
||
protected function registerValidator() | ||
{ | ||
Validator::extend('hashed_exists', HashedIdValidator::class.'@validate', trans('The selected :attribute is invalid.')); | ||
} | ||
} |