class FileIsImageConstraintValidator
Same name in other branches
- 11.x core/modules/file/src/Plugin/Validation/Constraint/FileIsImageConstraintValidator.php \Drupal\file\Plugin\Validation\Constraint\FileIsImageConstraintValidator
Validator for the FileIsImageConstraint.
Hierarchy
- class \Drupal\file\Plugin\Validation\Constraint\BaseFileConstraintValidator extends \Symfony\Component\Validator\ConstraintValidator
- class \Drupal\file\Plugin\Validation\Constraint\FileIsImageConstraintValidator extends \Drupal\file\Plugin\Validation\Constraint\BaseFileConstraintValidator implements \Drupal\Core\DependencyInjection\ContainerInjectionInterface
Expanded class hierarchy of FileIsImageConstraintValidator
File
-
core/
modules/ file/ src/ Plugin/ Validation/ Constraint/ FileIsImageConstraintValidator.php, line 16
Namespace
Drupal\file\Plugin\Validation\ConstraintView 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) {
$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.