File tree Expand file tree Collapse file tree 7 files changed +162
-0
lines changed Expand file tree Collapse file tree 7 files changed +162
-0
lines changed Original file line number Diff line number Diff line change 11# Changelog
2+ ## v.4.0.2
3+ - add PWZ rule
24## v.4.0.1
35 - add post_code rule
46## v.4.0.0
Original file line number Diff line number Diff line change @@ -40,6 +40,7 @@ Framework | Package | Note
40403 . 'NIP' - validate [ NIP] ( https://pl.wikipedia.org/wiki/NIP ) number
41414 . 'id_card_number' - validate Polish ID Card number
42425 . 'post_code' - validate Polish post codes (accept codes in format 00-000 and 00000),
43+ 6 . 'PWZ' - validate PWZ (Prawo wykonywania zawodu lekarza/farmaceuty) numer (more information [ HERE] ( https://nil.org.pl/rejestry/centralny-rejestr-lekarzy/zasady-weryfikowania-nr-prawa-wykonywania-zawodu ) )
4344
4445## Code Authors
4546
Original file line number Diff line number Diff line change 77 'NIP ' => 'NIP number is not valid! ' ,
88 'id_card_number ' => 'ID Card numner is not valid! ' ,
99 'post_code ' => 'Post code not valid! ' ,
10+ 'PWZ ' => 'PWZ code not valid! ' ,
1011
1112];
Original file line number Diff line number Diff line change 77 'NIP ' => 'Numer NIP jest niepoprawny! ' ,
88 'id_card_number ' => 'Numer dowodu osobistego jest niepoprawny! ' ,
99 'post_code ' => 'Kod pocztowy niepoprawny! ' ,
10+ 'PWZ ' => 'Numer PWZ niepoprawny! ' ,
1011
1112];
Original file line number Diff line number Diff line change 88use PacerIT \LaravelPolishValidationRules \Rules \NIPRule ;
99use PacerIT \LaravelPolishValidationRules \Rules \PESELRule ;
1010use PacerIT \LaravelPolishValidationRules \Rules \PostCodeRule ;
11+ use PacerIT \LaravelPolishValidationRules \Rules \PWZRule ;
1112use PacerIT \LaravelPolishValidationRules \Rules \REGONRule ;
1213
1314/**
@@ -75,5 +76,11 @@ private function registerRules()
7576 PostCodeRule::class.'@passes ' ,
7677 trans ('polish-validation::validation.post_code ' )
7778 );
79+
80+ Validator::extend (
81+ 'PWZ ' ,
82+ PWZRule::class.'@passes ' ,
83+ trans ('polish-validation::validation.PWZ ' )
84+ );
7885 }
7986}
Original file line number Diff line number Diff line change 1+ <?php
2+
3+ namespace PacerIT \LaravelPolishValidationRules \Rules ;
4+
5+ use Illuminate \Contracts \Validation \Rule ;
6+
7+ /**
8+ * Class PWZRule.
9+ *
10+ * @author Wiktor Pacer <kontakt@pacerit.pl>
11+ *
12+ * @since 09/09/2020
13+ */
14+ class PWZRule implements Rule
15+ {
16+ /**
17+ * Determine if the validation rule passes.
18+ *
19+ * @param string $attribute
20+ * @param mixed $value
21+ *
22+ * @return bool
23+ */
24+ public function passes ($ attribute , $ value )
25+ {
26+ return $ this ->checkPWZ ($ value );
27+ }
28+
29+ /**
30+ * Validate post code.
31+ *
32+ * @param string|null $string
33+ *
34+ * @return bool
35+ *
36+ * @author Wiktor Pacer <kontakt@pacerit.pl>
37+ *
38+ * @since 09/09/2020
39+ */
40+ private function checkPWZ (?string $ string ): bool
41+ {
42+ if ($ string === null ) {
43+ return false ;
44+ }
45+
46+ if (strlen ($ string ) !== 7 ) {
47+ return false ;
48+ }
49+
50+ // Get control number.
51+ $ control = (int ) substr ($ string , 0 , 1 );
52+
53+ if ($ control === 0 ) {
54+ return false ;
55+ }
56+
57+ // Calculate control number.
58+ $ controlSum = 0 ;
59+ for ($ x = 1 ; $ x <= 7 ; $ x ++) {
60+ $ controlSum += ((int ) substr ($ string , $ x , 1 ) * $ x );
61+ }
62+
63+ $ calculatedControl = ($ controlSum % 11 );
64+
65+ if ($ calculatedControl !== $ control ) {
66+ return false ;
67+ }
68+
69+ return true ;
70+ }
71+
72+ /**
73+ * Get the validation error message.
74+ *
75+ * @return string|array
76+ */
77+ public function message ()
78+ {
79+ return trans ('polish-validation::validation.PWZ ' );
80+ }
81+ }
Original file line number Diff line number Diff line change 1+ <?php
2+
3+ namespace Tests \Unit ;
4+
5+ use PacerIT \LaravelPolishValidationRules \Rules \PWZRule ;
6+
7+ /**
8+ * Class PWZRuleTest.
9+ *
10+ * @author Wiktor Pacer <kontakt@pacerit.pl>
11+ *
12+ * @since 2019-08-12
13+ */
14+ class PWZRuleTest extends AbstractRuleTest
15+ {
16+ /**
17+ * Set up test.
18+ *
19+ * @author Wiktor Pacer <kontakt@pacerit.pl>
20+ *
21+ * @since 2019-08-12
22+ */
23+ public function setUp (): void
24+ {
25+ parent ::setUp ();
26+ $ this ->rule = new PWZRule ();
27+ }
28+
29+ /**
30+ * Test valid PWZ number.
31+ *
32+ * @author Wiktor Pacer <kontakt@pacerit.pl>
33+ *
34+ * @since 2019-08-12
35+ */
36+ public function testValidPWZ ()
37+ {
38+ $ this ->assertTrue ($ this ->rule ->passes ('PWZ ' , 5425740 ));
39+ }
40+
41+ /**
42+ * Test not valid PWZ number.
43+ *
44+ * @author Wiktor Pacer <kontakt@pacerit.pl>
45+ *
46+ * @since 2019-08-12
47+ */
48+ public function testNotValidPWZ ()
49+ {
50+ // To long.
51+ $ this ->assertFalse ($ this ->rule ->passes ('PWZ ' , 00000000 ));
52+ // Starts with 0.
53+ $ this ->assertFalse ($ this ->rule ->passes ('PWZ ' , 0000000 ));
54+ // Control sum not valid.
55+ $ this ->assertFalse ($ this ->rule ->passes ('PWZ ' , 1000000 ));
56+ }
57+
58+ /**
59+ * Test null PWZ number.
60+ *
61+ * @author Wiktor Pacer <kontakt@pacerit.pl>
62+ *
63+ * @since 02/12/2019
64+ */
65+ public function testNullPWZ ()
66+ {
67+ $ this ->assertFalse ($ this ->rule ->passes ('PWZ ' , null ));
68+ }
69+ }
You can’t perform that action at this time.
0 commit comments