Skip to content

Commit

Permalink
Merge pull request #50 from XueSiLf/3.x
Browse files Browse the repository at this point in the history
🚀feat: add support for DateAfterColumn and DateBeforeColumn
  • Loading branch information
kiss291323003 authored Oct 24, 2024
2 parents a89ee54 + 27b2475 commit 95035b6
Show file tree
Hide file tree
Showing 4 changed files with 489 additions and 4 deletions.
62 changes: 60 additions & 2 deletions src/Validator/DateAfterColumn.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,65 @@

namespace EasySwoole\HttpAnnotation\Validator;

class DateAfterColumn
use EasySwoole\HttpAnnotation\Attributes\Param;
use EasySwoole\HttpAnnotation\Exception\Annotation;
use EasySwoole\HttpAnnotation\Validator\AbstractInterface\AbstractValidator;
use Psr\Http\Message\ServerRequestInterface;

class DateAfterColumn extends AbstractValidator
{
public $compare;

public function __construct(string $compare, ?string $errorMsg = null)
{
if (empty($errorMsg)) {
$errorMsg = "{#name} must be date after {#compare} column";
}
$this->compare = $compare;
$this->errorMsg($errorMsg);
}

protected function validate(Param $param, ServerRequestInterface $request): bool
{
$list = $this->allCheckParams();

if (!isset($list[$this->compare])) {
throw new Annotation("compare param: {$this->compare} require in DateAfterColumn rule, but not define in any controller annotation");
}

$compareValue = $list[$this->compare]->parsedValue();

if (is_numeric($compareValue) && (strlen($compareValue) == 10)) {
$afterUnixTime = $compareValue;
} else {
$afterUnixTime = strtotime($compareValue);
}

if (is_bool($afterUnixTime)) {
throw new Annotation("error arg: error compare param: {$this->compare} for DateAfterColumn validate rule");
}

$itemData = $param->parsedValue();

if (!is_string($itemData)) {
return false;
}

$unixTime = strtotime($itemData);

if (is_bool($unixTime)) {
return false;
}

if ($unixTime > $afterUnixTime) {
return true;
}

return false;
}

}
public function ruleName(): string
{
return "DateAfterColumn";
}
}
62 changes: 60 additions & 2 deletions src/Validator/DateBeforeColumn.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,65 @@

namespace EasySwoole\HttpAnnotation\Validator;

class DateBeforeColumn
use EasySwoole\HttpAnnotation\Attributes\Param;
use EasySwoole\HttpAnnotation\Exception\Annotation;
use EasySwoole\HttpAnnotation\Validator\AbstractInterface\AbstractValidator;
use Psr\Http\Message\ServerRequestInterface;

class DateBeforeColumn extends AbstractValidator
{
public $compare;

public function __construct(string $compare, ?string $errorMsg = null)
{
if (empty($errorMsg)) {
$errorMsg = "{#name} must be date before {#compare} column";
}
$this->compare = $compare;
$this->errorMsg($errorMsg);
}

protected function validate(Param $param, ServerRequestInterface $request): bool
{
$list = $this->allCheckParams();

if (!isset($list[$this->compare])) {
throw new Annotation("compare param: {$this->compare} require in DateBeforeColumn rule, but not define in any controller annotation");
}

$compareValue = $list[$this->compare]->parsedValue();

if (is_numeric($compareValue) && (strlen($compareValue) == 10)) {
$afterUnixTime = $compareValue;
} else {
$afterUnixTime = strtotime($compareValue);
}

if (is_bool($afterUnixTime)) {
throw new Annotation("error arg: error compare param: {$this->compare} for DateBeforeColumn validate rule");
}

$itemData = $param->parsedValue();

if (!is_string($itemData)) {
return false;
}

$unixTime = strtotime($itemData);

if (is_bool($unixTime)) {
return false;
}

if ($unixTime < $afterUnixTime) {
return true;
}

return false;
}

}
public function ruleName(): string
{
return "DateBeforeColumn";
}
}
184 changes: 184 additions & 0 deletions tests/Validator/DateAfterColumnTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,184 @@
<?php

namespace EasySwoole\HttpAnnotation\Tests\Validator;

