Skip to content

Commit

Permalink
Add zipcode validator as a shortcort to regexp validator.
Browse files Browse the repository at this point in the history
  • Loading branch information
tdphillipsjr committed Jun 1, 2014
1 parent 3d61f08 commit a71dabf
Show file tree
Hide file tree
Showing 3 changed files with 90 additions and 2 deletions.
8 changes: 6 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -193,8 +193,12 @@ the function $validator->validateObject(Validatable) may be used.
###UrlValidator
- usage: "url"
- behavior: This a validly formatted URL. Not RFC inclusive but should check most common types.

###ZipcodeValidator
- usage: "zipcode"
- behavior: Validate zipcodes in the format of "12345", "12345-6789", or "123456789". This validates only US zipcodes.
- TODO: Fix such that a country code can be accepted as an argument; build in this support. US as default.

##TODO
- Other validators?
- Database object injection
- Unique Value Validator: Requires database object injection

30 changes: 30 additions & 0 deletions src/Tdphillipsjr/Validator/ZipcodeValidator.php
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);
}
}
54 changes: 54 additions & 0 deletions tests/Tdphillipsjr/Validator/ZipcodeValidatorTest.php
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());
}
}
?>

0 comments on commit a71dabf

Please sign in to comment.