verify: add the verification of UUID

git-svn-id: https://svn.fournier38.fr/svn/ProgSVN/trunk@3624 bf3deb0d-5f1a-0410-827f-c0cc1f45334c
This commit is contained in:
2017-05-09 14:36:44 +00:00
parent 92202f6af1
commit 3471cb3d4b
2 changed files with 75 additions and 0 deletions

View File

@@ -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);
}
}

View File

@@ -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;
}
}