use EasySwoole\Http\Request;
use EasySwoole\HttpAnnotation\Attributes\Param;
use EasySwoole\HttpAnnotation\Validator\DateAfterColumn;
use PHPUnit\Framework\TestCase;

class DateAfterColumnTest extends TestCase
{
/*
* 合法
*/
public function testValidCase()
{
// 日期格式
$request = new Request();
$request->withQueryParams([
"date" => "20220501",
"date1" => "20220430"
]);

$param = new Param(name: "date");
$param->parsedValue($request);

$date1 = new Param(name: "date1");
$date1->parsedValue($request);

$rule = new DateAfterColumn(compare: "date1");

$rule->allCheckParams([
"date" => $param,
"date1" => $date1
]);

$this->assertEquals(true, $rule->execute($param, $request));

$request = new Request();
$request->withQueryParams([
"date" => "2022-05-06",
"date1" => "20220430"
]);

$param = new Param(name: "date");
$param->parsedValue($request);

$date1 = new Param(name: "date1");
$date1->parsedValue($request);

$rule = new DateAfterColumn(compare: "date1");

$rule->allCheckParams([
"date" => $param,
"date1" => $date1
]);

$this->assertEquals(true, $rule->execute($param, $request));

$request = new Request();
$request->withQueryParams([
"date" => "2022-05-06 00:00:00",
"date1" => "20200630",
]);

$param = new Param(name: "date");
$param->parsedValue($request);

$date1 = new Param(name: "date1");
$date1->parsedValue($request);

$rule = new DateAfterColumn(compare: "date1");

$rule->allCheckParams([
"date" => $param,
"date1" => $date1
]);

$this->assertEquals(true, $rule->execute($param, $request));
}

/*
* 默认错误信息
*/
public function testDefaultErrorMsgCase()
{
// 日期不符
$request = new Request();
$request->withQueryParams([
"date" => "2022-05-08",
"date1" => "20220530",
]);

$param = new Param(name: "date");
$param->parsedValue($request);

$date1 = new Param(name: "date1");
$date1->parsedValue($request);

$rule = new DateAfterColumn(compare: "date1");

$rule->allCheckParams([
"date" => $param,
"date1" => $date1
]);

$this->assertEquals(false, $rule->execute($param, $request));
$this->assertEquals("date must be date after date1 column", $rule->errorMsg());

// 非法参数
$request = new Request();
$request->withQueryParams([
"date" => "xuesi",
"date1" => "20220530",
]);

$param = new Param(name: "date");
$param->parsedValue($request);

$date1 = new Param(name: "date1");
$date1->parsedValue($request);

$rule = new DateAfterColumn(compare: "date1");

$rule->allCheckParams([
"date" => $param,
"date1" => $date1
]);

$this->assertEquals(false, $rule->execute($param, $request));
$this->assertEquals("date must be date after date1 column", $rule->errorMsg());

//字段必须是日期格式。因此传时间戳,失败
$request = new Request();
$request->withQueryParams([
"date" => "1654765394",
"date1" => "20220530",
]);

$param = new Param(name: "date");
$param->parsedValue($request);

$date1 = new Param(name: "date1");
$date1->parsedValue($request);

$rule = new DateAfterColumn(compare: "date1");

$rule->allCheckParams([
"date" => $param,
"date1" => $date1
]);

$this->assertEquals(false, $rule->execute($param, $request));
$this->assertEquals("date must be date after date1 column", $rule->errorMsg());
}

/*
* 自定义错误信息
*/
public function testCustomErrorMsgCase()
{
$request = new Request();
$request->withQueryParams([
"date" => "xuesi",
"date1" => "20220530",
]);

$param = new Param(name: "date");
$param->parsedValue($request);

$date1 = new Param(name: "date1");
$date1->parsedValue($request);

$rule = new DateAfterColumn(compare: "date1", errorMsg: '日期不合法');

$rule->allCheckParams([
"date" => $param,
"date1" => $date1
]);

$this->assertEquals(false, $rule->execute($param, $request));
$this->assertEquals("日期不合法", $rule->errorMsg());
}
}
Loading

0 comments on commit 95035b6

Please sign in to comment.