[FormatFactory] Ignore case in format values (#1967)

This commit is contained in:
Joseph 2021-02-09 13:13:03 +00:00 committed by GitHub
parent 771b851b52
commit 9e58735b01
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -46,7 +46,13 @@ class FormatFactory extends FactoryAbstract {
throw new \InvalidArgumentException('Format name invalid!'); throw new \InvalidArgumentException('Format name invalid!');
} }
$name = $this->sanitizeFormatName($name) . 'Format'; $name = $this->sanitizeFormatName($name);
if (is_null($name)) {
throw new \InvalidArgumentException('Unknown format given!');
}
$name .= 'Format';
$pathFormat = $this->getWorkingDir() . $name . '.php'; $pathFormat = $this->getWorkingDir() . $name . '.php';
if(!file_exists($pathFormat)) { if(!file_exists($pathFormat)) {
@ -72,7 +78,7 @@ class FormatFactory extends FactoryAbstract {
* @return bool true if the name is a valid format name, false otherwise. * @return bool true if the name is a valid format name, false otherwise.
*/ */
public function isFormatName($name){ public function isFormatName($name){
return is_string($name) && preg_match('/^[A-Z][a-zA-Z0-9-]*$/', $name) === 1; return is_string($name) && preg_match('/^[a-zA-Z0-9-]*$/', $name) === 1;
} }
/** /**
@ -108,8 +114,6 @@ class FormatFactory extends FactoryAbstract {
* * The PHP file name without file extension (i.e. `AtomFormat`) * * The PHP file name without file extension (i.e. `AtomFormat`)
* * The format name (i.e. `Atom`) * * The format name (i.e. `Atom`)
* *
* Casing is ignored (i.e. `ATOM` and `atom` are the same).
*
* A format file matching the given format name must exist in the working * A format file matching the given format name must exist in the working
* directory! * directory!
* *
@ -118,6 +122,7 @@ class FormatFactory extends FactoryAbstract {
* valid, null otherwise. * valid, null otherwise.
*/ */
protected function sanitizeFormatName($name) { protected function sanitizeFormatName($name) {
$name = ucfirst(strtolower($name));
if(is_string($name)) { if(is_string($name)) {
@ -131,18 +136,12 @@ class FormatFactory extends FactoryAbstract {
$name = $matches[1]; $name = $matches[1];
} }
// Improve performance for correctly written format names // The name is valid if a corresponding format file is found on disk
if(in_array($name, $this->getFormatNames())) { if(in_array($name, $this->getFormatNames())) {
$index = array_search($name, $this->getFormatNames()); $index = array_search($name, $this->getFormatNames());
return $this->getFormatNames()[$index]; return $this->getFormatNames()[$index];
} }
// The name is valid if a corresponding format file is found on disk
if(in_array(strtolower($name), array_map('strtolower', $this->getFormatNames()))) {
$index = array_search(strtolower($name), array_map('strtolower', $this->getFormatNames()));
return $this->getFormatNames()[$index];
}
Debug::log('Invalid format name: "' . $name . '"!'); Debug::log('Invalid format name: "' . $name . '"!');
} }