public static function directoryToArray(
        string $directory,
        bool $recursive = false,
        bool $listDirs = false,
        bool $listFiles = true,
        string $exclude = '',
        array $allowedFileTypes = [],
        bool $relativePath = false
    ) : array {
        $arrayItems = [];
        $skipByExclude = false;
        $handle = opendir($directory);
        if ($handle) {
            while (false !== ($file = readdir($handle))) {
                preg_match(
                    '/(^(([\.]) {1,2})$|(\.(svn|git|md|htaccess))|(Thumbs\.db|\.DS_STORE|\.|\.\.))$/iu',
                    $file,
                    $skip
                );
                if ($exclude) {
                    preg_match($exclude, $file, $skipByExclude);
                }
                if ($allowedFileTypes && !is_dir($directory . DIRECTORY_SEPARATOR . $file)) {
                    $ext = strtolower(preg_replace('/^.*\.([^.]+)$/D', '$1', $file));
                    if (!in_array($ext, $allowedFileTypes)) {
                        $skip = true;
                    }
                }
                if (!$skip && !$skipByExclude) {
                    if (is_dir($directory . DIRECTORY_SEPARATOR . $file)) {
                        if ($recursive) {
                            $arrayItems = array_merge(
                                $arrayItems,
                                self::directoryToArray(
                                    $directory . DIRECTORY_SEPARATOR . $file,
                                    $recursive,
                                    $listDirs,
                                    $listFiles,
                                    $exclude,
                                    $allowedFileTypes,
                                    $relativePath
                                )
                            );
                        }
                        if ($listDirs) {
                            $arrayItems[] = (
                            $relativePath
                                ? $file
                                : $directory . DIRECTORY_SEPARATOR . $file
                            );
                        }
                    } else {
                        if ($listFiles) {
                            $arrayItems[] = (
                            $relativePath
                                ? $file
                                : $directory . DIRECTORY_SEPARATOR . $file
                            );
                        }
                    }
                }
            }
            closedir($handle);
        }
        return $arrayItems;
    }