From d7c374bd8cc370f377264d2ec76cb1e09ae69fd2 Mon Sep 17 00:00:00 2001 From: logmanoriginal Date: Thu, 15 Nov 2018 20:08:33 +0100 Subject: [PATCH] [Format] Add function isFormatName Returns true if the provided format name is valid --- lib/Format.php | 15 ++++++++++++++- 1 file changed, 14 insertions(+), 1 deletion(-) diff --git a/lib/Format.php b/lib/Format.php index a6c97c6d..212bed2f 100644 --- a/lib/Format.php +++ b/lib/Format.php @@ -64,7 +64,7 @@ class Format { * @return object|bool The format object or false if the class is not instantiable. */ public static function create($name){ - if(!preg_match('@^[A-Z][a-zA-Z]*$@', $name)) { + if(!self::isFormatName($name)) { throw new \InvalidArgumentException('Format name invalid!'); } @@ -126,6 +126,19 @@ class Format { return self::$workingDir; } + /** + * Returns true if the provided name is a valid format name. + * + * A valid format name starts with a capital letter ([A-Z]), followed by + * zero or more alphanumeric characters or hyphen ([A-Za-z0-9-]). + * + * @param string $name The format name. + * @return bool true if the name is a valid format name, false otherwise. + */ + public static function isFormatName($name){ + return is_string($name) && preg_match('/^[A-Z][a-zA-Z0-9-]*$/', $name) === 1; + } + /** * Read format dir and catch informations about each format depending annotation * @return array Informations about each format