88 lines
2.3 KiB
PHP
88 lines
2.3 KiB
PHP
<?php
|
|
|
|
namespace App\Entity;
|
|
|
|
class Note
|
|
{
|
|
const HIGHLIGHT_PATTERN = '/<span\s+([^>]*)>(.*?)<\/span>/i';
|
|
const HIGHLIGHT_ATTR_KANJI = 'style="color: rgb(255, 78, 8);"';
|
|
|
|
private readonly int $id;
|
|
private readonly int $mod;
|
|
private readonly string $model;
|
|
private string $profile;
|
|
private array $cardIds = [];
|
|
protected array $fields = [];
|
|
private array $tags = [];
|
|
|
|
|
|
// -------------------------------------------------- Getters & setters ---
|
|
|
|
public function getId(): int
|
|
{
|
|
return $this->id;
|
|
}
|
|
public function getModel(): string
|
|
{
|
|
return $this->model;
|
|
}
|
|
|
|
public function getFields(): array
|
|
{
|
|
return $this->fields;
|
|
}
|
|
public function setFields(array $fields): static
|
|
{
|
|
$this->fields = $fields;
|
|
return $this;
|
|
}
|
|
|
|
|
|
// ------------------------------------------------------- Anki-related ---
|
|
|
|
public static function fromAnki(array $noteInfo): static
|
|
{
|
|
$note = new static();
|
|
|
|
[
|
|
'noteId' => $note->id,
|
|
'mod' => $note->mod,
|
|
'profile' => $note->profile,
|
|
'tags' => $note->tags,
|
|
'modelName' => $note->model,
|
|
'cards' => $note->cardIds,
|
|
] = $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']);
|
|
|
|
return $note;
|
|
}
|
|
|
|
public function toAnki(): array
|
|
{
|
|
return [
|
|
'id' => $this->id,
|
|
];
|
|
}
|
|
|
|
|
|
// ---------------------------------------------------- Derived methods ---
|
|
|
|
public function getCreatedAt(): \DateTimeImmutable
|
|
{
|
|
$timestamp = ceil($this->id / 1000);
|
|
return \DateTimeImmutable::createFromFormat('U', $timestamp);
|
|
}
|
|
|
|
public function getUpdatedAt(): \DateTimeImmutable
|
|
{
|
|
return \DateTimeImmutable::createFromFormat('U', $this->mod);
|
|
}
|
|
}
|