Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[FormatFactory] Allow lowercase format values #1967

Merged
merged 2 commits into from
Feb 9, 2021
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 10 additions & 11 deletions lib/FormatFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,13 @@ public function create($name){
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';

if(!file_exists($pathFormat)) {
Expand All @@ -72,7 +78,7 @@ public function create($name){
* @return bool true if the name is a valid format name, false otherwise.
*/
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;
}

/**
Expand Down Expand Up @@ -108,8 +114,6 @@ public function getFormatNames(){
* * The PHP file name without file extension (i.e. `AtomFormat`)
* * 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
* directory!
*
Expand All @@ -118,6 +122,7 @@ public function getFormatNames(){
* valid, null otherwise.
*/
protected function sanitizeFormatName($name) {
$name = ucfirst(strtolower($name));

if(is_string($name)) {

Expand All @@ -131,18 +136,12 @@ protected function sanitizeFormatName($name) {
$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())) {
$index = array_search($name, $this->getFormatNames());
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 . '"!');

}
Expand Down