From e3d9f96441ffdbd6a7e0b3308d2f806a285ff7cc Mon Sep 17 00:00:00 2001 From: Alejandro Celaya 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 [