タグを保存前に正規化する

This commit is contained in:
shibafu
2020-02-22 14:39:25 +09:00
parent cb0af4113d
commit 631d2ea298
4 changed files with 61 additions and 2 deletions

View File

@@ -23,4 +23,30 @@ class Metadata
* チェックインタグと同様に保存されるため、スペースや改行文字を含めてはいけません。
*/
public $tags = [];
/**
* 重複を排除し、正規化を行ったタグの集合を返します。
* @return string[]
*/
public function normalizedTags(): array
{
$tags = [];
foreach ($this->tags as $tag) {
$tag = $this->sanitize($tag);
$tag = $this->trim($tag);
$tags[$tag] = true;
}
return array_keys($tags);
}
private function sanitize(string $value): string
{
return preg_replace('/\r?\n/u', ' ', $value);
}
private function trim(string $value): string
{
return trim($value);
}
}