Skip to content

Commit

Permalink
Merge pull request #26 from Talentify/fix/add-zipcode-validation
Browse files Browse the repository at this point in the history
add validation on zipcode
  • Loading branch information
vivianequinaia committed Mar 17, 2021
2 parents 3d849ec + 6a5b5c8 commit cbcce5b
Show file tree
Hide file tree
Showing 4 changed files with 74 additions and 1 deletion.
18 changes: 18 additions & 0 deletions src/Geography/Address/ByCountry/Us/ZipCode.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,28 @@
namespace Talentify\ValueObject\Geography\Address\ByCountry\Us;

use Talentify\ValueObject\Geography\Address\PostalCode;
use Talentify\ValueObject\StringUtils;

/**
* @see https://en.wikipedia.org/wiki/ZIP_Code
*/
class ZipCode extends PostalCode
{
public function setValue(string $value): void
{
$value = StringUtils::trimSpacesWisely($value);
$changedValue = StringUtils::removeNonWordCharacters($value);
$characters = StringUtils::countCharacters($changedValue);

if ($characters == 4) {
$value = '0' . $value;
$characters++;
}

if (empty($value) || $characters != 5 && $characters != 9) {
throw new \InvalidArgumentException(sprintf('The value "%s" is not a valid postal code.', $value));
}

parent::setValue($value);
}
}
2 changes: 1 addition & 1 deletion src/Geography/Address/PostalCode.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ public function __construct(string $value)
$this->setValue($value);
}

private function setValue(string $value) : void
public function setValue(string $value) : void
{
$normalized = StringUtils::trimSpacesWisely($value);
if (empty($normalized)) {
Expand Down
5 changes: 5 additions & 0 deletions src/StringUtils.php
Original file line number Diff line number Diff line change
Expand Up @@ -49,4 +49,9 @@ public static function convertCaseToLower(string $value) : string
{
return mb_convert_case($value, MB_CASE_LOWER, 'UTF-8');
}

public static function countCharacters(string $value) : int
{
return strlen($value);
}
}
50 changes: 50 additions & 0 deletions tests/Geography/Address/ByCountry/Us/ZipCodeTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
<?php

namespace Talentify\ValueObject\Geography\Address\ByCountry\Us;

use Talentify\ValueObject\ValueObjectTestCase;

class ZipCodeTest extends ValueObjectTestCase
{

public function testAddZeroToTheLeftInteger() : void
{
$zipcode = new ZipCode('1234');

$this->assertSame('01234', $zipcode->getValue());
}

public static function getClassName() : string
{
return ZipCode::class;
}

public function sameValueDataProvider() : array
{
return [
["55416\n", '55416'],
["55416\r", '55416'],
["55416\t", '55416'],
["55416\r\n", '55416'],
["554\t16", '55416'],
['55416', '55416'],
['99750-0077', '99750-0077']
];
}

public function differentValueDataProvider() : array
{
return [
['55416', '12345'],
];
}

public function invalidValueDataProvider() : array
{
return [
['', 'The value "" is not a valid postal code.'],
["\t\r\n", "The value \"\t\r\n\" is not a valid postal code."],
['235-895', 'The value "235895" is not a valid postal code.']
];
}
}

0 comments on commit cbcce5b

Please sign in to comment.