RegisterController.php 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. <?php
  2. namespace Controllers;
  3. use Libs\ResponseLib;
  4. use Models\UserModel;
  5. use Psr\Http\Message\ServerRequestInterface;
  6. use Respect\Validation\Validator as val;
  7. use Respect\Validation\Exceptions\ValidationException;
  8. class RegisterController
  9. {
  10. private UserModel $userModel;
  11. public function __construct()
  12. {
  13. $this->userModel = new UserModel();
  14. }
  15. public function __invoke(ServerRequestInterface $request)
  16. {
  17. $body = json_decode((string) $request->getBody(), true) ?? [];
  18. try {
  19. val::key('username', val::stringType()->notEmpty()->length(1, 100))
  20. ->key('email', val::email())
  21. ->key('password', val::stringType()->length(8, null))
  22. ->key('phone', val::stringType()->notEmpty()->length(1, 50))
  23. ->key('address', val::stringType()->notEmpty()->length(1, 255))
  24. ->key('city', val::stringType()->notEmpty()->length(1, 100))
  25. ->key('state', val::stringType()->notEmpty()->length(1, 100))
  26. ->key('zip', val::stringType()->notEmpty()->length(1, 20))
  27. ->key('country', val::stringType()->notEmpty()->length(1, 100))
  28. ->key('kyc', val::intType())
  29. ->key('birthdate', val::intType())
  30. ->key('cpf', val::stringType()->notEmpty()->length(1, 50))
  31. ->key('company_id', val::intType()->positive())
  32. ->key('role_id', val::intType()->positive())
  33. ->assert($body);
  34. } catch (ValidationException $e) {
  35. return ResponseLib::sendFail("Validation failed: " . $e->getFullMessage(), [], "E_VALIDATE")->withStatus(400);
  36. }
  37. $userData = $this->userModel->createUser($body);
  38. if (!$userData) {
  39. return ResponseLib::sendFail("Email already exists or creation failed", [], "E_VALIDATE")->withStatus(400);
  40. }
  41. return ResponseLib::sendOk($userData, "S_CREATED");
  42. }
  43. }