isFormatName($name)) { throw new \InvalidArgumentException('Format name invalid!'); } $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)) { throw new \Exception('Format file ' . $filePath . ' does not exist!'); } require_once $pathFormat; if((new \ReflectionClass($name))->isInstantiable()) { return new $name(); } return false; } /** * 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 function isFormatName($name){ return is_string($name) && preg_match('/^[a-zA-Z0-9-]*$/', $name) === 1; } /** * Returns the list of format names from the working directory. * * The list is cached internally to allow for successive calls. * * @return array List of format names */ public function getFormatNames(){ static $formatNames = array(); // Initialized on first call if(empty($formatNames)) { $files = scandir($this->getWorkingDir()); if($files !== false) { foreach($files as $file) { if(preg_match('/^([^.]+)Format\.php$/U', $file, $out)) { $formatNames[] = $out[1]; } } } } return $formatNames; } /** * Returns the sanitized format name. * * The format name can be specified in various ways: * * The PHP file name (i.e. `AtomFormat.php`) * * The PHP file name without file extension (i.e. `AtomFormat`) * * The format name (i.e. `Atom`) * * A format file matching the given format name must exist in the working * directory! * * @param string $name The format name * @return string|null The sanitized format name if the provided name is * valid, null otherwise. */ protected function sanitizeFormatName($name) { $name = ucfirst(strtolower($name)); if(is_string($name)) { // Trim trailing '.php' if exists if(preg_match('/(.+)(?:\.php)/', $name, $matches)) { $name = $matches[1]; } // Trim trailing 'Format' if exists if(preg_match('/(.+)(?:Format)/i', $name, $matches)) { $name = $matches[1]; } // 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]; } Debug::log('Invalid format name: "' . $name . '"!'); } return null; // Bad parameter } }