From 58a8070b3ad40a4b8ee6ee7f18928d124af1a461 Mon Sep 17 00:00:00 2001
From: Alejandro Celaya <alejandrocelaya@gmail.com>
Date: Tue, 19 Nov 2024 20:16:33 +0100
Subject: [PATCH] Add Visit::toArray() method

---
 module/Core/src/Visit/Entity/Visit.php | 16 ++++++++++++++--
 1 file changed, 14 insertions(+), 2 deletions(-)

diff --git a/module/Core/src/Visit/Entity/Visit.php b/module/Core/src/Visit/Entity/Visit.php
index 9e8540bc5..2879ef66e 100644
--- a/module/Core/src/Visit/Entity/Visit.php
+++ b/module/Core/src/Visit/Entity/Visit.php
@@ -148,6 +148,15 @@ public function getType(): VisitType
     }
 
     public function jsonSerialize(): array
+    {
+        return $this->toArray();
+    }
+
+    /**
+     * @phpstan-type VisitedShortUrl array{shortCode: string, domain: string|null, shortUrl: string}
+     * @param (callable(ShortUrl $shortUrl): VisitedShortUrl)|null $visitedShortUrlToArray
+     */
+    public function toArray(callable|null $visitedShortUrlToArray = null): array
     {
         $base = [
             'referer' => $this->referer,
@@ -157,8 +166,11 @@ public function jsonSerialize(): array
             'potentialBot' => $this->potentialBot,
             'visitedUrl' => $this->visitedUrl,
         ];
-        if (! $this->isOrphan()) {
-            return $base;
+        if ($this->shortUrl !== null) {
+            return $visitedShortUrlToArray === null ? $base : [
+                ...$base,
+                'visitedShortUrl' => $visitedShortUrlToArray($this->shortUrl),
+            ];
         }
 
         return [