File tree Expand file tree Collapse file tree 7 files changed +161
-16
lines changed Expand file tree Collapse file tree 7 files changed +161
-16
lines changed Original file line number Diff line number Diff line change 11# Changelog
2+ ## v.4.0.1
3+ - add post_code rule
4+ ## v.4.0.0
5+ - update dependencies for Laravel and Lumen 8.x.x compatibility
26## v.3.0.0
37 - update dependencies for Laravel and Lumen 7.x.x compatibility
48## v.2.0.2
Original file line number Diff line number Diff line change @@ -19,26 +19,27 @@ For customize validaiton messages run:
1919
2020### Version compatibility
2121#### Laravel
22- Framework | Package
23- :---------|:--------
24- 5.8.x | ^1.x.x
25- 6.0.x | ^2.x.x
26- 7.x.x | ^3.x.x
27- 8.x.x | ^4.x.x
22+ Framework | Package | Note
23+ :---------|:--------|:------
24+ 5.8.x | ^1.x.x | No longer maintained.
25+ 6.0.x | ^2.x.x |
26+ 7.x.x | ^3.x.x |
27+ 8.x.x | ^4.x.x |
2828#### Lumen
29- Framework | Package
30- :---------|:--------
31- 5.8.x | ^1.x.x
32- 6.0.x | ^2.x.x
33- 7.x.x | ^3.x.x
34- 8.x.x | ^4.x.x
29+ Framework | Package | Note
30+ :---------|:--------|:------
31+ 5.8.x | ^1.x.x | No longer maintained.
32+ 6.0.x | ^2.x.x |
33+ 7.x.x | ^3.x.x |
34+ 8.x.x | ^4.x.x |
3535
3636## Rules
3737
38381 . 'PESEL' - validate [ PESEL] ( https://pl.wikipedia.org/wiki/PESEL ) number
39392 . 'REGON' - validate [ REGON] ( https://pl.wikipedia.org/wiki/REGON ) number
40403 . 'NIP' - validate [ NIP] ( https://pl.wikipedia.org/wiki/NIP ) number
41414 . 'id_card_number' - validate Polish ID Card number
42+ 5 . 'post_code' - validate Polish post codes (accept codes in format 00-000 and 00000),
4243
4344## Code Authors
4445
Original file line number Diff line number Diff line change 66 'REGON ' => 'REGON number is not valid! ' ,
77 'NIP ' => 'NIP number is not valid! ' ,
88 'id_card_number ' => 'ID Card numner is not valid! ' ,
9+ 'post_code ' => 'Post code not valid! ' ,
910
1011];
Original file line number Diff line number Diff line change 66 'REGON ' => 'Numer REGON jest niepoprawny ' ,
77 'NIP ' => 'Numer NIP jest niepoprawny! ' ,
88 'id_card_number ' => 'Numer dowodu osobistego jest niepoprawny! ' ,
9+ 'post_code ' => 'Kod pocztowy niepoprawny! ' ,
910
1011];
Original file line number Diff line number Diff line change 77use PacerIT \LaravelPolishValidationRules \Rules \IDCardNumberRule ;
88use PacerIT \LaravelPolishValidationRules \Rules \NIPRule ;
99use PacerIT \LaravelPolishValidationRules \Rules \PESELRule ;
10+ use PacerIT \LaravelPolishValidationRules \Rules \PostCodeRule ;
1011use PacerIT \LaravelPolishValidationRules \Rules \REGONRule ;
1112
1213/**
@@ -47,22 +48,32 @@ private function registerRules()
4748 {
4849 Validator::extend (
4950 'PESEL ' ,
50- PESELRule::class.'@passes '
51+ PESELRule::class.'@passes ' ,
52+ trans ('polish-validation::validation.PESEL ' )
5153 );
5254
5355 Validator::extend (
5456 'REGON ' ,
55- REGONRule::class.'@passes '
57+ REGONRule::class.'@passes ' ,
58+ trans ('polish-validation::validation.REGON ' )
5659 );
5760
5861 Validator::extend (
5962 'NIP ' ,
60- NIPRule::class.'@passes '
63+ NIPRule::class.'@passes ' ,
64+ trans ('polish-validation::validation.NIP ' )
6165 );
6266
6367 Validator::extend (
6468 'id_card_number ' ,
65- IDCardNumberRule::class.'@passes '
69+ IDCardNumberRule::class.'@passes ' ,
70+ trans ('polish-validation::validation.id_card_number ' )
71+ );
72+
73+ Validator::extend (
74+ 'post_code ' ,
75+ PostCodeRule::class.'@passes ' ,
76+ trans ('polish-validation::validation.post_code ' )
6677 );
6778 }
6879}
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 PostCodeRule.
9+ *
10+ * @author Wiktor Pacer <kontakt@pacerit.pl>
11+ *
12+ * @since 09/09/2020
13+ */
14+ class PostCodeRule 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 ->checkPostCode ($ 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 checkPostCode (?string $ string ): bool
41+ {
42+ if ($ string === null ) {
43+ return false ;
44+ }
45+
46+ if (!preg_match ('/^[0-9]{2}-?[0-9]{3}$/Du ' , $ string )) {
47+ return false ;
48+ }
49+
50+ return true ;
51+ }
52+
53+ /**
54+ * Get the validation error message.
55+ *
56+ * @return string|array
57+ */
58+ public function message ()
59+ {
60+ return trans ('polish-validation::validation.post_code ' );
61+ }
62+ }
Original file line number Diff line number Diff line change 1+ <?php
2+
3+ namespace Tests \Unit ;
4+
5+ use PacerIT \LaravelPolishValidationRules \Rules \PostCodeRule ;
6+
7+ /**
8+ * Class PostCodeRuleTest.
9+ *
10+ * @author Wiktor Pacer <kontakt@pacerit.pl>
11+ *
12+ * @since 2019-08-12
13+ */
14+ class PostCodeRuleTest 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 PostCodeRule ();
27+ }
28+
29+ /**
30+ * Test valid PostCode number.
31+ *
32+ * @author Wiktor Pacer <kontakt@pacerit.pl>
33+ *
34+ * @since 2019-08-12
35+ */
36+ public function testValidPostCode ()
37+ {
38+ $ this ->assertTrue ($ this ->rule ->passes ('post_code ' , '72-200 ' ));
39+ $ this ->assertTrue ($ this ->rule ->passes ('post_code ' , '72-200 ' ));
40+ }
41+
42+ /**
43+ * Test not valid PostCode number.
44+ *
45+ * @author Wiktor Pacer <kontakt@pacerit.pl>
46+ *
47+ * @since 2019-08-12
48+ */
49+ public function testNotValidPostCode ()
50+ {
51+ $ this ->assertFalse ($ this ->rule ->passes ('post_code ' , '7220 ' ));
52+ }
53+
54+ /**
55+ * Test null PostCode number.
56+ *
57+ * @author Wiktor Pacer <kontakt@pacerit.pl>
58+ *
59+ * @since 02/12/2019
60+ */
61+ public function testNullPostCode ()
62+ {
63+ $ this ->assertFalse ($ this ->rule ->passes ('post_code ' , null ));
64+ }
65+ }
You can’t perform that action at this time.
0 commit comments