diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..bf51689 --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +/.phpunit.cache/ diff --git a/phpunit.xml b/phpunit.xml index 9f8ad16..7b86f75 100644 --- a/phpunit.xml +++ b/phpunit.xml @@ -1,30 +1,14 @@ - - - - tests/ - - - - - src - - + + + + tests/ + + + + + + src + + diff --git a/src/MD_STD_IN.php b/src/MD_STD_IN.php index ef9bdc9..0c004e1 100644 --- a/src/MD_STD_IN.php +++ b/src/MD_STD_IN.php @@ -392,6 +392,27 @@ final class MD_STD_IN { } + /** + * Validates a ZIP code. + * + * @param string $input Input string. + * + * @return string + */ + public static function validate_zip_code(string $input):string { + + if (($input = trim($input)) === "") { + return ""; + } + + if (\mb_strlen($input) > 7) { + throw new MDgenericInvalidInputsException("ZIP code is too long"); + } + + return $input; + + } + /** * Returns an UTF8 version of a string. * diff --git a/tests/MD_STD_IN_Test.php b/tests/MD_STD_IN_Test.php index 9226d31..5425818 100644 --- a/tests/MD_STD_IN_Test.php +++ b/tests/MD_STD_IN_Test.php @@ -318,6 +318,8 @@ final class MD_STD_IN_Test extends TestCase { self::expectException(MDgenericInvalidInputsException::class); MD_STD_IN::validate_longitude(""); self::expectException(MDCoordinateOutOfRange::class); + MD_STD_IN::validate_longitude("1900"); + self::expectException(MDCoordinateOutOfRange::class); MD_STD_IN::validate_longitude(1900); self::expectException(MDCoordinateOutOfRange::class); MD_STD_IN::validate_longitude(-1900); @@ -346,6 +348,8 @@ final class MD_STD_IN_Test extends TestCase { self::expectException(MDgenericInvalidInputsException::class); MD_STD_IN::validate_latitude(""); self::expectException(MDCoordinateOutOfRange::class); + MD_STD_IN::validate_latitude("1900"); + self::expectException(MDCoordinateOutOfRange::class); MD_STD_IN::validate_latitude(1900); self::expectException(MDCoordinateOutOfRange::class); MD_STD_IN::validate_latitude(-1900); @@ -378,6 +382,24 @@ final class MD_STD_IN_Test extends TestCase { } + /** + * Function for testing validate_zip_code(). + * + * @small + * @covers \MD_STD_IN::validate_zip_code + * + * @return void + */ + public function test_validate_zip_code():void { + + self::assertEquals("", MD_STD_IN::validate_zip_code("")); + self::assertEquals("1234", MD_STD_IN::validate_zip_code(" 1234")); + + self::expectException(MDgenericInvalidInputsException::class); + MD_STD_IN::validate_zip_code("X094339604"); + + } + /** * Function for testing ensureStringIsUtf8(). *