-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add zipcode validator as a shortcort to regexp validator.
- Loading branch information
1 parent
3d61f08
commit a71dabf
Showing
3 changed files
with
90 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
<?php | ||
|
||
namespace Tdphillipsjr\Validator; | ||
|
||
/** | ||
* Determine if $data is a valid zipcode. This only validates US zipcodes. | ||
* Zipcodes may be 5 digits, 9 digits, or 9 digits with a hyphen. | ||
*/ | ||
class ZipcodeValidator extends BaseValidator | ||
{ | ||
public function __construct($data) | ||
{ | ||
parent::__construct($data, array()); | ||
} | ||
|
||
/** | ||
* This is not an RFC exhaustive validation, but it should match the most commonly | ||
* used url formats. | ||
* | ||
* @return boolean | ||
* @throws Validator\ValidationException | ||
*/ | ||
public function validate() | ||
{ | ||
$pattern = '/^\d{5}([\-]?\d{4})?$/'; | ||
if (!preg_match($pattern, $this->_data)) $this->addError('Zip code is not in a valid US format.'); | ||
|
||
return !sizeof($this->_errors); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
<?php | ||
|
||
namespace Tdphillipsjr\Validator; | ||
|
||
use Tdphillipsjr\Validator\ZipcodeValidator; | ||
|
||
class ZipcodeValidatorTest extends \PHPUnit_Framework_TestCase | ||
{ | ||
public function testFiveDigitPass() | ||
{ | ||
$data = 12345; | ||
$validator = new ZipcodeValidator($data); | ||
$this->assertTrue($validator->validate()); | ||
} | ||
|
||
public function testNineDigitPass() | ||
{ | ||
$data = 123456789; | ||
$validator = new ZipcodeValidator($data); | ||
$this->assertTrue($validator->validate()); | ||
} | ||
|
||
public function testNineDigitHyphenPass() | ||
{ | ||
$data = '12345-6789'; | ||
$validator = new ZipcodeValidator($data); | ||
$this->assertTrue($validator->validate()); | ||
} | ||
|
||
public function testShortFail() | ||
{ | ||
$data = 1234; | ||
$validator = new ZipcodeValidator($data); | ||
$validator->setThrow(false); | ||
$this->assertFalse($validator->validate()); | ||
} | ||
|
||
public function testLetterFail() | ||
{ | ||
$data = '12c45'; | ||
$validator = new ZipcodeValidator($data); | ||
$validator->setThrow(false); | ||
$this->assertFalse($validator->validate()); | ||
} | ||
|
||
public function testBadSymfolFail() | ||
{ | ||
$data = '12345&6789'; | ||
$validator = new ZipcodeValidator($data); | ||
$validator->setThrow(false); | ||
$this->assertFalse($validator->validate()); | ||
} | ||
} | ||
?> |