Skip to content

Commit cbcce5b

Browse files
Merge pull request #26 from Talentify/fix/add-zipcode-validation
add validation on zipcode
2 parents 3d849ec + 6a5b5c8 commit cbcce5b

File tree

4 files changed

+74
-1
lines changed

4 files changed

+74
-1
lines changed

src/Geography/Address/ByCountry/Us/ZipCode.php

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,28 @@
55
namespace Talentify\ValueObject\Geography\Address\ByCountry\Us;
66

77
use Talentify\ValueObject\Geography\Address\PostalCode;
8+
use Talentify\ValueObject\StringUtils;
89

910
/**
1011
* @see https://en.wikipedia.org/wiki/ZIP_Code
1112
*/
1213
class 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
}

src/Geography/Address/PostalCode.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff 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)) {

src/StringUtils.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff 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
}
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
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+
}

0 commit comments

Comments
 (0)