verify : add the static methods
verify : add the unit tests git-svn-id: https://svn.fournier38.fr/svn/ProgSVN/trunk@3520 bf3deb0d-5f1a-0410-827f-c0cc1f45334c
This commit is contained in:
96
Tests/verifyTest.php
Normal file
96
Tests/verifyTest.php
Normal file
@@ -0,0 +1,96 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
require_once ("verify.php");
|
||||||
|
|
||||||
|
/** Test the Verify */
|
||||||
|
class test_verify extends PHPUnit_Framework_TestCase
|
||||||
|
{
|
||||||
|
///////////////
|
||||||
|
// DATES //
|
||||||
|
///////////////
|
||||||
|
public function test_is_datetimeSQL1 ()
|
||||||
|
{
|
||||||
|
$verify = new \verify ();
|
||||||
|
$res = $verify->is_datetimeSQL ("2017-04-13 22:55:17");
|
||||||
|
$this->assertSame ($res, true);
|
||||||
|
}
|
||||||
|
|
||||||
|
public function test_is_datetimeSQL2 ()
|
||||||
|
{
|
||||||
|
$verify = new \verify ();
|
||||||
|
$res = $verify->is_datetimeSQL ("2017-13-55 22:55:17");
|
||||||
|
$this->assertSame ($res, false);
|
||||||
|
}
|
||||||
|
|
||||||
|
public function test_staticIs_datetimeSQL1 ()
|
||||||
|
{
|
||||||
|
$res = \verify::staticIs_datetimeSQL ("2017-04-13 22:55:17");
|
||||||
|
$this->assertSame ($res, true);
|
||||||
|
}
|
||||||
|
|
||||||
|
public function test_staticIs_datetimeSQL2 ()
|
||||||
|
{
|
||||||
|
$res = \verify::staticIs_datetimeSQL ("2017-13-55 22:55:17");
|
||||||
|
$this->assertSame ($res, false);
|
||||||
|
}
|
||||||
|
|
||||||
|
/////////////////
|
||||||
|
// NUMBERS //
|
||||||
|
/////////////////
|
||||||
|
public function test_staticIs_integer1 ()
|
||||||
|
{
|
||||||
|
$res = \verify::staticIs_integer ("2017-04-13 22:55:17");
|
||||||
|
$this->assertSame ($res, false);
|
||||||
|
}
|
||||||
|
|
||||||
|
public function test_staticIs_integer2 ()
|
||||||
|
{
|
||||||
|
$res = \verify::staticIs_integer ("01234");
|
||||||
|
$this->assertSame ($res, true);
|
||||||
|
}
|
||||||
|
|
||||||
|
public function test_staticIs_integer3 ()
|
||||||
|
{
|
||||||
|
$res = \verify::staticIs_integer ("0x1234");
|
||||||
|
$this->assertSame ($res, false);
|
||||||
|
}
|
||||||
|
|
||||||
|
public function test_staticIs_integer4 ()
|
||||||
|
{
|
||||||
|
$res = \verify::staticIs_integer ("");
|
||||||
|
$this->assertSame ($res, false);
|
||||||
|
}
|
||||||
|
|
||||||
|
////////////////
|
||||||
|
// EMAILS //
|
||||||
|
////////////////
|
||||||
|
|
||||||
|
/////////////
|
||||||
|
// URL //
|
||||||
|
/////////////
|
||||||
|
public function test_is_url1 ()
|
||||||
|
{
|
||||||
|
$verify = new \verify ();
|
||||||
|
$res = $verify->is_url ("invalid");
|
||||||
|
$this->assertsame ($res, false);
|
||||||
|
}
|
||||||
|
|
||||||
|
public function test_is_url2 ()
|
||||||
|
{
|
||||||
|
$verify = new \verify ();
|
||||||
|
$res = $verify->is_url ("http://valid");
|
||||||
|
$this->assertsame ($res, true);
|
||||||
|
}
|
||||||
|
|
||||||
|
public function test_staticIs_url1 ()
|
||||||
|
{
|
||||||
|
$res = \verify::staticIs_url ("invalid");
|
||||||
|
$this->assertsame ($res, false);
|
||||||
|
}
|
||||||
|
|
||||||
|
public function test_staticIs_url2 ()
|
||||||
|
{
|
||||||
|
$res = \verify::staticIs_url ("http://valid");
|
||||||
|
$this->assertsame ($res, true);
|
||||||
|
}
|
||||||
|
}
|
||||||
199
verify.php
199
verify.php
@@ -10,89 +10,149 @@ class verify
|
|||||||
// NETWORK //
|
// NETWORK //
|
||||||
/////////////////
|
/////////////////
|
||||||
/** Check if $val is a valid IPv4
|
/** Check if $val is a valid IPv4
|
||||||
Return TRUE or FALSE
|
* Return true or false
|
||||||
@param string $val The IP to test */
|
* @param string $val The IP to test
|
||||||
|
*/
|
||||||
public function is_ip ($val)
|
public function is_ip ($val)
|
||||||
|
{
|
||||||
|
return $this->staticIs_ip ($val);
|
||||||
|
}
|
||||||
|
|
||||||
|
/** Check if $val is a valid IPv4 in static mode
|
||||||
|
* Return true or false
|
||||||
|
* @param string $val The IP to test
|
||||||
|
*/
|
||||||
|
public function staticIs_ip ($val)
|
||||||
{
|
{
|
||||||
if (filter_var ($val, FILTER_VALIDATE_IP))
|
if (filter_var ($val, FILTER_VALIDATE_IP))
|
||||||
return TRUE;
|
return true;
|
||||||
return FALSE;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
/** Check if $val is a valid IPv4
|
/** Check if $val is a valid IPv4
|
||||||
Return TRUE or FALSE
|
* Return true or false
|
||||||
@param string $val The IPv4 to test */
|
* @param string $val The IPv4 to test
|
||||||
|
*/
|
||||||
public function is_ipv4 ($val)
|
public function is_ipv4 ($val)
|
||||||
|
{
|
||||||
|
return $this->staticIs_ipv4 ($val);
|
||||||
|
}
|
||||||
|
|
||||||
|
/** Check if $val is a valid IPv4 in static mode
|
||||||
|
* Return true or false
|
||||||
|
* @param string $val The IPv4 to test
|
||||||
|
*/
|
||||||
|
public function staticIs_ipv4 ($val)
|
||||||
{
|
{
|
||||||
if (filter_var ($val, FILTER_VALIDATE_IP, FILTER_FLAG_IPV4))
|
if (filter_var ($val, FILTER_VALIDATE_IP, FILTER_FLAG_IPV4))
|
||||||
return TRUE;
|
return true;
|
||||||
return FALSE;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
/** Check if $val is a valid IPv6
|
/** Check if $val is a valid IPv6
|
||||||
Return TRUE or FALSE
|
* Return true or false
|
||||||
@param string $val The IPv to test */
|
* @param string $val The IPv6 to test
|
||||||
|
*/
|
||||||
public function is_ipv6 ($val)
|
public function is_ipv6 ($val)
|
||||||
|
{
|
||||||
|
return $this->staticIs_ipv6 ($val);
|
||||||
|
}
|
||||||
|
|
||||||
|
/** Check if $val is a valid IPv6 in static mode
|
||||||
|
* Return true or false
|
||||||
|
* @param string $val The IPv6 to test
|
||||||
|
*/
|
||||||
|
public static function staticIs_ipv6 ($val)
|
||||||
{
|
{
|
||||||
if (filter_var ($val, FILTER_VALIDATE_IP, FILTER_FLAG_IPV6))
|
if (filter_var ($val, FILTER_VALIDATE_IP, FILTER_FLAG_IPV6))
|
||||||
return TRUE;
|
return true;
|
||||||
return FALSE;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
/** Check if $val is a valid hostname (without domain)
|
/** Check if $val is a valid hostname (without domain)
|
||||||
Return TRUE or FALSE
|
* Return true or false
|
||||||
@param string $val The hostname to check */
|
* @param string $val The hostname to check
|
||||||
|
*/
|
||||||
public function is_hostname ($val)
|
public function is_hostname ($val)
|
||||||
|
{
|
||||||
|
return $this->staticIs_hostname ($val);
|
||||||
|
}
|
||||||
|
|
||||||
|
/** Check if $val is a valid hostname (without domain)
|
||||||
|
* Return true or false
|
||||||
|
* @param string $val The hostname to check
|
||||||
|
*/
|
||||||
|
public static function staticIs_hostname ($val)
|
||||||
{
|
{
|
||||||
if (strlen ($val) < 2 || strlen ($val) > 128)
|
if (strlen ($val) < 2 || strlen ($val) > 128)
|
||||||
return FALSE;
|
return false;
|
||||||
if (strspn ($val, "abcdefghijklmnopqrstuvwxyz".
|
if (strspn ($val, "abcdefghijklmnopqrstuvwxyz".
|
||||||
"ABCDEFGHIJKLMNOPQRSTUVWXYZ".
|
"ABCDEFGHIJKLMNOPQRSTUVWXYZ".
|
||||||
"0123456789-.") !==
|
"0123456789-.") !==
|
||||||
strlen ($val))
|
strlen ($val))
|
||||||
return FALSE;
|
return false;
|
||||||
// No end dot ?
|
// No end dot ?
|
||||||
if (substr ($val, -1) === ".")
|
if (substr ($val, -1) === ".")
|
||||||
return FALSE;
|
return false;
|
||||||
return TRUE;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
/** Check if $val is a valid domain (without hostname)
|
/** Check if $val is a valid domain (without hostname)
|
||||||
Return TRUE or FALSE
|
* Return true or false
|
||||||
@param string $val The domain to check */
|
* @param string $val The domain to check
|
||||||
|
*/
|
||||||
public function is_domain ($val)
|
public function is_domain ($val)
|
||||||
|
{
|
||||||
|
return $this->staticIs_domain ($val);
|
||||||
|
}
|
||||||
|
|
||||||
|
/** Check if $val is a valid domain (without hostname) in static mode
|
||||||
|
* Return true or false
|
||||||
|
* @param string $val The domain to check
|
||||||
|
*/
|
||||||
|
public static function staticIs_domain ($val)
|
||||||
{
|
{
|
||||||
if (strlen ($val) < 2 || strlen ($val) > 128)
|
if (strlen ($val) < 2 || strlen ($val) > 128)
|
||||||
return FALSE;
|
return false;
|
||||||
if (strspn ($val, "abcdefghijklmnopqrstuvwxyz".
|
if (strspn ($val, "abcdefghijklmnopqrstuvwxyz".
|
||||||
"ABCDEFGHIJKLMNOPQRSTUVWXYZ".
|
"ABCDEFGHIJKLMNOPQRSTUVWXYZ".
|
||||||
"0123456789-.") !==
|
"0123456789-.") !==
|
||||||
strlen ($val))
|
strlen ($val))
|
||||||
return FALSE;
|
return false;
|
||||||
// No end dot ?
|
// No end dot ?
|
||||||
if (substr ($val, -1) === ".")
|
if (substr ($val, -1) === ".")
|
||||||
return FALSE;
|
return false;
|
||||||
return TRUE;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
/** Check if $val is a valid FQDN
|
/** Check if $val is a valid FQDN
|
||||||
Return TRUE or FALSE
|
* Return true or false
|
||||||
@param string $val The FQDN to check */
|
* @param string $val The FQDN to check
|
||||||
|
*/
|
||||||
public function is_FQDN ($val)
|
public function is_FQDN ($val)
|
||||||
|
{
|
||||||
|
return $this->staticIs_FQDN ($val);
|
||||||
|
}
|
||||||
|
|
||||||
|
/** Check if $val is a valid FQDN in static mode
|
||||||
|
* Return true or false
|
||||||
|
* @param string $val The FQDN to check
|
||||||
|
*/
|
||||||
|
public static function staticIs_FQDN ($val)
|
||||||
{
|
{
|
||||||
if (strlen ($val) < 2 || strlen ($val) > 255)
|
if (strlen ($val) < 2 || strlen ($val) > 255)
|
||||||
return FALSE;
|
return false;
|
||||||
if ($this->is_ip ($val))
|
if ($this->is_ip ($val))
|
||||||
return FALSE;
|
return false;
|
||||||
if (strspn ($val, "abcdefghijklmnopqrstuvwxyz".
|
if (strspn ($val, "abcdefghijklmnopqrstuvwxyz".
|
||||||
"ABCDEFGHIJKLMNOPQRSTUVWXYZ".
|
"ABCDEFGHIJKLMNOPQRSTUVWXYZ".
|
||||||
"0123456789-.") !==
|
"0123456789-.") !==
|
||||||
strlen ($val))
|
strlen ($val))
|
||||||
return FALSE;
|
return false;
|
||||||
// No end dot ?
|
// No end dot ?
|
||||||
if (substr ($val, -1) === ".")
|
if (substr ($val, -1) === ".")
|
||||||
return FALSE;
|
return false;
|
||||||
return TRUE;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
///////////////
|
///////////////
|
||||||
@@ -100,45 +160,98 @@ class verify
|
|||||||
///////////////
|
///////////////
|
||||||
/** Check if $val is a valid date
|
/** Check if $val is a valid date
|
||||||
A valid date is 2014-03-20 12:27:34
|
A valid date is 2014-03-20 12:27:34
|
||||||
Return TRUE or FALSE
|
Return true or false
|
||||||
@param string $val The date to check */
|
@param string $val The date to check */
|
||||||
public function is_datetimeSQL ($val)
|
public function is_datetimeSQL ($val)
|
||||||
|
{
|
||||||
|
return $this->staticIs_datetimeSQL ($val);
|
||||||
|
}
|
||||||
|
|
||||||
|
/** Check if $val is a valid date in static mode
|
||||||
|
A valid date is 2014-03-20 12:27:34
|
||||||
|
Return true or false
|
||||||
|
@param string $val The date to check */
|
||||||
|
public static function staticIs_datetimeSQL ($val)
|
||||||
{
|
{
|
||||||
if (strlen ($val) !== 19)
|
if (strlen ($val) !== 19)
|
||||||
return FALSE;
|
return false;
|
||||||
$arr = date_parse ($val);
|
$arr = date_parse ($val);
|
||||||
if ($arr["warning_count"] !==0)
|
if ($arr["warning_count"] !==0)
|
||||||
return FALSE;
|
return false;
|
||||||
if ($arr["error_count"] !==0)
|
if ($arr["error_count"] !==0)
|
||||||
return FALSE;
|
return false;
|
||||||
if (isset ($arr["tz_abbr"]))
|
if (isset ($arr["tz_abbr"]))
|
||||||
return FALSE;
|
return false;
|
||||||
if (strspn ($val, "0123456789 :-") !== strlen ($val))
|
if (strspn ($val, "0123456789 :-") !== strlen ($val))
|
||||||
return FALSE;
|
return false;
|
||||||
return TRUE;
|
if (\DateTime::createFromFormat("Y-m-d H:i:s", $val) === false)
|
||||||
|
return false;
|
||||||
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
/////////////////
|
/////////////////
|
||||||
// NUMBERS //
|
// NUMBERS //
|
||||||
/////////////////
|
/////////////////
|
||||||
/** Return TRUE if the provided value is an integer in decimal (not octal)
|
/** Return true if the provided value is an integer in decimal (not octal)
|
||||||
@param string $val The Integer val to check */
|
* @param string $val The Integer val to check
|
||||||
|
*/
|
||||||
public function is_integer ($val)
|
public function is_integer ($val)
|
||||||
{
|
{
|
||||||
|
return $this->staticIs_integer ($val);
|
||||||
|
}
|
||||||
|
|
||||||
|
/** Return true if the provided value is an integer in decimal (not octal)
|
||||||
|
* In static mode
|
||||||
|
* @param string $val The Integer val to check
|
||||||
|
*/
|
||||||
|
public static function staticIs_integer ($val)
|
||||||
|
{
|
||||||
|
if (! is_string ($val) || trim ($val) === "")
|
||||||
|
return false;
|
||||||
if (strspn ($val, "0123456789") !== strlen ($val))
|
if (strspn ($val, "0123456789") !== strlen ($val))
|
||||||
return FALSE;
|
return false;
|
||||||
return TRUE;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
////////////////
|
////////////////
|
||||||
// EMAILS //
|
// EMAILS //
|
||||||
////////////////
|
////////////////
|
||||||
/** Return TRUE if the provided value is a valid email address
|
/** Return true if the provided value is a valid email address
|
||||||
* @param string $val The value to check
|
* @param string $val The value to check
|
||||||
* @return boolean
|
* @return boolean
|
||||||
*/
|
*/
|
||||||
public function is_email ($val)
|
public function is_email ($val)
|
||||||
|
{
|
||||||
|
return $this->staticIs_email ($val);
|
||||||
|
}
|
||||||
|
|
||||||
|
/** Return true if the provided value is a valid email address in static mode
|
||||||
|
* @param string $val The value to check
|
||||||
|
* @return boolean
|
||||||
|
*/
|
||||||
|
public static function staticIs_email ($val)
|
||||||
{
|
{
|
||||||
return !! filter_var ($val, FILTER_VALIDATE_EMAIL);
|
return !! filter_var ($val, FILTER_VALIDATE_EMAIL);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/////////////
|
||||||
|
// URL //
|
||||||
|
/////////////
|
||||||
|
/** Return true if the provided value is a valid URL
|
||||||
|
* @param string $val The value to check
|
||||||
|
* @return boolean
|
||||||
|
*/
|
||||||
|
public function is_URL ($val)
|
||||||
|
{
|
||||||
|
return $this->staticIs_URL ($val);
|
||||||
|
}
|
||||||
|
|
||||||
|
/** Return true if the provided value is a valid URL in static mode
|
||||||
|
* @param string $val The value to check
|
||||||
|
* @return boolean
|
||||||
|
*/
|
||||||
|
public static function staticIs_URL ($val)
|
||||||
|
{
|
||||||
|
return !! filter_var ($val, FILTER_VALIDATE_URL);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user