Skip to content

Commit

Permalink
[FormatFactory] Allow lowercase format value
Browse files Browse the repository at this point in the history
  • Loading branch information
VerifiedJoseph committed Feb 2, 2021
1 parent eab575d commit 71474fc
Showing 1 changed file with 10 additions and 11 deletions.
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($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

0 comments on commit 71474fc

Please sign in to comment.