Example #1
0
 public function testBuffer()
 {
     $this->assertSame('text/plain', (new Finfo(FILEINFO_MIME_TYPE))->buffer(self::CONTENTS));
     $this->assertSame('text/plain', (new Finfo())->buffer(self::CONTENTS, FILEINFO_MIME_TYPE));
     $finfo = new Finfo();
     $finfo->set_flags(FILEINFO_MIME_TYPE);
     $this->assertSame('text/plain', $finfo->buffer(self::CONTENTS));
 }
Example #2
0
 /**
  * Gets mime type of a content
  *
  * @param $content
  * @return string
  */
 public static function getContentMime($content)
 {
     $finfo = new Finfo(FILEINFO_MIME_TYPE);
     return $finfo->buffer($content);
 }
 public function parse(\SplFileInfo $file, string $filename = null, string $title = null) : Dictionary
 {
     $this->filenames = [];
     if ($file instanceof \SplTempFileObject) {
         $binary = (new \esperecyan\dictionary_php\Parser())->getBinary($file);
     }
     if (!$file instanceof \SplFileObject) {
         $file = $file->openFile();
     } else {
         $file->rewind();
     }
     $finfo = new Finfo(FILEINFO_MIME_TYPE);
     $type = isset($binary) ? $finfo->buffer($binary) : $finfo->file($file->getRealPath());
     switch ($type) {
         case 'application/zip':
             $this->header = true;
             $quizFile = $this->parseArchive($file);
             $binary = file_get_contents($quizFile);
             if (!mb_check_encoding($binary, 'Windows-31J')) {
                 throw new SyntaxException(sprintf(_('%sの辞書の符号化方式 (文字コード) は Shift_JIS でなければなりません。'), 'Inteligenceω'));
             }
             $txt = new \SplTempFileObject();
             $txt->fwrite(mb_convert_encoding($binary, 'UTF-8', 'Windows-31J'));
             $files = new \FilesystemIterator($quizFile->getPath(), \FilesystemIterator::SKIP_DOTS);
             unlink($quizFile);
             break;
         case 'text/csv':
         case 'text/plain':
             $txt = $file;
             break;
         default:
             throw new SyntaxException(_('Inteligenceωの辞書はテキストファイルかZIPファイルでなければなりません。'));
     }
     $filesCount = count($this->filenames) + 1;
     if ($filesCount > GenericDictionaryParser::MAX_FILES) {
         throw new SyntaxException(sprintf(_('アーカイブ中のファイル数は %1$s 個以下にしてください: 現在 %2$s 個'), GenericDictionaryParser::MAX_FILES, $filesCount));
     }
     $dictionary = new Dictionary($files ?? [], $this->filenames);
     $txt->setFlags(\SplFileObject::DROP_NEW_LINE | \SplFileObject::READ_AHEAD | \SplFileObject::SKIP_EMPTY);
     foreach ($txt as $line) {
         $this->parseLine($dictionary, $line);
     }
     $this->parseLine($dictionary);
     if (!$dictionary->getWords()) {
         throw new SyntaxException(_('正常に変換可能な行が見つかりませんでした。'));
     }
     if ($this->wholeText !== '') {
         $regard = $this->generateRegard();
         if ($regard) {
             $metaFields['@regard'] = $this->generateRegard();
         }
     }
     if (!is_null($title)) {
         $metaFields['@title'] = $title;
     } elseif (!is_null($filename)) {
         $titleFromFilename = $this->getTitleFromFilename($filename);
         if ($titleFromFilename) {
             $metaFields['@title'] = $titleFromFilename;
         }
     }
     if (isset($metaFields)) {
         $dictionary->setMetadata($metaFields);
     }
     return $dictionary;
 }