* * For the full copyright and license information, please view the "LICENSE.md" * file that was distributed with this source code. */ declare(strict_types=1); namespace Respect\Validation\Rules; use finfo; use SplFileInfo; use function is_file; use function is_string; use function mb_strpos; /** * Validates if the file is a valid image by checking its MIME type. * * @author Danilo Benevides * @author Guilherme Siani * @author Henrique Moody */ final class Image extends AbstractRule { /** * @var finfo */ private $fileInfo; /** * Initializes the rule. */ public function __construct(finfo $fileInfo = null) { $this->fileInfo = $fileInfo ?: new finfo(FILEINFO_MIME_TYPE); } /** * {@inheritdoc} */ public function validate($input): bool { if ($input instanceof SplFileInfo) { return $this->validate($input->getPathname()); } if (!is_string($input)) { return false; } if (!is_file($input)) { return false; } return 0 === mb_strpos($this->fileInfo->file($input), 'image/'); } }