Skip to content

Commit faf3eed

Browse files
committed
update some format
1 parent 9ac09cd commit faf3eed

File tree

2 files changed

+27
-16
lines changed

2 files changed

+27
-16
lines changed

src/Valid.php

+3-3
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
use function is_numeric;
88

99
/**
10-
* Class Valid - Simple Data Validator
10+
* Class Valid - Simple Data Validator TODO
1111
*
1212
* @package Inhere\Validate
1313
*/
@@ -42,9 +42,9 @@ public function getData(): array
4242
return self::$data;
4343
}
4444

45-
/**********************************************************************************************
45+
/*************************************************************************
4646
* =========== validate data field value and return
47-
*********************************************************************************************/
47+
*************************************************************************/
4848

4949
/**
5050
* @param string $field

src/Validators.php

+24-13
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,12 @@ class Validators
5959
{
6060
use NameAliasTrait;
6161

62+
public const REGEX_DATE = '/^([\d]{4})-((?:0?[\d])|(?:1[0-2]))-((?:0?[\d])|(?:[1-2][\d])|(?:3[01]))( [\d]{2}:[\d]{2}:[\d]{2})?$/';
63+
64+
public const REGEX_DATE_FMT = '/^([\d]{4})-((0?[\d])|(1[0-2]))-((0?[\d])|([1-2][\d])|(3[01]))( [\d]{2}:[\d]{2}:[\d]{2})?$/';
65+
66+
public const REGEX_ABS_URL = '/^(https?:)?\/\/[$~:;#,%&_=\(\)\[\]\.\? \+\-@\/a-zA-Z0-9]+$/';
67+
6268
/**
6369
* @var array
6470
*/
@@ -1290,33 +1296,33 @@ public static function afterOrEqualDate($val, $afterDate): bool
12901296
/**
12911297
* Check for date format
12921298
*
1293-
* @param string $date Date to validate
1299+
* @param string|mixed $date Date to validate
12941300
*
12951301
* @return bool Validity is ok or not
12961302
*/
12971303
public static function isDateFormat($date): bool
12981304
{
1299-
return (bool)preg_match(
1300-
'/^([\d]{4})-((0?[\d])|(1[0-2]))-((0?[\d])|([1-2][\d])|(3[01]))( [\d]{2}:[\d]{2}:[\d]{2})?$/',
1301-
$date
1302-
);
1305+
if (!$date || !is_string($date)) {
1306+
return false;
1307+
}
1308+
1309+
return 1 === preg_match(self::REGEX_DATE_FMT, $date);
13031310
}
13041311

13051312
/**
13061313
* Check for date validity
13071314
*
1308-
* @param string $date Date to validate
1315+
* @param string|mixed $date Date to validate
13091316
*
13101317
* @return bool Validity is ok or not
13111318
*/
13121319
public static function isDate($date): bool
13131320
{
1314-
if (!preg_match(
1315-
'/^([\d]{4})-((?:0?[\d])|(?:1[0-2]))-((?:0?[\d])|(?:[1-2][\d])|(?:3[01]))( [\d]{2}:[\d]{2}:[\d]{2})?$/',
1316-
$date,
1317-
$matches
1318-
)
1319-
) {
1321+
if (!$date || !is_string($date)) {
1322+
return false;
1323+
}
1324+
1325+
if (!preg_match(self::REGEX_DATE, $date, $matches)) {
13201326
return false;
13211327
}
13221328

@@ -1392,6 +1398,11 @@ public static function isFloat($float): bool
13921398
return (string)((float)$float) === (string)$float;
13931399
}
13941400

1401+
/**
1402+
* @param float|mixed $float
1403+
*
1404+
* @return bool
1405+
*/
13951406
public static function isUnsignedFloat($float): bool
13961407
{
13971408
if (!is_scalar($float)) {
@@ -1486,7 +1497,7 @@ public static function absoluteUrl($url): bool
14861497
return false;
14871498
}
14881499

1489-
return 1 === preg_match('/^(https?:)?\/\/[$~:;#,%&_=\(\)\[\]\.\? \+\-@\/a-zA-Z0-9]+$/', $url);
1500+
return 1 === preg_match(self::REGEX_ABS_URL, $url);
14901501
}
14911502

14921503
/**

0 commit comments

Comments
 (0)