Split PHP
This commit is contained in:
@@ -0,0 +1,191 @@
|
||||
<?php
|
||||
|
||||
class ProgrammeService
|
||||
{
|
||||
private string $timezone;
|
||||
|
||||
public function __construct(string $timezone = 'Europe/Vienna')
|
||||
{
|
||||
$this->timezone = $timezone;
|
||||
}
|
||||
|
||||
public function load(array $data, ?Config $config = null): array
|
||||
{
|
||||
$channelConfig = $config ? $config->getChannels() : [];
|
||||
$channels = [];
|
||||
$programmes = [];
|
||||
|
||||
foreach ($data['channels'] ?? [] as $id => $channel) {
|
||||
$cfg = $channelConfig[$id] ?? [];
|
||||
|
||||
if (($cfg['enabled'] ?? true) !== true) {
|
||||
continue;
|
||||
}
|
||||
|
||||
$channels[$id] = [
|
||||
'id' => $id,
|
||||
'name' => $cfg['name'] ?? $channel['name'] ?? $id,
|
||||
'icon' => $cfg['icon'] ?? $channel['icon'] ?? '',
|
||||
'number' => (int)($cfg['number'] ?? 999999),
|
||||
'favorite' => (bool)($cfg['favorite'] ?? false),
|
||||
];
|
||||
|
||||
foreach ($channel['programmes'] ?? [] as $p) {
|
||||
$programmes[$id][] = [
|
||||
'title' => $p['title'] ?? '',
|
||||
'desc' => $p['description'] ?? '',
|
||||
'category' => $p['category'] ?? '',
|
||||
'start' => (new DateTime())->setTimestamp((int)$p['start'])->setTimezone(new DateTimeZone($this->timezone)),
|
||||
'stop' => (new DateTime())->setTimestamp((int)$p['stop'])->setTimezone(new DateTimeZone($this->timezone)),
|
||||
];
|
||||
}
|
||||
}
|
||||
|
||||
uasort($channels, function ($a, $b) {
|
||||
return ($a['number'] ?? 999999) <=> ($b['number'] ?? 999999);
|
||||
});
|
||||
|
||||
return [$channels, $programmes];
|
||||
}
|
||||
|
||||
public function getCurrentProgrammes(array $data, ?Config $config = null): array
|
||||
{
|
||||
$channelConfig = $config ? $config->getChannels() : [];
|
||||
$now = time();
|
||||
$items = [];
|
||||
|
||||
foreach ($data['channels'] ?? [] as $id => $channel) {
|
||||
$cfg = $channelConfig[$id] ?? [];
|
||||
|
||||
if (($cfg['enabled'] ?? true) !== true) {
|
||||
continue;
|
||||
}
|
||||
|
||||
foreach ($channel['programmes'] ?? [] as $p) {
|
||||
$start = (int)($p['start'] ?? 0);
|
||||
$stop = (int)($p['stop'] ?? 0);
|
||||
|
||||
if ($now >= $start && $now < $stop) {
|
||||
$duration = max(1, $stop - $start);
|
||||
$elapsed = max(0, $now - $start);
|
||||
$percent = min(100, round(($elapsed / $duration) * 100));
|
||||
|
||||
$items[] = [
|
||||
'channel_id' => $id,
|
||||
'channel_name' => $cfg['name'] ?? $channel['name'] ?? $id,
|
||||
'channel_icon' => $cfg['icon'] ?? $channel['icon'] ?? '',
|
||||
'channel_number' => (int)($cfg['number'] ?? 999999),
|
||||
'favorite' => (bool)($cfg['favorite'] ?? false),
|
||||
'title' => $p['title'] ?? '',
|
||||
'subtitle' => $p['subtitle'] ?? '',
|
||||
'description' => $p['description'] ?? '',
|
||||
'category' => $p['category'] ?? '',
|
||||
'start' => $start,
|
||||
'stop' => $stop,
|
||||
'percent' => $percent,
|
||||
'remaining_minutes' => max(0, (int)ceil(($stop - $now) / 60)),
|
||||
];
|
||||
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
usort($items, function ($a, $b) {
|
||||
return $a['channel_number'] <=> $b['channel_number'];
|
||||
});
|
||||
|
||||
return $items;
|
||||
}
|
||||
|
||||
public function getDayProgrammes(Config $config, array $data, string $date, ?string $channelId = null): array
|
||||
{
|
||||
$channelConfig = $config->getChannels();
|
||||
|
||||
$tz = new DateTimeZone($this->timezone);
|
||||
$dayStart = new DateTime($date . ' 00:00:00', $tz);
|
||||
$dayEnd = clone $dayStart;
|
||||
$dayEnd->modify('+1 day');
|
||||
|
||||
$startTs = $dayStart->getTimestamp();
|
||||
$endTs = $dayEnd->getTimestamp();
|
||||
|
||||
$items = [];
|
||||
|
||||
foreach ($data['channels'] ?? [] as $id => $channel) {
|
||||
if ($channelId !== null && $id !== $channelId) {
|
||||
continue;
|
||||
}
|
||||
|
||||
$cfg = $channelConfig[$id] ?? [];
|
||||
|
||||
if (($cfg['enabled'] ?? true) !== true) {
|
||||
continue;
|
||||
}
|
||||
|
||||
$programmes = [];
|
||||
|
||||
foreach ($channel['programmes'] ?? [] as $p) {
|
||||
$start = (int)($p['start'] ?? 0);
|
||||
$stop = (int)($p['stop'] ?? 0);
|
||||
|
||||
if ($start < $endTs && $stop > $startTs) {
|
||||
$visibleStart = max($start, $startTs);
|
||||
$visibleStop = min($stop, $endTs);
|
||||
|
||||
$startDt = (new DateTime())->setTimestamp($visibleStart)->setTimezone($tz);
|
||||
$stopDt = (new DateTime())->setTimestamp($visibleStop)->setTimezone($tz);
|
||||
|
||||
$startMinute = ((int)$startDt->format('H') * 60) + (int)$startDt->format('i');
|
||||
$stopMinute = ((int)$stopDt->format('H') * 60) + (int)$stopDt->format('i');
|
||||
|
||||
if ($visibleStop >= $endTs) {
|
||||
$stopMinute = 1440;
|
||||
}
|
||||
|
||||
$durationMinutes = max(1, (int)(($visibleStop - $visibleStart) / 60));
|
||||
|
||||
$programmes[] = [
|
||||
'id' => md5($id . '|' . $start . '|' . $stop . '|' . ($p['title'] ?? '')),
|
||||
'title' => $p['title'] ?? '',
|
||||
'subtitle' => $p['subtitle'] ?? '',
|
||||
'description' => $p['description'] ?? '',
|
||||
'category' => $p['category'] ?? '',
|
||||
'start' => $start,
|
||||
'stop' => $stop,
|
||||
'visible_start' => $visibleStart,
|
||||
'visible_stop' => $visibleStop,
|
||||
'start_minute' => $startMinute,
|
||||
'stop_minute' => $stopMinute,
|
||||
'duration_minutes' => $durationMinutes,
|
||||
'is_current' => time() >= $start && time() < $stop,
|
||||
'is_past' => $stop < time(),
|
||||
];
|
||||
}
|
||||
}
|
||||
|
||||
if (!$programmes) {
|
||||
continue;
|
||||
}
|
||||
|
||||
usort($programmes, function ($a, $b) {
|
||||
return $a['start'] <=> $b['start'];
|
||||
});
|
||||
|
||||
$items[] = [
|
||||
'channel_id' => $id,
|
||||
'channel_name' => $cfg['name'] ?? $channel['name'] ?? $id,
|
||||
'channel_icon' => $cfg['icon'] ?? $channel['icon'] ?? '',
|
||||
'channel_number' => (int)($cfg['number'] ?? 999999),
|
||||
'favorite' => (bool)($cfg['favorite'] ?? false),
|
||||
'programmes' => $programmes,
|
||||
];
|
||||
}
|
||||
|
||||
usort($items, function ($a, $b) {
|
||||
return $a['channel_number'] <=> $b['channel_number'];
|
||||
});
|
||||
|
||||
return $items;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user