/** * @param string $type * @param \Closure $generateImage * @param \Closure $checkOutput * @param array $logLevels * @dataProvider imageProvider */ public function testCorrect(string $type, \Closure $generateImage, \Closure $checkOutput, array $logLevels = []) { $validator = new ImageValidator($type, ''); $validator->setLogger($this); $checkOutput->call($this, $validator->correct($generateImage())); $this->assertEquals($logLevels, $this->logLevels); }
/** * @param string|\SplFileInfo $file * @param string $filename * @return string[] */ public function correct($file, string $filename) : array { if (!(new validator\FilenameValidator())->validate($filename)) { throw new SyntaxException(sprintf(_('「%s」は妥当なファイル名ではありません。'), $filename)); } $type = is_string($file) ? $this->finfo->buffer($file) : $this->finfo->file($file->getRealPath()); if (empty(self::VALID_EXTENSIONS[$type])) { throw new SyntaxException(sprintf(_('「%s」は妥当な画像、音声、動画ファイルではありません。'), $filename)); } $extension = (new \SplFileInfo($filename))->getExtension(); if ($type === 'video/mp4' && $extension === 'm4a') { $type = 'audio/mp4'; } elseif (!in_array($extension, $type === 'video/mp4' ? array_merge(self::VALID_EXTENSIONS['audio/mp4'], self::VALID_EXTENSIONS['video/mp4']) : self::VALID_EXTENSIONS[$type])) { throw new SyntaxException(sprintf(_('「%s」の拡張子は次のいずれかにしなければなりません:'), $filename) . ' ' . implode(', ', self::VALID_EXTENSIONS[$type])); } $topLevelType = explode('/', $type)[0]; $this->checkFileSize(is_string($file) ? mb_strlen($file, 'ASCII') : $file->getSize(), $topLevelType, $filename); if ($topLevelType === 'image') { $validator = new validator\ImageValidator($type, $filename); $validator->setLogger($this->logger); $file = $validator->correct(is_string($file) ? $file : (new Parser())->getBinary($file)); } return ['bytes' => $file, 'type' => $type === 'image/svg+xml' ? 'image/svg+xml; charset=UTF-8' : $type, 'name' => $filename]; }