File tree Expand file tree Collapse file tree 4 files changed +74
-1
lines changed
tests/Geography/Address/ByCountry/Us Expand file tree Collapse file tree 4 files changed +74
-1
lines changed Original file line number Diff line number Diff line change 5
5
namespace Talentify \ValueObject \Geography \Address \ByCountry \Us ;
6
6
7
7
use Talentify \ValueObject \Geography \Address \PostalCode ;
8
+ use Talentify \ValueObject \StringUtils ;
8
9
9
10
/**
10
11
* @see https://en.wikipedia.org/wiki/ZIP_Code
11
12
*/
12
13
class ZipCode extends PostalCode
13
14
{
15
+ public function setValue (string $ value ): void
16
+ {
17
+ $ value = StringUtils::trimSpacesWisely ($ value );
18
+ $ changedValue = StringUtils::removeNonWordCharacters ($ value );
19
+ $ characters = StringUtils::countCharacters ($ changedValue );
20
+
21
+ if ($ characters == 4 ) {
22
+ $ value = '0 ' . $ value ;
23
+ $ characters ++;
24
+ }
25
+
26
+ if (empty ($ value ) || $ characters != 5 && $ characters != 9 ) {
27
+ throw new \InvalidArgumentException (sprintf ('The value "%s" is not a valid postal code. ' , $ value ));
28
+ }
29
+
30
+ parent ::setValue ($ value );
31
+ }
14
32
}
Original file line number Diff line number Diff line change @@ -24,7 +24,7 @@ public function __construct(string $value)
24
24
$ this ->setValue ($ value );
25
25
}
26
26
27
- private function setValue (string $ value ) : void
27
+ public function setValue (string $ value ) : void
28
28
{
29
29
$ normalized = StringUtils::trimSpacesWisely ($ value );
30
30
if (empty ($ normalized )) {
Original file line number Diff line number Diff line change @@ -49,4 +49,9 @@ public static function convertCaseToLower(string $value) : string
49
49
{
50
50
return mb_convert_case ($ value , MB_CASE_LOWER , 'UTF-8 ' );
51
51
}
52
+
53
+ public static function countCharacters (string $ value ) : int
54
+ {
55
+ return strlen ($ value );
56
+ }
52
57
}
Original file line number Diff line number Diff line change
1
+ <?php
2
+
3
+ namespace Talentify \ValueObject \Geography \Address \ByCountry \Us ;
4
+
5
+ use Talentify \ValueObject \ValueObjectTestCase ;
6
+
7
+ class ZipCodeTest extends ValueObjectTestCase
8
+ {
9
+
10
+ public function testAddZeroToTheLeftInteger () : void
11
+ {
12
+ $ zipcode = new ZipCode ('1234 ' );
13
+
14
+ $ this ->assertSame ('01234 ' , $ zipcode ->getValue ());
15
+ }
16
+
17
+ public static function getClassName () : string
18
+ {
19
+ return ZipCode::class;
20
+ }
21
+
22
+ public function sameValueDataProvider () : array
23
+ {
24
+ return [
25
+ ["55416 \n" , '55416 ' ],
26
+ ["55416 \r" , '55416 ' ],
27
+ ["55416 \t" , '55416 ' ],
28
+ ["55416 \r\n" , '55416 ' ],
29
+ ["554 \t16 " , '55416 ' ],
30
+ ['55416 ' , '55416 ' ],
31
+ ['99750-0077 ' , '99750-0077 ' ]
32
+ ];
33
+ }
34
+
35
+ public function differentValueDataProvider () : array
36
+ {
37
+ return [
38
+ ['55416 ' , '12345 ' ],
39
+ ];
40
+ }
41
+
42
+ public function invalidValueDataProvider () : array
43
+ {
44
+ return [
45
+ ['' , 'The value "" is not a valid postal code. ' ],
46
+ ["\t\r\n" , "The value \"\t\r\n\" is not a valid postal code. " ],
47
+ ['235-895 ' , 'The value "235895" is not a valid postal code. ' ]
48
+ ];
49
+ }
50
+ }
You can’t perform that action at this time.
0 commit comments