Advertisement
Guest User

Untitled

a guest
Jul 12th, 2019
102
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 2.69 KB | None | 0 0
  1. / src/Method/UserMethod.php
  2. <?php
  3.  
  4. namespace App\Method;
  5.  
  6. use App\Domain\JsonRpcMethodWithDocInterface;
  7. use Symfony\Component\Validator\Constraint;
  8. use Symfony\Component\Validator\Constraints\Choice;
  9. use Symfony\Component\Validator\Constraints\Collection;
  10. use Symfony\Component\Validator\Constraints\Length;
  11. use Symfony\Component\Validator\Constraints\NotBlank;
  12. use Symfony\Component\Validator\Constraints\Optional;
  13. use Symfony\Component\Validator\Constraints\Positive;
  14. use Symfony\Component\Validator\Constraints\Required;
  15. use Yoanm\JsonRpcParamsSymfonyValidator\Domain\MethodWithValidatedParamsInterface;
  16. use Yoanm\JsonRpcServer\Domain\JsonRpcMethodInterface;
  17. use Yoanm\JsonRpcServerDoc\Domain\Model\ErrorDoc;
  18. use Yoanm\JsonRpcServerDoc\Domain\Model\Type\ArrayDoc;
  19. use Yoanm\JsonRpcServerDoc\Domain\Model\Type\NumberDoc;
  20. use Yoanm\JsonRpcServerDoc\Domain\Model\Type\ObjectDoc;
  21. use Yoanm\JsonRpcServerDoc\Domain\Model\Type\StringDoc;
  22. use Yoanm\JsonRpcServerDoc\Domain\Model\Type\TypeDoc;
  23.  
  24. class UserMethod implements JsonRpcMethodInterface, MethodWithValidatedParamsInterface, JsonRpcMethodWithDocInterface
  25. {
  26.     public function apply(array $paramList = null)
  27.     {
  28.         return [
  29.             'name' => $paramList['name'],
  30.             'age' => $paramList['age'],
  31.             'sex' => $paramList['sex'] ?? null,
  32.         ];
  33.     }
  34.  
  35.     public function getParamsConstraint() : Constraint
  36.     {
  37.         return new Collection(['fields' => [
  38.             'name' => new Required([
  39.                 new Length(['min' => 1, 'max' => 32])
  40.             ]),
  41.             'age' => new Required([
  42.                 new Positive()
  43.             ]),
  44.             'sex' => new Optional([
  45.                 new Choice(['f', 'm'])
  46.             ]),
  47.         ]]);
  48.     }
  49.  
  50.     public function getDocDescription(): string
  51.     {
  52.         return 'User method';
  53.     }
  54.  
  55.     public function getDocTag(): string
  56.     {
  57.         return 'main';
  58.     }
  59.  
  60.     public function getDocErrors(): array
  61.     {
  62.         return [new ErrorDoc('Error 1', 1)];
  63.     }
  64.  
  65.     public function getDocResponse(): TypeDoc
  66.     {
  67.         $response = new ObjectDoc();
  68.         $response->setNullable(false);
  69.  
  70.         $response->addSibling((new StringDoc())
  71.             ->setNullable(false)
  72.             ->setDescription('Name of user')
  73.             ->setName('name')
  74.         );
  75.  
  76.         $response->addSibling((new NumberDoc())
  77.             ->setNullable(false)
  78.             ->setDescription('Age of user')
  79.             ->setName('age')
  80.         );
  81.  
  82.         $response->addSibling((new StringDoc())
  83.             ->setNullable(true)
  84.             ->setDescription('Sex of user')
  85.             ->setName('sex')
  86.         );
  87.  
  88.         return $response;
  89.     }
  90. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement