-
Notifications
You must be signed in to change notification settings - Fork 14
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(isTimestamp): 增加
isTimestamp
校验器,用于校验数据是否符合数据库 timestamp
字段
- Loading branch information
Showing
4 changed files
with
163 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
<?php | ||
|
||
/** | ||
* Wei Framework | ||
* | ||
* @copyright Copyright (c) 2008-2022 Twin Huang | ||
* @license http://opensource.org/licenses/mit-license.php MIT License | ||
*/ | ||
|
||
namespace Wei; | ||
|
||
/** | ||
* Check if the input is a valid database timestamp | ||
* | ||
* @author Twin Huang <twinhuang@qq.com> | ||
*/ | ||
class IsTimestamp extends IsAnyDateTime | ||
{ | ||
protected $format = 'Y-m-d H:i:s'; | ||
|
||
protected $min = '1970-01-01 00:00:01'; | ||
|
||
protected $max = '2038-01-19 03:14:07'; | ||
|
||
protected function doValidate($input) | ||
{ | ||
$result = parent::doValidate($input); | ||
if (!$result) { | ||
return $result; | ||
} | ||
|
||
return $this->validateRule($input, 'between', [ | ||
'min' => $this->min, | ||
'max' => $this->max, | ||
]); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
<?php | ||
|
||
namespace WeiTest; | ||
|
||
/** | ||
* @internal | ||
* @mixin \IsTimestampMixin | ||
*/ | ||
final class IsTimestampTest extends BaseValidatorTestCase | ||
{ | ||
/** | ||
* @dataProvider providerForTimestamp | ||
* @param mixed $input | ||
* @param mixed|null $format | ||
*/ | ||
public function testTimestamp($input, $format = null) | ||
{ | ||
$this->assertTrue($this->isTimestamp($input, $format)); | ||
} | ||
|
||
/** | ||
* @dataProvider providerForNotTimestamp | ||
* @param mixed $input | ||
* @param mixed|null $format | ||
*/ | ||
public function testNotTimestamp($input, $format = null) | ||
{ | ||
$this->assertFalse($this->isTimestamp($input, $format)); | ||
} | ||
|
||
public function providerForTimestamp() | ||
{ | ||
return [ | ||
['2012-02-29 23:59:59'], | ||
['1970-01-01 00:00:01'], | ||
['2038-01-19 03:14:07'], | ||
]; | ||
} | ||
|
||
public function providerForNotTimestamp() | ||
{ | ||
return [ | ||
['0'], | ||
[0], | ||
[0.0], | ||
['2013-02-29 24:00:00'], | ||
['2013-01-32 23:60:00'], | ||
['2013-00-00 23:59:61'], | ||
['2012 61:00'], | ||
['2013-02-29 24:00:00'], // date_get_last_errors() => warning_count = 1, The parsed date was invalid | ||
['1970-01-01 00:00:00'], | ||
['2038-01-19 03:14:08'], | ||
]; | ||
} | ||
} |