From a71dabf43373c0bec646935bb63ad438daccff9a Mon Sep 17 00:00:00 2001 From: Tom P Date: Sun, 1 Jun 2014 17:48:33 -0400 Subject: [PATCH] Add zipcode validator as a shortcort to regexp validator. --- README.md | 8 ++- .../Validator/ZipcodeValidator.php | 30 +++++++++++ .../Validator/ZipcodeValidatorTest.php | 54 +++++++++++++++++++ 3 files changed, 90 insertions(+), 2 deletions(-) create mode 100644 src/Tdphillipsjr/Validator/ZipcodeValidator.php create mode 100644 tests/Tdphillipsjr/Validator/ZipcodeValidatorTest.php diff --git a/README.md b/README.md index cdc22f9..1e0a0e6 100755 --- a/README.md +++ b/README.md @@ -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 diff --git a/src/Tdphillipsjr/Validator/ZipcodeValidator.php b/src/Tdphillipsjr/Validator/ZipcodeValidator.php new file mode 100644 index 0000000..5238576 --- /dev/null +++ b/src/Tdphillipsjr/Validator/ZipcodeValidator.php @@ -0,0 +1,30 @@ +_data)) $this->addError('Zip code is not in a valid US format.'); + + return !sizeof($this->_errors); + } +} diff --git a/tests/Tdphillipsjr/Validator/ZipcodeValidatorTest.php b/tests/Tdphillipsjr/Validator/ZipcodeValidatorTest.php new file mode 100644 index 0000000..89607bc --- /dev/null +++ b/tests/Tdphillipsjr/Validator/ZipcodeValidatorTest.php @@ -0,0 +1,54 @@ +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()); + } +} +?> \ No newline at end of file