Skip to content

Commit

Permalink
新增SmallThanColumn 和BigThanColumn规则
Browse files Browse the repository at this point in the history
  • Loading branch information
kiss291323003 committed Aug 17, 2024
1 parent 08fb141 commit 2e6f57f
Show file tree
Hide file tree
Showing 2 changed files with 80 additions and 0 deletions.
40 changes: 40 additions & 0 deletions src/Validator/BigThanColumn.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
<?php

namespace EasySwoole\HttpAnnotation\Validator;

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

class BigThanColumn extends AbstractValidator
{

function __construct(public string $paramName,?string $errorMsg = null)
{
if(empty($errorMsg)){
$errorMsg = "{#name} value must big than {$paramName} value";
}
$this->errorMsg($errorMsg);
}

protected function validate(Param $param, ServerRequestInterface $request): bool
{
$itemData = $param->parsedValue();
$list = $this->allCheckParams();
if(!isset($list[$this->paramName])){
throw new Annotation("compare param: {$this->paramName} require in BigThanColumn rule ,but not define in any controller annotation");
}
$compare = $list[$this->paramName]->parsedValue();
if($itemData > $compare){
return true;
}else{
return false;
}
}

function ruleName(): string
{
return 'BigThanColumn';
}
}
40 changes: 40 additions & 0 deletions src/Validator/SmallThanColumn.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
<?php

namespace EasySwoole\HttpAnnotation\Validator;

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

class SmallThanColumn extends AbstractValidator
{

function __construct(public string $paramName,?string $errorMsg = null)
{
if(empty($errorMsg)){
$errorMsg = "{#name} value must small than {$paramName} value";
}
$this->errorMsg($errorMsg);
}

protected function validate(Param $param, ServerRequestInterface $request): bool
{
$itemData = $param->parsedValue();
$list = $this->allCheckParams();
if(!isset($list[$this->paramName])){
throw new Annotation("compare param: {$this->paramName} require in SmallThanColumn rule ,but not define in any controller annotation");
}
$compare = $list[$this->paramName]->parsedValue();
if($itemData > $compare){
return false;
}else{
return true;
}
}

function ruleName(): string
{
return 'SmallThanColumn';
}
}

0 comments on commit 2e6f57f

Please sign in to comment.