/** * @param Dictionary $dictionary * @throws \BadMethodCallException $this->textFileOnly が偽、かつ「画像・音声・動画ファイルを含む場合のファイル形式」をCSVファイルのみで構文解析していた場合。 * @throws EmptyOutputException 該当の辞書形式に変換可能なお題が一つも存在しなかった。 * @return string[] */ public function serialize(Dictionary $dictionary) : array { $directoryName = (new \esperecyan\dictionary_php\validator\FilenameValidator())->convertToValidFilenameWithoutExtensionInArchives($dictionary->getTitle()); foreach ($dictionary->getWords() as $word) { $serialized = $this->type === 'Inteligenceω しりとり' ? $this->serializeWordAsShiritori($word) : $this->serializeWordAsQuiz($word, $directoryName); if ($serialized !== '') { $words[] = $serialized; } } if (empty($words)) { throw new EmptyOutputException(sprintf(_('%sの辞書形式に変換可能なお題が見つかりませんでした。'), $this->type)); } $previousSubstituteCharacter = mb_substitute_character(); mb_substitute_character(\IntlChar::ord(self::SUBSTITUTE_CHARACTER)); $bytes = mb_convert_encoding($this->serializeMetadata($dictionary, '%') . implode('', $words), 'Windows-31J', 'UTF-8'); mb_substitute_character($previousSubstituteCharacter); $files = $dictionary->getFiles(); if (!$files && !$this->textFileOnly && $dictionary->getFilenames()) { throw new \BadMethodCallException(); } elseif ($this->type === 'Inteligenceω クイズ' && $files && !$this->textFileOnly) { $archive = $this->generateArchive(); foreach ($files as $file) { $archive->addFile($file, "{$directoryName}/" . $file->getFilename()); } $archive->addFromString("{$directoryName}.txt", $bytes); $archivePath = $archive->filename; $archive->close(); return ['bytes' => file_get_contents($archivePath), 'type' => 'application/zip', 'name' => $this->getFilename($dictionary, 'zip')]; } else { return ['bytes' => $bytes, 'type' => 'text/plain; charset=Shift_JIS', 'name' => $this->getFilename($dictionary, 'txt')]; } }