]> gitweb.erp-flowers.ru Git - erp24_rep/yii-erp24/.git/commitdiff
[ERP-166] новый метод очистки телефонных номеров со справочником feature_smirnov_erp-166_clear_phone origin/feature_smirnov_erp-166_clear_phone
authorAlexander Smirnov <fredeom@mail.ru>
Fri, 30 Aug 2024 12:23:33 +0000 (15:23 +0300)
committerAlexander Smirnov <fredeom@mail.ru>
Fri, 30 Aug 2024 12:23:33 +0000 (15:23 +0300)
erp24/helpers/ClientHelper.php

index c2ecb6ea77131b966d991792d2347c8474c7b5b8..7452fec845c134514c635a54467dcc37076e40b9 100644 (file)
@@ -13,45 +13,32 @@ class ClientHelper {
     }
 
     public static function phoneClear($phone) {
+        $prefixes = [
+            7 => 10,    // Russia
+            39 => 10,   // Italy
+            38 => 10,   // Ukrain
+        ];
         $phone = preg_replace("/[^0-9\,]/i", "", $phone);
-        //   $urlstr = preg_replace('/[^A-Za-z0-9_\-]/', '', $urlstr);
+        $phones = explode(",", $phone);
+        foreach ($phones as $ind => $p) {
+            if ($ind == 0) { continue; }
+            $phones[$ind] = clearPhone($p);
+        }
+        $phone = $phones[0];
         $phone_str = strlen($phone);
-        if ($phone_str == 10 && $phone[0] != '7') {
-            $phone = "7$phone";
-        } elseif ($phone_str == 11) {
-            $str0 = substr($phone, 0, 1);
-            if ($str0 == 8) {
-                $phone = substr($phone, 1,11);
-                $phone = "7$phone";
+        if ($phone_str > 1) {
+            if ($phone[0] == 8) {
+                $phone = '7' . substr($phone, 1);
             }
-        } elseif ($phone_str > 11) {
-            $phone_arr = explode(",", $phone);
-            $ph = "";
-            $i = 0 ;
-            foreach($phone_arr as $phone1) {
-                if (!empty($phone1)) {
-                    $i++;
-                    $phone_str2 = strlen($phone1);
-                    if ($phone_str2 == 11) {
-                        $str01 = substr($phone1, 0, 1);
-                        if ($str01 == 8) {
-                            $phone1 = substr($phone1, 1,11);
-                            $ph .="7$phone1";
-                        } else {
-                            $ph .=$phone1;
-                        }
-                    } else {
-                        $ph .= "$phone1";
-                    }
-                }
-                $cnt = count($phone_arr);
-                if ($i < $cnt and !empty($cnt)) {
-                    $ph .= ",";
+            foreach ($prefixes as $p => $cnt) {
+                if (str_starts_with($phone, $p)) {
+                    $phone = $p . substr($phone, strlen($p), $cnt);
+                    break;
                 }
             }
-            $phone =$ph;
-            //echo"=".count($phone_arr)."=";
         }
+        $phones[0] = $phone;
+        $phone = implode(',', $phones);
         return $phone;
     }