FileValidator : désormais pattern fichier et pas juste extension

+ Correction suggestions fichiers
master 3.17.0
Julien Rosset 3 weeks ago
parent 6dd0d020c1
commit eae1767640

@ -10,19 +10,35 @@ use SplFileInfo;
*/ */
class FileValidator extends DirectoryValidator { 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 * Initialization
* *
* @param string|null $extensionsPattern The allowed extensions (regex pattern) * @param string|null $filenamePattern The filename pattern (regex)
* @param FilesystemValidationOptionsList|FilesystemValidationOption[]|FilesystemValidationOption|null $options The options * <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); 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) { if (($default = parent::getValidDefault($default)) === null) {
return 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'); throw new InvalidArgumentException('The default value has not a valid extension');
} }
return $default; return $default;
@ -44,29 +60,32 @@ class FileValidator extends DirectoryValidator {
if (!parent::validate($value)) { if (!parent::validate($value)) {
return false; 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 false;
} }
return true; 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 { public function getFilenamePattern (): ?string {
return $this->extensionsPattern; 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 * @return $this
*/ */
public function setExtensionsPattern (?string $extensionsPattern = null): static { public function setFilenamePattern (?string $filenamePattern = null): static {
$this->extensionsPattern = $extensionsPattern; $this->filenamePattern = $filenamePattern;
return $this; return $this;
} }
@ -74,6 +93,9 @@ class FileValidator extends DirectoryValidator {
* @inheritDoc * @inheritDoc
*/ */
protected function checkSuggestedFile (SplFileInfo $file): bool { 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;
} }
} }
Loading…
Cancel
Save