From 403ead674d6d55ead33add20c44cf6e2669b1b18 Mon Sep 17 00:00:00 2001 From: Kalle Fagerberg Date: Sat, 17 Sep 2022 17:20:26 +0200 Subject: [PATCH] Changed to implement JsonSerializable --- lib/Controller/PersonalSettingsController.php | 6 +----- lib/Core/PodcastData/PodcastData.php | 20 +++++++++++++++---- lib/Core/PodcastData/PodcastMetrics.php | 14 ++++++++++++- 3 files changed, 30 insertions(+), 10 deletions(-) diff --git a/lib/Controller/PersonalSettingsController.php b/lib/Controller/PersonalSettingsController.php index 4d7d417..3e1cb05 100644 --- a/lib/Controller/PersonalSettingsController.php +++ b/lib/Controller/PersonalSettingsController.php @@ -35,12 +35,8 @@ class PersonalSettingsController extends Controller { */ public function metrics(): JSONResponse { $metrics = $this->metricsReader->metrics($this->userId); - - $metricsArrays = array_map(function (PodcastMetrics $metric) { - return $metric->toArray(); - }, $metrics); return new JSONResponse([ - 'subscriptions' => $metricsArrays, + 'subscriptions' => $metrics, ]); } } diff --git a/lib/Core/PodcastData/PodcastData.php b/lib/Core/PodcastData/PodcastData.php index 8b7ee10..e23b40f 100644 --- a/lib/Core/PodcastData/PodcastData.php +++ b/lib/Core/PodcastData/PodcastData.php @@ -4,9 +4,10 @@ declare(strict_types=1); namespace OCA\GPodderSync\Core\PodcastData; use DateTime; +use JsonSerializable; use SimpleXMLElement; -class PodcastData { +class PodcastData implements JsonSerializable { private string $title; private string $author; private string $link; @@ -30,6 +31,10 @@ class PodcastData { $this->fetchedAtUnix = $fetchedAtUnix; } + /** + * @return PodcastData + * @throws Exception if the XML data could not be parsed. + */ public static function parseRssXml(string $xmlString, ?int $fetchedAtUnix = null): PodcastData { $xml = new SimpleXMLElement($xmlString); $channel = $xml->channel; @@ -45,7 +50,7 @@ class PodcastData { ); } - private static function getXPathContent(SimpleXMLElement $xml, string $xpath) { + private static function getXPathContent(SimpleXMLElement $xml, string $xpath): ?string { $match = $xml->xpath($xpath); if ($match) { return (string)$match[0]; @@ -53,7 +58,7 @@ class PodcastData { return null; } - private static function getXPathAttribute(SimpleXMLElement $xml, string $xpath) { + private static function getXPathAttribute(SimpleXMLElement $xml, string $xpath): ?string { $match = $xml->xpath($xpath); if ($match) { return (string)$match[0][0]; @@ -111,7 +116,7 @@ class PodcastData { } /** - * @return array + * @return array */ public function toArray(): array { return @@ -125,6 +130,13 @@ class PodcastData { ]; } + /** + * @return array + */ + public function jsonSerialize(): mixed { + return $this->toArray(); + } + /** * @return PodcastData */ diff --git a/lib/Core/PodcastData/PodcastMetrics.php b/lib/Core/PodcastData/PodcastMetrics.php index aa76262..927cade 100644 --- a/lib/Core/PodcastData/PodcastMetrics.php +++ b/lib/Core/PodcastData/PodcastMetrics.php @@ -3,7 +3,9 @@ declare(strict_types=1); namespace OCA\GPodderSync\Core\PodcastData; -class PodcastMetrics { +use JsonSerializable; + +class PodcastMetrics implements JsonSerializable { private string $url; private int $listenedSeconds; private PodcastActionCounts $actionCounts; @@ -56,6 +58,9 @@ class PodcastMetrics { return $this->podcastData; } + /** + * @return array + */ public function toArray(): array { return [ @@ -65,4 +70,11 @@ class PodcastMetrics { 'podcastData' => $this->podcastData->toArray(), ]; } + + /** + * @return array + */ + public function jsonSerialize(): mixed { + return $this->toArray(); + } }