Procházet zdrojové kódy

Tshield login fix (not tested)

EduLascala před 6 dny
rodič
revize
042591d2e1
2 změnil soubory, kde provedl 87 přidání a 5 odebrání
  1. 30 5
      controllers/LoginController.php
  2. 57 0
      services/TshieldService.php

+ 30 - 5
controllers/LoginController.php

@@ -120,14 +120,21 @@ class LoginController
         }
 
         if (is_numeric($value)) {
+            $numeric = (string)$value;
             $timestamp = (int)$value;
+
             if ($timestamp > 0) {
-                if (strlen((string)$timestamp) === 8) {
-                    $formatted = \DateTimeImmutable::createFromFormat('Ymd', (string)$timestamp);
+                if (strlen($numeric) === 8) {
+                    $formatted = \DateTimeImmutable::createFromFormat('Ymd', $numeric);
                     if ($formatted) {
                         return $formatted->format('Y-m-d');
                     }
                 }
+
+                if (strlen($numeric) >= 13) {
+                    $timestamp = (int) floor($timestamp / 1000);
+                }
+
                 if ($timestamp >= 1000000000) {
                     $dt = (new \DateTimeImmutable())->setTimestamp($timestamp);
                     return $dt->format('Y-m-d');
@@ -135,11 +142,29 @@ class LoginController
             }
         }
 
-        if (is_string($value) && preg_match('/^\d{4}-\d{2}-\d{2}$/', $value)) {
-            return $value;
+        if (is_string($value)) {
+            $value = trim($value);
+
+            if (preg_match('/^\d{4}-\d{2}-\d{2}$/', $value)) {
+                return $value;
+            }
+
+            if (preg_match('/^\d{8}$/', $value)) {
+                $formatted = \DateTimeImmutable::createFromFormat('Ymd', $value);
+                if ($formatted) {
+                    return $formatted->format('Y-m-d');
+                }
+            }
+
+            foreach (['d/m/Y', 'd-m-Y', 'Y/m/d', 'Y.m.d'] as $fmt) {
+                $formatted = \DateTimeImmutable::createFromFormat($fmt, $value);
+                if ($formatted) {
+                    return $formatted->format('Y-m-d');
+                }
+            }
         }
 
-        return (string)$value;
+        return null;
     }
 
     private function formatAddress(array $user): ?string

+ 57 - 0
services/TshieldService.php

@@ -270,6 +270,9 @@ class TshieldService
         }
 
         $defaultAdditional = [];
+        if (isset($analysisPayload['birthdate'])) {
+            $analysisPayload['birthdate'] = $this->normalizeBirthdate($analysisPayload['birthdate']);
+        }
         $fieldValues = [
             'name' => $analysisPayload['name'] ?? null,
             'document' => $analysisPayload['document'] ?? null,
@@ -311,6 +314,60 @@ class TshieldService
         return $payload;
     }
 
+    private function normalizeBirthdate($value): ?string
+    {
+        if ($value === null || $value === '') {
+            return null;
+        }
+
+        if (is_numeric($value)) {
+            $numeric = (string)$value;
+            $timestamp = (int)$value;
+
+            if ($timestamp > 0) {
+                if (strlen($numeric) === 8) {
+                    $formatted = \DateTimeImmutable::createFromFormat('Ymd', $numeric);
+                    if ($formatted) {
+                        return $formatted->format('Y-m-d');
+                    }
+                }
+
+                if (strlen($numeric) >= 13) {
+                    $timestamp = (int) floor($timestamp / 1000);
+                }
+
+                if ($timestamp >= 1000000000) {
+                    $dt = (new \DateTimeImmutable())->setTimestamp($timestamp);
+                    return $dt->format('Y-m-d');
+                }
+            }
+        }
+
+        if (is_string($value)) {
+            $value = trim($value);
+
+            if (preg_match('/^\d{4}-\d{2}-\d{2}$/', $value)) {
+                return $value;
+            }
+
+            if (preg_match('/^\d{8}$/', $value)) {
+                $formatted = \DateTimeImmutable::createFromFormat('Ymd', $value);
+                if ($formatted) {
+                    return $formatted->format('Y-m-d');
+                }
+            }
+
+            foreach (['d/m/Y', 'd-m-Y', 'Y/m/d', 'Y.m.d'] as $fmt) {
+                $formatted = \DateTimeImmutable::createFromFormat($fmt, $value);
+                if ($formatted) {
+                    return $formatted->format('Y-m-d');
+                }
+            }
+        }
+
+        return null;
+    }
+
     private function injectDefaults(array $payload, string $validationId): array
     {
         if (empty($payload['client_validation_id'])) {