Skip to content

Commit

Permalink
feat(isTimestamp): 增加 isTimestamp 校验器,用于校验数据是否符合数据库 timestamp 字段
Browse files Browse the repository at this point in the history
  • Loading branch information
twinh committed Dec 2, 2022
1 parent a72f566 commit 3da68cd
Show file tree
Hide file tree
Showing 4 changed files with 163 additions and 0 deletions.
62 changes: 62 additions & 0 deletions docs/auto-completion-static.php
Expand Up @@ -2385,6 +2385,21 @@ public static function check($input, string $name = '%name%'): Ret
}
}

class IsTimestamp
{
/**
* Check the input value, return a Ret object
*
* @param mixed $input
* @param string $name
* @return Ret
* @see BaseValidator::check
*/
public static function check($input, string $name = '%name%'): Ret
{
}
}

class IsTinyChar
{
/**
Expand Down Expand Up @@ -6439,6 +6454,22 @@ public static function notTime($input, $format = null)
{
}

/**
* @return $this
* @see \Wei\IsTimestamp::__invoke
*/
public static function timestamp($input, $format = null)
{
}

/**
* @return $this
* @see \Wei\IsTimestamp::__invoke
*/
public static function notTimestamp($input, $format = null)
{
}

/**
* @return $this
* @see \Wei\IsTinyChar::__invoke
Expand Down Expand Up @@ -9112,6 +9143,21 @@ public function check($input, string $name = '%name%'): Ret
}
}

class IsTimestamp
{
/**
* Check the input value, return a Ret object
*
* @param mixed $input
* @param string $name
* @return Ret
* @see BaseValidator::check
*/
public function check($input, string $name = '%name%'): Ret
{
}
}

class IsTinyChar
{
/**
Expand Down Expand Up @@ -13154,6 +13200,22 @@ public function notTime($key = null, string $label = null, $format = null)
{
}

/**
* @return $this
* @see \Wei\IsTimestamp::__invoke
*/
public function timestamp($key = null, string $label = null, $format = null)
{
}

/**
* @return $this
* @see \Wei\IsTimestamp::__invoke
*/
public function notTimestamp($key = null, string $label = null, $format = null)
{
}

/**
* @return $this
* @see \Wei\IsTinyChar::__invoke
Expand Down
9 changes: 9 additions & 0 deletions docs/auto-completion.php
Expand Up @@ -883,6 +883,14 @@ class IsTimeMixin
{
}

/**
* @property Wei\IsTimestamp $isTimestamp Check if the input is a valid database timestamp
* @method mixed isTimestamp($input, $format = null)
*/
class IsTimestampMixin
{
}

/**
* @property Wei\IsTinyChar $isTinyChar Check if the input is a string of 255 characters or less
* @method mixed isTinyChar($input, $minLength = null, $maxLength = null)
Expand Down Expand Up @@ -1436,6 +1444,7 @@ class WeiMixin
* @mixin IsStringMixin
* @mixin IsTextMixin
* @mixin IsTimeMixin
* @mixin IsTimestampMixin
* @mixin IsTinyCharMixin
* @mixin IsTinyIntMixin
* @mixin IsTldMixin
Expand Down
37 changes: 37 additions & 0 deletions lib/IsTimestamp.php
@@ -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,
]);
}
}
55 changes: 55 additions & 0 deletions tests/unit/IsTimestampTest.php
@@ -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'],
];
}
}

0 comments on commit 3da68cd

Please sign in to comment.