class FileEncodingConstraintValidator
Validates the file encoding constraint.
Hierarchy
- class \Drupal\file\Plugin\Validation\Constraint\BaseFileConstraintValidator extends \Symfony\Component\Validator\ConstraintValidator
- class \Drupal\file\Plugin\Validation\Constraint\FileEncodingConstraintValidator extends \Drupal\file\Plugin\Validation\Constraint\BaseFileConstraintValidator
Expanded class hierarchy of FileEncodingConstraintValidator
File
-
core/
modules/ file/ src/ Plugin/ Validation/ Constraint/ FileEncodingConstraintValidator.php, line 13
Namespace
Drupal\file\Plugin\Validation\ConstraintView source
class FileEncodingConstraintValidator extends BaseFileConstraintValidator {
/**
* {@inheritdoc}
*/
public function validate(mixed $value, Constraint $constraint) : void {
/** @var \Drupal\file\Entity\FileInterface $file */
$file = $this->assertValueIsFile($value);
if (!$constraint instanceof FileEncodingConstraint) {
throw new UnexpectedTypeException($constraint, FileEncodingConstraint::class);
}
$encodings = $constraint->encodings;
$data = file_get_contents($file->getFileUri());
foreach ($encodings as $encoding) {
$this->validateEncoding($data, $encoding, $constraint);
}
}
/**
* Validates the encoding of the file.
*
* @param string $data
* The file data.
* @param string $encoding
* The encoding to validate.
* @param \Drupal\file\Plugin\Validation\Constraint\FileEncodingConstraint $constraint
* The constraint.
*/
protected function validateEncoding(string $data, string $encoding, FileEncodingConstraint $constraint) : void {
if (mb_check_encoding($data, $encoding)) {
return;
}
$this->context
->addViolation($constraint->message, [
'%encoding' => $encoding,
'%detected' => mb_detect_encoding($data),
]);
}
}
Members
Title Sort descending | Modifiers | Object type | Summary |
---|---|---|---|
BaseFileConstraintValidator::assertValueIsFile | protected | function | Checks the value is of type FileInterface. |
FileEncodingConstraintValidator::validate | public | function | |
FileEncodingConstraintValidator::validateEncoding | protected | function | Validates the encoding of the file. |
Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.