Explorar o código

修改手机号的验证正则

llp hai 1 día
pai
achega
cdc4b03d98

+ 1 - 1
packages/Webkul/BagistoApi/src/Resources/lang/en/app.php

@@ -109,7 +109,7 @@ return [
             'id-required'                       => 'Customer ID is required',
             'invalid-id-format'                 => 'Invalid ID format. Expected IRI format like "/api/admin/customers/1" or numeric ID',
             'not-found'                         => 'Customer not found',
-            'phone-special-chars-not-allowed'   => 'Mobile number can only contain digits. Special characters are not allowed',
+            'phone-invalid-format'              => 'Invalid phone number format. Please use a valid phone number, e.g. "+1 212 316 5641"',
             'invalid-gender'                    => 'Invalid gender value ":gender". Allowed values are: :valid',
         ],
 

+ 27 - 11
packages/Webkul/BagistoApi/src/State/CustomerProfileProcessor.php

@@ -139,9 +139,8 @@ class CustomerProfileProcessor implements ProcessorInterface
         }
 
         if (is_object($data) && property_exists($data, 'phone') && ! empty($data->phone)) {
-            // Validate phone - no special characters allowed
-            $this->validatePhone($data->phone);
-            $updateData['phone'] = $data->phone;
+            // Validate phone - supports international format with country code, e.g. "+1 212 316 5641"
+            $updateData['phone'] = $this->validatePhone($data->phone);
         }
 
         if (is_object($data) && property_exists($data, 'gender') && ! empty($data->gender)) {
@@ -314,22 +313,39 @@ class CustomerProfileProcessor implements ProcessorInterface
     }
 
     /**
-     * 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 should only contain digits - remove all non-digit characters
-        $cleanedPhone = preg_replace('/[^0-9]/', '', $phone);
+        $phone = trim($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'));
+        // "+" 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'));
         }
+
+        // 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;
     }
 }

+ 29 - 12
packages/Webkul/BagistoApi/src/Validators/CustomerValidator.php

@@ -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;
     }
 
     /**