CprPreregistrationController.php 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. <?php
  2. namespace Controllers;
  3. use Libs\ResponseLib;
  4. use Models\CprPreregistrationModel;
  5. use Psr\Http\Message\ServerRequestInterface;
  6. use Respect\Validation\Validator as v;
  7. class CprPreregistrationController
  8. {
  9. public function __invoke(ServerRequestInterface $request)
  10. {
  11. $body = json_decode((string) $request->getBody(), true) ?? [];
  12. // Extract and trim inputs
  13. $name = isset($body['name']) ? trim((string)$body['name']) : '';
  14. $contactNumber = isset($body['contact_number']) ? trim((string)$body['contact_number']) : '';
  15. $email = isset($body['email']) ? trim((string)$body['email']) : '';
  16. $productType = isset($body['product_type']) ? trim((string)$body['product_type']) : '';
  17. $internalControlNumber = isset($body['internal_control_number']) ? trim((string)$body['internal_control_number']) : '';
  18. $productQuantityRaw = isset($body['product_quantity']) ? trim((string)$body['product_quantity']) : '';
  19. $propertyName = isset($body['property_name']) ? trim((string)$body['property_name']) : '';
  20. $propertyLocation = isset($body['property_location']) ? trim((string)$body['property_location']) : '';
  21. // Define validation rules
  22. $rules = [
  23. 'name' => v::stringType()->notEmpty()->length(2, 120),
  24. // Accept digits, spaces, plus, dash, parentheses
  25. 'contact_number' => v::stringType()->notEmpty()->regex('/^[0-9+()\\s-]{8,25}$/'),
  26. 'email' => v::email()->notEmpty(),
  27. 'product_type' => v::stringType()->notEmpty()->alnum(" -_/.,")->length(1, 100),
  28. 'internal_control_number' => v::stringType()->notEmpty()->alnum("-_/.")->length(1, 100),
  29. // Accept numeric strings convertible to int
  30. 'product_quantity' => v::stringType()->notEmpty()->alnum(" -_/.,")->length(1, 200),
  31. 'property_name' => v::stringType()->notEmpty()->alnum(" -_/.,")->length(1, 150),
  32. 'property_location' => v::stringType()->notEmpty()->alnum(" -_/.,")->length(1, 200),
  33. ];
  34. $inputs = [
  35. 'name' => $name,
  36. 'contact_number' => $contactNumber,
  37. 'email' => $email,
  38. 'product_type' => $productType,
  39. 'internal_control_number' => $internalControlNumber,
  40. 'product_quantity' => $productQuantityRaw,
  41. 'property_name' => $propertyName,
  42. 'property_location' => $propertyLocation,
  43. ];
  44. $errors = [];
  45. foreach ($inputs as $field => $value) {
  46. try {
  47. $rules[$field]->assert($value);
  48. } catch (\Throwable $e) {
  49. $errors[$field] = $e->getMessage();
  50. }
  51. }
  52. if (!empty($errors)) {
  53. return ResponseLib::sendFail("Validation failed", ['errors' => $errors], "E_VALIDATE")->withStatus(400);
  54. }
  55. // Keep as free text after validation
  56. $productQuantity = $productQuantityRaw;
  57. try {
  58. $model = new CprPreregistrationModel();
  59. $saved = $model->insert(
  60. $name,
  61. $contactNumber,
  62. $email,
  63. $productType,
  64. $internalControlNumber,
  65. $productQuantity,
  66. $propertyName,
  67. $propertyLocation
  68. );
  69. return ResponseLib::sendOk($saved, "S_CREATED");
  70. } catch (\Throwable $e) {
  71. return ResponseLib::sendFail("Database error: " . $e->getMessage(), [], "E_DB")->withStatus(500);
  72. }
  73. }
  74. }