class FileIsImageConstraintValidator

Same name in other branches
  1. 10 core/modules/file/src/Plugin/Validation/Constraint/FileIsImageConstraintValidator.php \Drupal\file\Plugin\Validation\Constraint\FileIsImageConstraintValidator

Validator for the FileIsImageConstraint.

Hierarchy

Expanded class hierarchy of FileIsImageConstraintValidator

File

core/modules/file/src/Plugin/Validation/Constraint/FileIsImageConstraintValidator.php, line 16

Namespace

Drupal\file\Plugin\Validation\Constraint
View source
class FileIsImageConstraintValidator extends BaseFileConstraintValidator implements ContainerInjectionInterface {
    
    /**
     * Creates a new FileIsImageConstraintValidator.
     *
     * @param \Drupal\Core\Image\ImageFactory $imageFactory
     *   The image factory.
     */
    public function __construct(ImageFactory $imageFactory) {
    }
    
    /**
     * {@inheritdoc}
     */
    public static function create(ContainerInterface $container) {
        return new static($container->get('image.factory'));
    }
    
    /**
     * {@inheritdoc}
     */
    public function validate(mixed $value, Constraint $constraint) : void {
        $file = $this->assertValueIsFile($value);
        if (!$constraint instanceof FileIsImageConstraint) {
            throw new UnexpectedTypeException($constraint, FileIsImageConstraint::class);
        }
        $image = $this->imageFactory
            ->get($file->getFileUri());
        if (!$image->isValid()) {
            $supportedExtensions = $this->imageFactory
                ->getSupportedExtensions();
            $this->context
                ->addViolation($constraint->message, [
                '%types' => implode(', ', $supportedExtensions),
            ]);
        }
    }

}

Members

Title Sort descending Modifiers Object type Summary Overriden Title
BaseFileConstraintValidator::assertValueIsFile protected function Checks the value is of type FileInterface.
FileIsImageConstraintValidator::create public static function Instantiates a new instance of this class. Overrides ContainerInjectionInterface::create
FileIsImageConstraintValidator::validate public function
FileIsImageConstraintValidator::__construct public function Creates a new FileIsImageConstraintValidator.

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.