<?phpnamespace App\Model;/** * Where each song really begins and ends (the blanks, an intro left out when it is played): found by WinZik in the sound * ('auto'), given by YouTube's chapters ('yt'), or set by hand ('manual', also written into the MP3's ID3 tag). Kept in * var/marks.json (every format). The marks set by hand win, then YouTube's, then the ones found. */final class Marks{ /** @var array<string, array{s: float, e: float, src: string}> per song (its full path) */ private array $songs = []; private bool $dirty = false; public function __construct(private string $file) { // written by WinZik::writeJsonFile(): a Latin-1 file name (not UTF-8) comes back as it was $data = \App\Core\WinZik::readJsonFile($file); foreach (is_array($data) ? $data : [] as $path => $m) { if (is_string($path) && is_array($m)) { $this->songs[$path] = ['s' => (float)($m['s'] ?? 0), 'e' => (float)($m['e'] ?? 0), 'src' => (string)($m['src'] ?? 'auto')]; } } } /** @return array{s: float, e: float, src: string}|null (e = 0: to its end) */ public function get(string $path): ?array { return $this->songs[$path] ?? null; } public function set(string $path, float $start, float $end, string $src): void { $old = $this->songs[$path] ?? null; // a lesser source does not replace a better one (the ones set by hand stay) $rank = ['auto' => 0, 'yt' => 1, 'manual' => 2]; if ($old !== null && ($rank[$old['src']] ?? 0) > ($rank[$src] ?? 0)) { return; } $this->songs[$path] = ['s' => max(0.0, $start), 'e' => max(0.0, $end), 'src' => $src]; $this->dirty = true; } public function forget(string $path): void { unset($this->songs[$path]); $this->dirty = true; } public function save(): void { if (!$this->dirty) { return; } // a name that is not UTF-8 kept (b64:); a value JSON cannot hold never empties the file (tried again later) if (\App\Core\WinZik::writeJsonFile($this->file, $this->songs, JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE)) { $this->dirty = false; } }}