nextcloud-gpodder/lib/Core/EpisodeAction/EpisodeActionReader.php
Jonathan Flueren 7813339a95 Allow EpisodeAction upload as array
Parsing EpisodeActions from String can lead to problems if the String is changed by only a little bits.
Additionally, the gpodder api describes the upload as Array.
Uploading EpisodeActions as String is still possible.
2021-10-06 19:40:52 +02:00

81 lines
2.1 KiB
PHP

<?php
declare(strict_types=1);
namespace OCA\GPodderSync\Core\EpisodeAction;
class EpisodeActionReader
{
const EPISODEACTION_IDENTIFIER = 'EpisodeAction{';
/**
* @param string $episodeActionsString
* @return EpisodeAction[]
*/
public function fromString(string $episodeActionsString): array
{
$patterns = [
'/EpisodeAction{(podcast=\')(?<podcast>.*?)(\', episode=\')(?<episode>.*?)(\', guid=\')(?<guid>.*?)(\', action=)(?<action>.*?)(, timestamp=)(?<timestamp>.*?)(, started=)(?<started>.*?)(, position=)(?<position>.*?)(, total=)(?<total>.*?)}]*/',
'/EpisodeAction{(podcast=\')(?<podcast>.*?)(\', episode=\')(?<episode>.*?)(\', action=)(?<action>.*?)(, timestamp=)(?<timestamp>.*?)(, started=)(?<started>.*?)(, position=)(?<position>.*?)(, total=)(?<total>.*?)}]*/',
];
$episodeActions = [];
$episodeActionStrings = explode(self::EPISODEACTION_IDENTIFIER, $episodeActionsString);
array_shift($episodeActionStrings);
foreach ($episodeActionStrings as $episodeActionString) {
foreach ($patterns as $pattern) {
preg_match(
$pattern,
self::EPISODEACTION_IDENTIFIER . $episodeActionString,
$matches
);
if ($matches["action"] !== null) {
$episodeActions[] = new EpisodeAction(
$matches["podcast"],
$matches["episode"],
$matches["action"],
$matches["timestamp"],
(int)$matches["started"],
(int)$matches["position"],
(int)$matches["total"],
$matches["guid"] ?? null,
null,
);
break;
}
}
}
return $episodeActions;
}
/**
* @param $episodeActionsArray[]
* @return EpisodeAction[]
*/
public function fromArray(array $episodeActionsArray): array
{
$episodeActions = [];
foreach($episodeActionsArray as $episodeAction) {
$episodeActions[] = new EpisodeAction(
$episodeAction["podcast"],
$episodeAction["episode"],
$episodeAction["action"],
$episodeAction["timestamp"],
$episodeAction["started"],
$episodeAction["position"],
$episodeAction["total"],
$episodeAction["guid"] ?? null,
);
}
return $episodeActions;
}
}