Param.php 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. <?php
  2. namespace EasySwoole\HttpAnnotation\Tests\TestController;
  3. use EasySwoole\HttpAnnotation\AnnotationController;
  4. use EasySwoole\HttpAnnotation\AnnotationTag\ApiAuth;
  5. use EasySwoole\HttpAnnotation\AnnotationTag\ApiGroupAuth;
  6. use EasySwoole\HttpAnnotation\Exception\Annotation\ParamValidateError;
  7. /**
  8. * Class Param
  9. * @package EasySwoole\HttpAnnotation\Tests\TestController
  10. * @ApiGroupAuth(name="groupAuth",required="",notEmpty="")
  11. * @\EasySwoole\HttpAnnotation\AnnotationTag\Param(name="groupParam",required="",notEmpty="")
  12. */
  13. class Param extends AnnotationController
  14. {
  15. /**
  16. * @ApiAuth(name="onRequestAuth",required="",notEmpty="")
  17. * @\EasySwoole\HttpAnnotation\AnnotationTag\Param(name="onRequestParam",required="",notEmpty="")
  18. */
  19. public function onRequest(?string $action): ?bool
  20. {
  21. return parent::onRequest($action); // TODO: Change the autogenerated stub
  22. }
  23. /**
  24. * @ApiAuth(name="auth",required="",notEmpty="")
  25. * @\EasySwoole\HttpAnnotation\AnnotationTag\Param(name="param",required="",notEmpty="")
  26. */
  27. public function index()
  28. {
  29. $this->response()->write(json_encode($this->request()->getQueryParams()));
  30. }
  31. /**
  32. * @\EasySwoole\HttpAnnotation\AnnotationTag\Param(name="foo",lessThanWithColumn="bar")
  33. */
  34. public function lessThanWithColumn()
  35. {
  36. }
  37. /**
  38. * @\EasySwoole\HttpAnnotation\AnnotationTag\Param(name="columnA",betweenMbLen={3,5})
  39. * @\EasySwoole\HttpAnnotation\AnnotationTag\Param(name="columnB",mbLength="3")
  40. * @\EasySwoole\HttpAnnotation\AnnotationTag\Param(name="columnC",mbLengthMax="3")
  41. * @\EasySwoole\HttpAnnotation\AnnotationTag\Param(name="columnD",mbLengthMin="1")
  42. */
  43. public function mbLengthWithColumn()
  44. {
  45. }
  46. /**
  47. * @\EasySwoole\HttpAnnotation\AnnotationTag\Param(name="foo",greaterThanWithColumn="bar")
  48. */
  49. public function greaterThanWithColumn()
  50. {
  51. }
  52. /**
  53. * @\EasySwoole\HttpAnnotation\AnnotationTag\Param(name="foo",required="",notEmpty="",deprecated=true)
  54. */
  55. public function deprecated()
  56. {
  57. }
  58. /**
  59. * @\EasySwoole\HttpAnnotation\AnnotationTag\Param(name="foo",required="",notEmpty="")
  60. */
  61. public function notDeprecated()
  62. {
  63. }
  64. /**
  65. * @\EasySwoole\HttpAnnotation\AnnotationTag\Param(name="string",type="string")
  66. * @\EasySwoole\HttpAnnotation\AnnotationTag\Param(name="int",type="int")
  67. * @\EasySwoole\HttpAnnotation\AnnotationTag\Param(name="float",type="float")
  68. * @\EasySwoole\HttpAnnotation\AnnotationTag\Param(name="bool",type="bool")
  69. * @\EasySwoole\HttpAnnotation\AnnotationTag\Param(name="json",type="json")
  70. * @\EasySwoole\HttpAnnotation\AnnotationTag\Param(name="array",type="array")
  71. */
  72. public function paramType(string $string, int $int, float $float, bool $bool, \stdClass $json, array $array)
  73. {
  74. if (gettype($string) !== 'string' ||
  75. gettype($int) !== 'integer' ||
  76. gettype($float) !== 'double' ||
  77. gettype($bool) !== 'boolean' ||
  78. gettype($json) !== 'object' ||
  79. gettype($array) !== 'array'
  80. ) {
  81. $this->response()->write('error');
  82. } else {
  83. $this->response()->write('success');
  84. }
  85. }
  86. protected function onException(\Throwable $throwable): void
  87. {
  88. if ($throwable instanceof ParamValidateError) {
  89. $validate = $throwable->getValidate();
  90. $rule = $validate->getError()->getErrorRule();
  91. if (in_array($rule, ['lessThanWithColumn', 'greaterThanWithColumn',"betweenMbLen","mbLength","mbLengthMax","mbLengthMin"])) {
  92. throw new \Exception($validate->getError()->__toString());
  93. }
  94. }
  95. parent::onException($throwable); // TODO: Change the autogenerated stub
  96. }
  97. }