feat: Add media info to Note Class

This commit is contained in:
Dendy 2025-02-06 17:58:32 +09:00
parent b99d7ae971
commit 576816fa03
1 changed files with 52 additions and 0 deletions

View File

@ -21,6 +21,7 @@ class Note
// Maybe these doesn't make sense to keep but leaving it here just in // Maybe these doesn't make sense to keep but leaving it here just in
// case for handiness' sake // case for handiness' sake
private array $fields = []; private array $fields = [];
private ?array $mediaInfo = null;
private array $cardIds; private array $cardIds;
const HIGHLIGHT_PATTERN = '/<span\s+([^>]*)>(.*?)<\/span>/i'; const HIGHLIGHT_PATTERN = '/<span\s+([^>]*)>(.*?)<\/span>/i';
@ -55,8 +56,16 @@ class Note
'cards' => $note->cardIds, 'cards' => $note->cardIds,
] = $noteInfo; ] = $noteInfo;
// the fields array key value comes with an order fields that is
// already maintained by PHP since arrays are ordered dictionaries.
// So we can safely just drop it.
//
// REVIEW: Having said that, maybe ordering the array before throwing
// the order would be advisable.
$note->fields = array_map(fn($x) => $x['value'], $noteInfo['fields']); $note->fields = array_map(fn($x) => $x['value'], $noteInfo['fields']);
$note->mediaInfo = $note->parseMediaInfo($note->fields['Notes']);
// Set VocabKanji field // Set VocabKanji field
//$vocabKanji = explode('', $note->fields['VocabKanji']); //$vocabKanji = explode('', $note->fields['VocabKanji']);
$note->terms = self::parseVocabDef($note->fields['VocabDef']); $note->terms = self::parseVocabDef($note->fields['VocabDef']);
@ -136,6 +145,49 @@ class Note
return $terms; return $terms;
} }
public function parseMediaInfo(string $notes): ?array
{
$matches = null;
// Parse the notes fields. It can be in the form of
// series-name_S01 EP07 (11h22m33s44ms)
// or
// movie-name EP (11h22m33s44ms)
if (1 !== preg_match(
'/(?<name>[a-z\-_]+)(_S)?(?<season>\d*) EP(?<episode>\d*) \((?<time>.*)\)/n',
$notes,
$matches,
)) {
return null;
}
// Remove number-indexed matches, cast numbers to integers
$matches = [
'name' => $matches['name'],
'time' => $matches['time'],
// NOTE: intval returns 0 if not a number, which is false-like
'season' => intval($matches['season']) ?: null,
'episode' => intval($matches['episode']) ?: null,
];
// Parse time into a DateInterval and replace it in the matches array
$time = new \DateInterval('PT0S');
preg_match('/(\d+)ms/', $matches['time'], $milliseconds);
preg_match('/(\d+)s/', $matches['time'], $seconds);
preg_match('/(\d+)m/', $matches['time'], $minutes);
preg_match('/(\d+)h/', $matches['time'], $hours);
if ($milliseconds[1] ?? false) $time->f = $milliseconds[1] * 1000;
if ($seconds[1] ?? false) $time->s = $seconds[1];
if ($minutes[1] ?? false) $time->i = $minutes[1];
if ($hours[1] ?? false) $time->h = $hours[1];
$matches['time'] = $time;
return $matches;
}
public function getCreatedAt(): \DateTimeImmutable public function getCreatedAt(): \DateTimeImmutable
{ {
$timestamp = ceil($this->id / 1000); $timestamp = ceil($this->id / 1000);