File tree Expand file tree Collapse file tree
tests/Geography/Address/ByCountry/Us Expand file tree Collapse file tree Original file line number Diff line number Diff line change 55namespace Talentify \ValueObject \Geography \Address \ByCountry \Us ;
66
77use Talentify \ValueObject \Geography \Address \PostalCode ;
8+ use Talentify \ValueObject \StringUtils ;
89
910/**
1011 * @see https://en.wikipedia.org/wiki/ZIP_Code
1112 */
1213class ZipCode extends PostalCode
1314{
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+ }
1432}
Original file line number Diff line number Diff line change @@ -24,7 +24,7 @@ public function __construct(string $value)
2424 $ this ->setValue ($ value );
2525 }
2626
27- private function setValue (string $ value ) : void
27+ public function setValue (string $ value ) : void
2828 {
2929 $ normalized = StringUtils::trimSpacesWisely ($ value );
3030 if (empty ($ normalized )) {
Original file line number Diff line number Diff line change @@ -49,4 +49,9 @@ public static function convertCaseToLower(string $value) : string
4949 {
5050 return mb_convert_case ($ value , MB_CASE_LOWER , 'UTF-8 ' );
5151 }
52+
53+ public static function countCharacters (string $ value ) : int
54+ {
55+ return strlen ($ value );
56+ }
5257}
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