|
|
|
|
@ -10,19 +10,35 @@ use SplFileInfo;
|
|
|
|
|
*/
|
|
|
|
|
class FileValidator extends DirectoryValidator {
|
|
|
|
|
/**
|
|
|
|
|
* @var string|null The allowed extensions (regex pattern)
|
|
|
|
|
* @var string|null The filename pattern (regex)
|
|
|
|
|
* <br>Null if no restriction
|
|
|
|
|
*/
|
|
|
|
|
private ?string $extensionsPattern;
|
|
|
|
|
private ?string $filenamePattern;
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Initialization
|
|
|
|
|
*
|
|
|
|
|
* @param string|null $extensionsPattern The allowed extensions (regex pattern)
|
|
|
|
|
* @param string|null $filenamePattern The filename pattern (regex)
|
|
|
|
|
* <br>Null if no restriction
|
|
|
|
|
* @param FilesystemValidationOptionsList|FilesystemValidationOption[]|FilesystemValidationOption|null $options The options
|
|
|
|
|
*/
|
|
|
|
|
public function __construct (?string $extensionsPattern = null, FilesystemValidationOptionsList|FilesystemValidationOption|array|null $options = null) {
|
|
|
|
|
public function __construct (?string $filenamePattern = null, FilesystemValidationOptionsList|FilesystemValidationOption|array|null $options = null) {
|
|
|
|
|
parent::__construct($options);
|
|
|
|
|
$this->setExtensionsPattern($extensionsPattern);
|
|
|
|
|
$this->setFilenamePattern($filenamePattern);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Créer un pattern de fichier (regex) pour vérifier l'extension du fichier
|
|
|
|
|
*
|
|
|
|
|
* La comparaison est insensible à la casse
|
|
|
|
|
*
|
|
|
|
|
* @param string $extension L'extension attendue
|
|
|
|
|
* @param string ...$extraExtensions Des extensions complémentaires autorisées
|
|
|
|
|
*
|
|
|
|
|
* @return string|null Le pattern de regex correspondant
|
|
|
|
|
*/
|
|
|
|
|
public static function createExtensionPattern (string $extension, string ...$extraExtensions): ?string {
|
|
|
|
|
return /** @lang PhpRegExp */ '/\.(' . implode(preg_quote('|', '/'), array_merge([$extension], $extraExtensions)) . ')$/i';
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
@ -32,7 +48,7 @@ class FileValidator extends DirectoryValidator {
|
|
|
|
|
if (($default = parent::getValidDefault($default)) === null) {
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
if ($this->extensionsPattern !== null && preg_match($this->extensionsPattern, $default) !== 1) {
|
|
|
|
|
if ($this->filenamePattern !== null && preg_match($this->filenamePattern, $default) !== 1) {
|
|
|
|
|
throw new InvalidArgumentException('The default value has not a valid extension');
|
|
|
|
|
}
|
|
|
|
|
return $default;
|
|
|
|
|
@ -44,29 +60,32 @@ class FileValidator extends DirectoryValidator {
|
|
|
|
|
if (!parent::validate($value)) {
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
if ($this->extensionsPattern !== null && preg_match($this->extensionsPattern, $value) !== 1) {
|
|
|
|
|
if ($this->filenamePattern !== null && preg_match($this->filenamePattern, $value) !== 1) {
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* The allowed extensions (regex pattern)
|
|
|
|
|
* The filename pattern (regex)
|
|
|
|
|
*
|
|
|
|
|
* Null if no restriction
|
|
|
|
|
*
|
|
|
|
|
* @return string|null The allowed extensions (regex pattern)
|
|
|
|
|
* @return string|null The filename pattern (regex)
|
|
|
|
|
*/
|
|
|
|
|
public function getExtensionsPattern (): ?string {
|
|
|
|
|
return $this->extensionsPattern;
|
|
|
|
|
public function getFilenamePattern (): ?string {
|
|
|
|
|
return $this->filenamePattern;
|
|
|
|
|
}
|
|
|
|
|
/**
|
|
|
|
|
* Replace the allowed extensions
|
|
|
|
|
* Replace filename pattern (regex)
|
|
|
|
|
*
|
|
|
|
|
* @param string|null $extensionsPattern The allowed extensions (regex pattern)
|
|
|
|
|
* @param string|null $filenamePattern The filename pattern (regex)
|
|
|
|
|
* <br>Null if no restriction
|
|
|
|
|
*
|
|
|
|
|
* @return $this
|
|
|
|
|
*/
|
|
|
|
|
public function setExtensionsPattern (?string $extensionsPattern = null): static {
|
|
|
|
|
$this->extensionsPattern = $extensionsPattern;
|
|
|
|
|
public function setFilenamePattern (?string $filenamePattern = null): static {
|
|
|
|
|
$this->filenamePattern = $filenamePattern;
|
|
|
|
|
return $this;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@ -74,6 +93,9 @@ class FileValidator extends DirectoryValidator {
|
|
|
|
|
* @inheritDoc
|
|
|
|
|
*/
|
|
|
|
|
protected function checkSuggestedFile (SplFileInfo $file): bool {
|
|
|
|
|
return true;
|
|
|
|
|
if ($file->isDir()) {
|
|
|
|
|
return true; // Pour permettre l'auto-complétion d'un nom de répertoire devant le fichier
|
|
|
|
|
}
|
|
|
|
|
return $this->filenamePattern === null || preg_match($this->filenamePattern, $file->getFilename()) === 1;
|
|
|
|
|
}
|
|
|
|
|
}
|