diff --git a/Tests/verifyTest.php b/Tests/verifyTest.php index 4f28342..a4834da 100644 --- a/Tests/verifyTest.php +++ b/Tests/verifyTest.php @@ -119,4 +119,46 @@ class test_verify extends PHPUnit_Framework_TestCase $res = \verify::staticIs_url ("http://valid"); $this->assertsame ($res, true); } + + //////////////// + // OTHERS // + //////////////// + public function test_is_UUID1 () + { + $verify = new \verify (); + $res = $verify->is_UUID ("ca39275f-9224-425f-ba94-efce2aa5b52c"); + $this->assertsame ($res, true); + } + + public function test_is_UUID2 () + { + $verify = new \verify (); + $res = $verify->is_UUID ("zz39275f-9224-425f-ba94-efce2aa5b52c"); + $this->assertsame ($res, false); + } + + public function test_is_UUID3 () + { + $verify = new \verify (); + $res = $verify->is_UUID ("2c"); + $this->assertsame ($res, false); + } + + public function test_staticis_UUID1 () + { + $res = \verify::staticis_UUID ("ca39275f-9224-425f-ba94-efce2aa5b52c"); + $this->assertsame ($res, true); + } + + public function test_staticis_UUID2 () + { + $res = \verify::staticis_UUID ("zz39275f-9224-425f-ba94-efce2aa5b52c"); + $this->assertsame ($res, false); + } + + public function test_staticis_UUID3 () + { + $res = \verify::staticis_UUID ("2c"); + $this->assertsame ($res, false); + } } diff --git a/verify.php b/verify.php index a42ddbb..8869d00 100644 --- a/verify.php +++ b/verify.php @@ -285,4 +285,37 @@ class verify { return !! filter_var ($val, FILTER_VALIDATE_URL); } + + ////////////////// + /// OTHERS /// + ////////////////// + /** Return true if the provided value is a valid UUID + * %04x%04x-%04x-%04x-%04x-%04x%04x%04x + * @param string $val The value to check + * @return boolean + */ + public function is_UUID ($val) + { + return \verify::staticIs_UUID ($val); + } + + /** Return true if the provided value is a valid UUID in static mode + * %04x%04x-%04x-%04x-%04x-%04x%04x%04x + * @param string $val The value to check + * @return boolean + */ + public static function staticIs_UUID ($val) + { + if (! is_string ($val)) + return false; + if (strlen ($val) !== 36) + return false; + if (strspn ($val, "0123456789abcdefABCDEF-") !== strlen ($val)) + return false; + if ($val[8] !== "-" || $val[13] !== "-" || $val[18] !== "-" || + $val[23] !== "-") + return false; + return true; + } + }