Passed
Push — main ( 3a968f...2b582c )
by Rafael
17:15
created

Functions::getFirstNonEmptyDirectory()   A

Complexity

Conditions 6
Paths 5

Size

Total Lines 17
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 11
dl 0
loc 17
rs 9.2222
c 0
b 0
f 0
cc 6
nc 5
nop 2
1
<?php
2
3
/* Copyright (C) 2024       Rafael San José         <[email protected]>
4
 *
5
 * This program is free software; you can redistribute it and/or modify
6
 * it under the terms of the GNU General Public License as published by
7
 * the Free Software Foundation; either version 3 of the License, or
8
 * (at your option) any later version.
9
 *
10
 * This program is distributed in the hope that it will be useful,
11
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13
 * GNU General Public License for more details.
14
 *
15
 * You should have received a copy of the GNU General Public License
16
 * along with this program. If not, see <https://www.gnu.org/licenses/>.
17
 * or see https://www.gnu.org/
18
 */
19
20
namespace Alxarafe\Lib;
21
22
abstract class Functions
23
{
24
    /**
25
     * Obtains the main url
26
     *
27
     * @return string
28
     */
29
    public static function getUrl()
30
    {
31
        $ssl = !empty($_SERVER['HTTPS']) && $_SERVER['HTTPS'] == 'on';
32
        $proto = strtolower($_SERVER['SERVER_PROTOCOL']);
33
        $proto = substr($proto, 0, strpos($proto, '/')) . ($ssl ? 's' : '');
34
        if (isset($_SERVER['HTTP_HOST'])) {
35
            $host = $_SERVER['HTTP_HOST'];
36
        } else {
37
            $port = $_SERVER['SERVER_PORT'];
38
            $port = ((!$ssl && $port == '80') || ($ssl && $port == '443')) ? '' : ':' . $port;
39
            $host = $_SERVER['SERVER_NAME'] . $port;
40
        }
41
42
        $script = $_SERVER['SCRIPT_NAME'];
43
44
        $script = substr($script, 0, strlen($script) - strlen('/index.php'));
45
        return $proto . '://' . $host . $script;
46
    }
47
48
    /**
49
     * This function is used to obtain the value of a POST variable, and if it does not exist
50
     * (for example, the first time the form is loaded), take a default value.
51
     *
52
     * @param $postVar
53
     * @param $defaultValue
54
     * @return mixed
55
     */
56
    public static function getIfIsset($postVar, $defaultValue)
57
    {
58
        $return = filter_input(INPUT_POST, $postVar);
59
        if ($return === null || $return === false) {
60
            return $defaultValue;
61
        }
62
        return $return;
63
    }
64
65
    /**
66
     * Defines the constant $name, if it is not already defined.
67
     *
68
     * @param string $name
69
     * @param        $value
70
     */
71
    public static function defineIfNotDefined(string $name, $value)
72
    {
73
        if (!defined($name)) {
74
            define($name, $value);
75
        }
76
    }
77
78
    /**
79
     * Convert an array of attributes to a string.
80
     *
81
     * @param array $attributes
82
     * @return string
83
     */
84
    public static function htmlAttributes(array $attributes): string
85
    {
86
        $_attributes = '';
87
        foreach ($attributes as $key => $value) {
88
            $_attributes .= $key . '="' . htmlspecialchars($value) . '" ';
89
        }
90
        return trim($_attributes);
91
    }
92
93
    public static function getThemes()
94
    {
95
        $routes = [
96
            '/Templates/theme',
97
            '/vendor/rsanjoseo/alxarafe/Templates/theme',
98
        ];
99
        return Functions::getFirstNonEmptyDirectory($routes, '');
100
    }
101
102
    /**
103
     * Gets the list of files that match a pattern, at the first path in the array
104
     * where the matching files exist.
105
     *
106
     * @param $pathArray
107
     * @param $pattern
108
     * @return string[]
109
     */
110
    public static function getFirstNonEmptyDirectory($pathArray, $pattern): array
111
    {
112
        foreach ($pathArray as $path) {
113
            $realPath = realpath(constant('BASE_PATH') . '/..' . $path);
114
            if ($realPath === false || !is_dir($realPath)) {
115
                continue;
116
            }
117
            $files = glob($realPath . '/*' . $pattern);
118
            if (!empty($files)) {
119
                $result = [];
120
                foreach ($files as $file) {
121
                    $result[$file] = basename($file, $pattern);
122
                }
123
                return $result;
124
            }
125
        }
126
        return [];
127
    }
128
}
129