-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Update post_code and PESEL rules (#3)
- add changes from newest version
- Loading branch information
1 parent
5edb1a7
commit fa09b45
Showing
8 changed files
with
248 additions
and
35 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
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 |
---|---|---|
|
@@ -3,27 +3,56 @@ | |
namespace PacerIT\LaravelPolishValidationRules\Rules; | ||
|
||
use Illuminate\Contracts\Validation\Rule; | ||
use Illuminate\Support\Arr; | ||
|
||
/** | ||
* Class PESELRule. | ||
* | ||
* @author Wiktor Pacer <[email protected]> | ||
* | ||
* @since 2019-08-12 | ||
*/ | ||
class PESELRule implements Rule | ||
{ | ||
// Gender digit position in PESEL number. | ||
const GENDER_POSITION = 9; | ||
const GENDER_MALE = 0; | ||
const GENDER_FEMALE = 1; | ||
|
||
/** | ||
* Determine if the validation rule passes. | ||
* | ||
* @param string $attribute | ||
* @param mixed $value | ||
* @param array $parameters | ||
* | ||
* @return bool | ||
*/ | ||
public function passes($attribute, $value) | ||
public function passes($attribute, $value, $parameters = []) | ||
{ | ||
return $this->checkPESEL($value); | ||
// First we check if PESEL is valid. If not, there is no need to check other attributes. | ||
if (!$this->checkPESEL($value)) { | ||
return false; | ||
} | ||
|
||
// Get parameters. | ||
$parameters = explode(':', (string) Arr::first($parameters)); | ||
|
||
$result = true; | ||
foreach ($parameters as $mode) { | ||
switch ($mode) { | ||
case 'gender_male': | ||
$result = $this->validateGender($value); | ||
break; | ||
|
||
case 'gender_female': | ||
$result = $this->validateGender($value, self::GENDER_FEMALE); | ||
break; | ||
|
||
default: | ||
return true; | ||
} | ||
} | ||
|
||
return $result; | ||
} | ||
|
||
/** | ||
|
@@ -34,7 +63,6 @@ public function passes($attribute, $value) | |
* @return bool | ||
* | ||
* @see http://phpedia.pl/wiki/PESEL Souce of this algorithm | ||
* @since 2019-08-12 | ||
*/ | ||
private function checkPESEL(?string $string): bool | ||
{ | ||
|
@@ -62,6 +90,30 @@ private function checkPESEL(?string $string): bool | |
return false; | ||
} | ||
|
||
/** | ||
* Validate gender in PESEL number. | ||
* | ||
* @param string $pesel | ||
* @param int $gender | ||
* | ||
* @return bool | ||
*/ | ||
private function validateGender(string $pesel, int $gender = self::GENDER_MALE): bool | ||
{ | ||
$genderFromPesel = $pesel[self::GENDER_POSITION]; | ||
$result = (bool) ($genderFromPesel % 2); | ||
|
||
if ($gender === self::GENDER_MALE && $result) { | ||
return true; | ||
} | ||
|
||
if ($gender === self::GENDER_FEMALE && !$result) { | ||
return true; | ||
} | ||
|
||
return false; | ||
} | ||
|
||
/** | ||
* Get the validation error message. | ||
* | ||
|
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 |
---|---|---|
|
@@ -3,27 +3,43 @@ | |
namespace PacerIT\LaravelPolishValidationRules\Rules; | ||
|
||
use Illuminate\Contracts\Validation\Rule; | ||
use Illuminate\Support\Arr; | ||
|
||
/** | ||
* Class PostCodeRule. | ||
* | ||
* @author Wiktor Pacer <[email protected]> | ||
* | ||
* @since 09/09/2020 | ||
*/ | ||
class PostCodeRule implements Rule | ||
{ | ||
const REGEX_DASH_REQUIRED = '/^[0-9]{2}-[0-9]{3}$/Du'; | ||
const REGEX_DASH_NOT_ALLOWED = '/^[0-9]{5}$/Du'; | ||
const REGEX_DASH_OPTIONAL = '/^[0-9]{2}-?[0-9]{3}$/Du'; | ||
|
||
/** | ||
* Determine if the validation rule passes. | ||
* | ||
* @param string $attribute | ||
* @param mixed $value | ||
* @param array $parameters | ||
* | ||
* @return bool | ||
*/ | ||
public function passes($attribute, $value) | ||
public function passes($attribute, $value, $parameters = []) | ||
{ | ||
return $this->checkPostCode($value); | ||
// Get first parameter. | ||
$mode = (string) Arr::first($parameters); | ||
|
||
switch ($mode) { | ||
case 'with_dash': | ||
return $this->checkPostCode($value, 1); | ||
|
||
case 'without_dash': | ||
return $this->checkPostCode($value, 2); | ||
|
||
default: | ||
return $this->checkPostCode($value); | ||
} | ||
} | ||
|
||
/** | ||
|
@@ -34,20 +50,28 @@ public function passes($attribute, $value) | |
* @return bool | ||
* | ||
* @author Wiktor Pacer <[email protected]> | ||
* | ||
* @since 09/09/2020 | ||
*/ | ||
private function checkPostCode(?string $string): bool | ||
private function checkPostCode(?string $string, int $mode = 0): bool | ||
{ | ||
if ($string === null) { | ||
return false; | ||
} | ||
|
||
if (!preg_match('/^[0-9]{2}-?[0-9]{3}$/Du', $string)) { | ||
return false; | ||
switch ($mode) { | ||
case 1: | ||
$regex = self::REGEX_DASH_REQUIRED; | ||
break; | ||
|
||
case 2: | ||
$regex = self::REGEX_DASH_NOT_ALLOWED; | ||
break; | ||
|
||
default: | ||
$regex = self::REGEX_DASH_OPTIONAL; | ||
break; | ||
} | ||
|
||
return true; | ||
return preg_match($regex, $string); | ||
} | ||
|
||
/** | ||
|
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 |
---|---|---|
|
@@ -3,19 +3,26 @@ | |
namespace Tests\Unit; | ||
|
||
use Illuminate\Contracts\Validation\Rule; | ||
use PHPUnit\Framework\TestCase; | ||
use Orchestra\Testbench\TestCase; | ||
use PacerIT\LaravelPolishValidationRules\Providers\LaravelPolishValidationRulesServiceProvider; | ||
|
||
/** | ||
* Class AbstractRuleTest. | ||
* | ||
* @author Wiktor Pacer <[email protected]> | ||
* | ||
* @since 2019-08-12 | ||
*/ | ||
abstract class AbstractRuleTest extends TestCase | ||
{ | ||
/** | ||
* @var Rule | ||
*/ | ||
protected $rule; | ||
|
||
/** | ||
* @inheritDoc | ||
*/ | ||
protected function getPackageProviders($app) | ||
{ | ||
return [LaravelPolishValidationRulesServiceProvider::class]; | ||
} | ||
} |
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 |
---|---|---|
|
@@ -2,6 +2,7 @@ | |
|
||
namespace Tests\Unit; | ||
|
||
use Illuminate\Support\Facades\Validator; | ||
use PacerIT\LaravelPolishValidationRules\Rules\PESELRule; | ||
|
||
/** | ||
|
@@ -65,4 +66,66 @@ public function testNullPESEL() | |
{ | ||
$this->assertFalse($this->rule->passes('pessel', null)); | ||
} | ||
|
||
/** | ||
* Test PESEL "gender_male" option. | ||
* | ||
* @author Wiktor Pacer <[email protected]> | ||
*/ | ||
public function testGenderMaleOption() | ||
{ | ||
$rules = ['pesel' => 'PESEL:gender_male']; | ||
|
||
$validMalePesel = [ | ||
'07262444973', | ||
'18011233773', | ||
'26121492032', | ||
]; | ||
|
||
// Test for male gender. | ||
foreach ($validMalePesel as $validPesel) { | ||
$data = ['pesel' => $validPesel]; | ||
$validator = Validator::make($data, $rules); | ||
$this->assertFalse($validator->fails()); | ||
} | ||
|
||
$rules = ['pesel' => 'PESEL:gender_female']; | ||
|
||
foreach ($validMalePesel as $validPesel) { | ||
$data = ['pesel' => $validPesel]; | ||
$validator = Validator::make($data, $rules); | ||
$this->assertTrue($validator->fails()); | ||
} | ||
} | ||
|
||
/** | ||
* Test PESEL "gender_female" option. | ||
* | ||
* @author Wiktor Pacer <[email protected]> | ||
*/ | ||
public function testGenderFemaleOption() | ||
{ | ||
$rules = ['pesel' => 'PESEL:gender_female']; | ||
|
||
$validFemalePesel = [ | ||
'08260713768', | ||
'59092028627', | ||
'20083149926', | ||
]; | ||
|
||
// Test for female gender. | ||
foreach ($validFemalePesel as $validPesel) { | ||
$data = ['pesel' => $validPesel]; | ||
$validator = Validator::make($data, $rules); | ||
$this->assertFalse($validator->fails()); | ||
} | ||
|
||
$rules = ['pesel' => 'PESEL:gender_male']; | ||
|
||
foreach ($validFemalePesel as $validPesel) { | ||
$data = ['pesel' => $validPesel]; | ||
$validator = Validator::make($data, $rules); | ||
$this->assertTrue($validator->fails()); | ||
} | ||
} | ||
} |
Oops, something went wrong.