|
|
@@ -46,7 +46,7 @@ class CustomerValidator
|
|
|
}
|
|
|
|
|
|
// Additional custom validations
|
|
|
- $this->validatePhone($customer->phone);
|
|
|
+ $customer->phone = $this->validatePhone($customer->phone);
|
|
|
$this->validateGender($customer->gender);
|
|
|
}
|
|
|
|
|
|
@@ -78,10 +78,10 @@ class CustomerValidator
|
|
|
}
|
|
|
|
|
|
if ($customer->phone !== null) {
|
|
|
+ // Validate phone format and normalize before storage
|
|
|
+ $customer->phone = $this->validatePhone($customer->phone);
|
|
|
$data['phone'] = $customer->phone;
|
|
|
$rules['phone'] = 'string|unique:customers,phone,'.$customer->id;
|
|
|
- // Validate phone format
|
|
|
- $this->validatePhone($customer->phone);
|
|
|
}
|
|
|
|
|
|
// Validate gender if provided
|
|
|
@@ -109,23 +109,40 @@ class CustomerValidator
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
- * Validate phone number - only digits allowed
|
|
|
+ * Validate phone number - supports international format.
|
|
|
+ *
|
|
|
+ * Allowed formats (optional "+" country code prefix, digits separated by
|
|
|
+ * spaces, hyphens, dots or parentheses):
|
|
|
+ * +1 2123165641, +86 138 0013 8000, (212) 316-5641, 2123165641
|
|
|
+ *
|
|
|
+ * Returns the normalized number (e.g. "+12123165641") for storage.
|
|
|
*
|
|
|
* @throws InvalidInputException
|
|
|
*/
|
|
|
- private function validatePhone(?string $phone): void
|
|
|
+ private function validatePhone(?string $phone): ?string
|
|
|
{
|
|
|
if ($phone === null || $phone === '') {
|
|
|
- return;
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+
|
|
|
+ $phone = trim($phone);
|
|
|
+
|
|
|
+ // "+" may only appear once, at the very beginning; the rest must be
|
|
|
+ // digits with optional space/hyphen/dot/parenthesis separators.
|
|
|
+ if (! preg_match('/^\+?\d[\d\s\-()\.]*$/', $phone)) {
|
|
|
+ throw new InvalidInputException(__('bagistoapi::app.graphql.customer.phone-invalid-format'));
|
|
|
}
|
|
|
|
|
|
- // Phone should only contain digits - remove all non-digit characters
|
|
|
- $cleanedPhone = preg_replace('/[^0-9]/', '', $phone);
|
|
|
-
|
|
|
- // If the cleaned phone is different from original, it means special characters were present
|
|
|
- if ($cleanedPhone !== $phone) {
|
|
|
- throw new InvalidInputException(__('bagistoapi::app.graphql.customer.phone-special-chars-not-allowed'));
|
|
|
+ // After stripping separators, the number of digits must match the
|
|
|
+ // international E.164 range (7 to 15 digits).
|
|
|
+ $digits = preg_replace('/[^0-9]/', '', $phone);
|
|
|
+
|
|
|
+ if (strlen($digits) < 7 || strlen($digits) > 15) {
|
|
|
+ throw new InvalidInputException(__('bagistoapi::app.graphql.customer.phone-invalid-format'));
|
|
|
}
|
|
|
+
|
|
|
+ // Normalize for storage: optional leading "+" followed by plain digits.
|
|
|
+ return (str_starts_with($phone, '+') ? '+' : '').$digits;
|
|
|
}
|
|
|
|
|
|
/**
|