| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113 |
- <?php
- namespace cx_newsletter;
- class customer_validator
- extends validator
- implements validator_interface {
- protected function validate() : ?string {
- $customer = $this->get_target();
-
- if (!$customer->is_complete()) {
- return 'Target is not complete.';
- }
- if (strlen($customer->name) < 4) {
- return 'Name of the customer must have minimum 4 chars.';
- }
- if (strlen($customer->name) > 64) {
- return 'Name of the customer must have less than 64 chars.';
- }
- if (strlen($customer->surname) > 64) {
- return 'Surname of the customer must have less than 64 chars.';
- }
- if (strlen($customer->company) > 128) {
- return 'Company name must have less than 128 chars.';
- }
- if (strlen($customer->comments) > 255) {
- return 'Comments of the customer must be shorten than 255 chars.';
- }
- $has_email = !empty($customer->email);
- $has_phone = !empty($customer->phone_number);
- $phone_validator = new \cx_appengine\validator('phone');
- $email_validator = new \cx_appengine\validator('email');
- $good_email = $email_validator->validate($customer->email);
- $good_phone = true;
- if (strlen($customer->phone_number) > 12) {
- $good_phone = false;
- }
- if (strlen($customer->phone_number) < 9) {
- $good_phone = false;
- }
- if ($has_email and !$good_email) {
- return 'Email is not valid!';
- }
- if ($has_phone and !$good_phone) {
- return 'Phone number is not valid.';
- }
- if (!$has_email and !$has_phone) {
- return 'Customer must have phone number, email or both.';
- }
- $email_filter = [ 'email' => $customer->email ];
- $phone_filter = [ 'phone_number' => $customer->phone_number ];
- $emails = $this->get_mapper()->load_all($email_filter);
- $phones = $this->get_mapper()->load_all($phone_filter);
-
- $id = null;
- if ($customer->has_id()) {
- $id = $customer->get_id();
- }
- foreach ($emails as $collision) {
- if (!$has_email) {
- break;
- }
- if ($id !== null and $collision->get_id() === $id) {
- continue;
- }
-
- return 'Customer with that E-Mail already exists.';
- }
- foreach ($phones as $collision) {
- if (!$has_phone) {
- break;
- }
- if ($id !== null and $collision->get_id() === $id) {
- continue;
- }
-
- return 'Customer with that phone number already exists.';
- }
- return null;
- }
- public function prevalidate(\wpdb $database, table_names $tables) : void {
- $this->mapper = new customers_mapper($database, $tables);
- }
- protected function get_mapper() : customers_mapper {
- return $this->mapper;
- }
- private customers_mapper $mapper;
- }
|