Changed to implement JsonSerializable

This commit is contained in:
Kalle Fagerberg 2022-09-17 17:20:26 +02:00 committed by thrillfall
parent 2b82e9e9c9
commit 403ead674d
3 changed files with 30 additions and 10 deletions

View File

@ -35,12 +35,8 @@ class PersonalSettingsController extends Controller {
*/ */
public function metrics(): JSONResponse { public function metrics(): JSONResponse {
$metrics = $this->metricsReader->metrics($this->userId); $metrics = $this->metricsReader->metrics($this->userId);
$metricsArrays = array_map(function (PodcastMetrics $metric) {
return $metric->toArray();
}, $metrics);
return new JSONResponse([ return new JSONResponse([
'subscriptions' => $metricsArrays, 'subscriptions' => $metrics,
]); ]);
} }
} }

View File

@ -4,9 +4,10 @@ declare(strict_types=1);
namespace OCA\GPodderSync\Core\PodcastData; namespace OCA\GPodderSync\Core\PodcastData;
use DateTime; use DateTime;
use JsonSerializable;
use SimpleXMLElement; use SimpleXMLElement;
class PodcastData { class PodcastData implements JsonSerializable {
private string $title; private string $title;
private string $author; private string $author;
private string $link; private string $link;
@ -30,6 +31,10 @@ class PodcastData {
$this->fetchedAtUnix = $fetchedAtUnix; $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 { public static function parseRssXml(string $xmlString, ?int $fetchedAtUnix = null): PodcastData {
$xml = new SimpleXMLElement($xmlString); $xml = new SimpleXMLElement($xmlString);
$channel = $xml->channel; $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); $match = $xml->xpath($xpath);
if ($match) { if ($match) {
return (string)$match[0]; return (string)$match[0];
@ -53,7 +58,7 @@ class PodcastData {
return null; return null;
} }
private static function getXPathAttribute(SimpleXMLElement $xml, string $xpath) { private static function getXPathAttribute(SimpleXMLElement $xml, string $xpath): ?string {
$match = $xml->xpath($xpath); $match = $xml->xpath($xpath);
if ($match) { if ($match) {
return (string)$match[0][0]; return (string)$match[0][0];
@ -111,7 +116,7 @@ class PodcastData {
} }
/** /**
* @return array * @return array<string,mixed>
*/ */
public function toArray(): array { public function toArray(): array {
return return
@ -125,6 +130,13 @@ class PodcastData {
]; ];
} }
/**
* @return array<string,mixed>
*/
public function jsonSerialize(): mixed {
return $this->toArray();
}
/** /**
* @return PodcastData * @return PodcastData
*/ */

View File

@ -3,7 +3,9 @@ declare(strict_types=1);
namespace OCA\GPodderSync\Core\PodcastData; namespace OCA\GPodderSync\Core\PodcastData;
class PodcastMetrics { use JsonSerializable;
class PodcastMetrics implements JsonSerializable {
private string $url; private string $url;
private int $listenedSeconds; private int $listenedSeconds;
private PodcastActionCounts $actionCounts; private PodcastActionCounts $actionCounts;
@ -56,6 +58,9 @@ class PodcastMetrics {
return $this->podcastData; return $this->podcastData;
} }
/**
* @return array<string,mixed>
*/
public function toArray(): array { public function toArray(): array {
return return
[ [
@ -65,4 +70,11 @@ class PodcastMetrics {
'podcastData' => $this->podcastData->toArray(), 'podcastData' => $this->podcastData->toArray(),
]; ];
} }
/**
* @return array<string,mixed>
*/
public function jsonSerialize(): mixed {
return $this->toArray();
}
} }