1-customer_validator.php 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. <?php
  2. namespace cx_newsletter;
  3. class customer_validator
  4. extends validator
  5. implements validator_interface {
  6. protected function validate() : ?string {
  7. $customer = $this->get_target();
  8. if (!$customer->is_complete()) {
  9. return 'Target is not complete.';
  10. }
  11. if (strlen($customer->name) < 4) {
  12. return 'Name of the customer must have minimum 4 chars.';
  13. }
  14. if (strlen($customer->name) > 64) {
  15. return 'Name of the customer must have less than 64 chars.';
  16. }
  17. if (strlen($customer->surname) > 64) {
  18. return 'Surname of the customer must have less than 64 chars.';
  19. }
  20. if (strlen($customer->company) > 128) {
  21. return 'Company name must have less than 128 chars.';
  22. }
  23. if (strlen($customer->comments) > 255) {
  24. return 'Comments of the customer must be shorten than 255 chars.';
  25. }
  26. $has_email = !empty($customer->email);
  27. $has_phone = !empty($customer->phone_number);
  28. $phone_validator = new \cx_appengine\validator('phone');
  29. $email_validator = new \cx_appengine\validator('email');
  30. $good_email = $email_validator->validate($customer->email);
  31. $good_phone = true;
  32. if (strlen($customer->phone_number) > 12) {
  33. $good_phone = false;
  34. }
  35. if (strlen($customer->phone_number) < 9) {
  36. $good_phone = false;
  37. }
  38. if ($has_email and !$good_email) {
  39. return 'Email is not valid!';
  40. }
  41. if ($has_phone and !$good_phone) {
  42. return 'Phone number is not valid.';
  43. }
  44. if (!$has_email and !$has_phone) {
  45. return 'Customer must have phone number, email or both.';
  46. }
  47. $email_filter = [ 'email' => $customer->email ];
  48. $phone_filter = [ 'phone_number' => $customer->phone_number ];
  49. $emails = $this->get_mapper()->load_all($email_filter);
  50. $phones = $this->get_mapper()->load_all($phone_filter);
  51. $id = null;
  52. if ($customer->has_id()) {
  53. $id = $customer->get_id();
  54. }
  55. foreach ($emails as $collision) {
  56. if (!$has_email) {
  57. break;
  58. }
  59. if ($id !== null and $collision->get_id() === $id) {
  60. continue;
  61. }
  62. return 'Customer with that E-Mail already exists.';
  63. }
  64. foreach ($phones as $collision) {
  65. if (!$has_phone) {
  66. break;
  67. }
  68. if ($id !== null and $collision->get_id() === $id) {
  69. continue;
  70. }
  71. return 'Customer with that phone number already exists.';
  72. }
  73. return null;
  74. }
  75. public function prevalidate(\wpdb $database, table_names $tables) : void {
  76. $this->mapper = new customers_mapper($database, $tables);
  77. }
  78. protected function get_mapper() : customers_mapper {
  79. return $this->mapper;
  80. }
  81. private customers_mapper $mapper;
  82. }