respect-validation/library/Rules/Image.php
Henrique Moody 7f66bcea10
Bump PHP support from 8.1 to 8.5
We want to release version 3.0 as fresh as possible, without having to
maintain backward compatibility with the previous versions. Because that
version will be on for some time, we decided it will be best to support
only PHP version 8.5 or higher.

Acked-by: Alexandre Gomes Gaigalas <alganet@gmail.com>
2025-12-18 19:03:38 +01:00

52 lines
1.1 KiB
PHP

<?php
/*
* Copyright (c) Alexandre Gomes Gaigalas <alganet@gmail.com>
* SPDX-License-Identifier: MIT
*/
declare(strict_types=1);
namespace Respect\Validation\Rules;
use Attribute;
use finfo;
use Respect\Validation\Message\Template;
use Respect\Validation\Rules\Core\Simple;
use SplFileInfo;
use function is_file;
use function is_string;
use function mb_strpos;
use const FILEINFO_MIME_TYPE;
#[Attribute(Attribute::TARGET_PROPERTY | Attribute::IS_REPEATABLE)]
#[Template(
'{{name}} must be a valid image file',
'{{name}} must not be a valid image file',
)]
final class Image extends Simple
{
public function __construct(
private finfo $fileInfo = new finfo(FILEINFO_MIME_TYPE)
) {
}
public function isValid(mixed $input): bool
{
if ($input instanceof SplFileInfo) {
return $this->isValid($input->getPathname());
}
if (!is_string($input)) {
return false;
}
if (!is_file($input)) {
return false;
}
return mb_strpos((string) $this->fileInfo->file($input), 'image/') === 0;
}
}