Skip to content

Commit

Permalink
Merge pull request #27 from gaobinzhan/2.x
Browse files Browse the repository at this point in the history
fix: param type[array]
  • Loading branch information
kiss291323003 authored Mar 16, 2021
2 parents 920cee6 + 530708a commit 9775b9b
Show file tree
Hide file tree
Showing 3 changed files with 67 additions and 18 deletions.
44 changes: 26 additions & 18 deletions src/AnnotationTag/Param.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
namespace EasySwoole\HttpAnnotation\AnnotationTag;

use EasySwoole\Annotation\AbstractAnnotationTag;

/**
* Class Param
* @package EasySwoole\HttpAnnotation\AnnotationTag
Expand Down Expand Up @@ -52,14 +53,15 @@ class Param extends AbstractAnnotationTag
public $validateRuleList = [];

private $allowValidateRule = [
'required', 'notEmpty', 'optional',
'activeUrl', 'alpha', 'alphaNum', 'alphaDash', 'between', 'bool',
'decimal', 'dateBefore', 'dateAfter', 'equal', 'different',
'equalWithColumn', 'differentWithColumn', 'lessThanWithColumn', 'greaterThanWithColumn',
'float', 'func', 'inArray', 'integer', 'isIp',
'notEmpty', 'numeric', 'notInArray', 'length', 'lengthMax', 'lengthMin',
'func', 'inArray', 'notInArray', 'isIp',
'integer', 'numeric', 'float', 'length', 'lengthMax', 'lengthMin',
'betweenLen', 'money', 'max', 'min', 'regex', 'allDigital',
'required', 'timestamp', 'timestampBeforeDate', 'timestampAfterDate',
'timestampBefore', 'timestampAfter', 'url','optional','allowFile','allowFileType'
'timestamp', 'timestampBeforeDate', 'timestampAfterDate',
'timestampBefore', 'timestampAfter', 'url', 'allowFile', 'allowFileType'
];

/**
Expand Down Expand Up @@ -241,12 +243,12 @@ public function tagName(): string

function __onParser()
{
foreach ($this->allowValidateRule as $ruleName){
if($this->$ruleName !== null){
foreach ($this->allowValidateRule as $ruleName) {
if ($this->$ruleName !== null) {
$this->validateRuleList[$ruleName] = $this->$ruleName;
//对inArray 做特殊处理
if(in_array($ruleName,['inArray','notInArray','allowFile','allowFileType'])){
if(!is_array($this->$ruleName[0])){
if (in_array($ruleName, ['inArray', 'notInArray', 'allowFile', 'allowFileType'])) {
if (!is_array($this->$ruleName[0])) {
$this->$ruleName = [$this->$ruleName];
}
}
Expand All @@ -256,29 +258,35 @@ function __onParser()

public function typeCast($val)
{
switch ($this->type)
{
case 'string':{
switch ($this->type) {
case 'string':
{
return (string)$val;
}
case 'int':{
case 'int':
{
return (int)$val;
}
case 'double':
case 'real':
case 'float':{
case 'float':
{
return (float)$val;
}
case 'bool':{
case 'bool':
{
return (bool)$val;
}
case 'object':{
case 'object':
{
return json_decode($val);
}
case 'array':{
return json_encode($val);
case 'array':
{
return (array)$val;
}
default:{
default:
{
return $val;
}
}
Expand Down
16 changes: 16 additions & 0 deletions tests/ParamTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -151,4 +151,20 @@ public function testDeprecated()

$this->fail('test deprecated fail');
}

public function testParamType()
{
$response = $this->fakeResponse();
$this->controller->__hook('paramType', $this->fakeRequest('/',
['groupAuth' => 1, 'groupParam' => 1, 'onRequestAuth' => 1, 'onRequestParam' => 1, 'auth' => 1, 'param' => 1,
'string' => 1,
'int' => '1',
'float' => '1',
'bool' => 1,
'object' => json_encode(['a' => 1, 'b' => 2]),
'array' => []
]
), $response);
$this->assertEquals('success', $response->getBody()->__toString());
}
}
25 changes: 25 additions & 0 deletions tests/TestController/Param.php
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,31 @@ public function notDeprecated()

}

/**
* @\EasySwoole\HttpAnnotation\AnnotationTag\Param(name="string",type="string")
* @\EasySwoole\HttpAnnotation\AnnotationTag\Param(name="int",type="int")
* @\EasySwoole\HttpAnnotation\AnnotationTag\Param(name="float",type="float")
* @\EasySwoole\HttpAnnotation\AnnotationTag\Param(name="bool",type="bool")
* @\EasySwoole\HttpAnnotation\AnnotationTag\Param(name="object",type="object")
* @\EasySwoole\HttpAnnotation\AnnotationTag\Param(name="array",type="array")
*/
public function paramType(string $string, int $int, float $float, bool $bool, \stdClass $object, array $array)
{
if (gettype($string) !== 'string' ||
gettype($int) !== 'integer' ||
gettype($float) !== 'double' ||
gettype($bool) !== 'boolean' ||
gettype($object) !== 'object' ||
gettype($array) !== 'array'

) {
$this->response()->write('error');
} else {
$this->response()->write('success');
}

}

protected function onException(\Throwable $throwable): void
{
if ($throwable instanceof ParamValidateError) {
Expand Down

0 comments on commit 9775b9b

Please sign in to comment.