2021-06-27 11:19:26 +00:00
|
|
|
<?php
|
|
|
|
declare(strict_types=1);
|
|
|
|
|
|
|
|
namespace OCA\GPodderSync\Core\EpisodeAction;
|
|
|
|
|
2021-10-25 12:02:18 +00:00
|
|
|
use InvalidArgumentException;
|
|
|
|
|
2021-10-07 12:33:16 +00:00
|
|
|
class EpisodeActionReader {
|
|
|
|
private array $requiredProperties = ['podcast', 'episode', 'action', 'timestamp'];
|
|
|
|
|
2021-10-04 15:59:05 +00:00
|
|
|
/**
|
2021-10-07 12:33:16 +00:00
|
|
|
* @param array $episodeActionsArray []
|
2021-10-04 15:59:05 +00:00
|
|
|
* @return EpisodeAction[]
|
2021-10-25 12:02:18 +00:00
|
|
|
* @throws InvalidArgumentException
|
2021-10-04 15:59:05 +00:00
|
|
|
*/
|
2021-10-07 12:33:16 +00:00
|
|
|
public function fromArray(array $episodeActionsArray): array {
|
2021-10-04 15:59:05 +00:00
|
|
|
$episodeActions = [];
|
|
|
|
|
2021-10-07 12:33:16 +00:00
|
|
|
foreach ($episodeActionsArray as $episodeAction) {
|
|
|
|
if ($this->hasRequiredProperties($episodeAction) === false) {
|
2021-10-25 12:02:18 +00:00
|
|
|
throw new InvalidArgumentException(sprintf('Client sent incomplete or invalid data: %s', json_encode($episodeAction, JSON_THROW_ON_ERROR)));
|
2021-10-07 12:33:16 +00:00
|
|
|
}
|
2021-10-04 15:59:05 +00:00
|
|
|
$episodeActions[] = new EpisodeAction(
|
|
|
|
$episodeAction["podcast"],
|
|
|
|
$episodeAction["episode"],
|
2021-10-06 15:01:41 +00:00
|
|
|
strtoupper($episodeAction["action"]),
|
2021-10-04 15:59:05 +00:00
|
|
|
$episodeAction["timestamp"],
|
2021-10-06 14:41:19 +00:00
|
|
|
$episodeAction["started"] ?? -1,
|
|
|
|
$episodeAction["position"] ?? -1,
|
|
|
|
$episodeAction["total"] ?? -1,
|
2021-10-04 15:59:05 +00:00
|
|
|
$episodeAction["guid"] ?? null,
|
2021-10-06 14:41:19 +00:00
|
|
|
null
|
2021-10-04 15:59:05 +00:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
return $episodeActions;
|
|
|
|
}
|
2021-10-07 12:33:16 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @param array $episodeAction
|
|
|
|
* @return bool
|
|
|
|
*/
|
|
|
|
private function hasRequiredProperties(array $episodeAction): bool {
|
|
|
|
return (count(array_intersect($this->requiredProperties, array_keys($episodeAction))) === count($this->requiredProperties));
|
|
|
|
}
|
2021-06-27 11:19:26 +00:00
|
|
|
}
|