Completed
Push — master ( 8c67dd...c3b5d2 )
by Mikael
02:13
created

functions.php ➔ glob_recursive()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 0
Metric Value
cc 2
nc 2
nop 2
dl 0
loc 8
ccs 0
cts 4
cp 0
crap 6
rs 10
c 0
b 0
f 0
1
<?php
2
/**
3
 * Global functions used in various modules.
4
 */
5
6
7
8
/**
9
 * i18n, internationalization, send all strings though this function to enable i18n. Inspired by Drupal´s t()-function.
10
 *
11
 * @param string $str  the string to check up for translation.
12
 * @param array  $args associative array with arguments to be
13
 *                     replaced in the str.
14
 *                      - !variable: Inserted as is. Use this for text
15
 *                        that has already been sanitized.
16
 *                      - @variable: Escaped to HTML using htmlEnt(). Use
17
 *                        this for anything displayed on a page on the site.
18
 *
19
 * @return string the translated string.
20
 */
21
function t($str, $args = [])
22
{
23
    /*
24
    if (CLydia::Instance()->config["i18n"]) {
25
        $str = gettext($str);
26
    }
27
    */
28
    
29
    // santitize and replace arguments
30
    if (!empty($args)) {
31
        foreach ($args as $key => $val) {
32
            switch ($key[0]) {
33
                case "@":
34
                    $args[$key] = htmlentities($val);
35
                    break;
36
37
                case "!":
38
                default: /* pass through */
39
                    break;
40
            }
41
        }
42
        return strtr($str, $args);
43
    }
44
    return $str;
45
}
46
47
48
49
/**
50
 * Sort array but maintain index when compared items are equal.
51
 * http://www.php.net/manual/en/function.usort.php#38827
52
 *
53
 * @param array    &$array       input array
54
 * @param callable $cmpFunction custom function to compare values
55
 *
56
 * @return void
57
 *
58
 */
59
function mergesort(&$array, $cmpFunction)
60
{
61
    // Arrays of size < 2 require no action.
62
    if (count($array) < 2) {
63
        return;
64
    }
65
    // Split the array in half
66
    $halfway = count($array) / 2;
67
    $array1 = array_slice($array, 0, $halfway);
68
    $array2 = array_slice($array, $halfway);
69
    // Recurse to sort the two halves
70
    mergesort($array1, $cmpFunction);
71
    mergesort($array2, $cmpFunction);
72
    // If all of $array1 is <= all of $array2, just append them.
73
    if (call_user_func($cmpFunction, end($array1), $array2[0]) < 1) {
74
        $array = array_merge($array1, $array2);
75
        return;
76
    }
77
    // Merge the two sorted arrays into a single sorted array
78
    $array = array();
79
    $ptr1 = $ptr2 = 0;
80
    while ($ptr1 < count($array1) && $ptr2 < count($array2)) {
81
        if (call_user_func($cmpFunction, $array1[$ptr1], $array2[$ptr2]) < 1) {
82
            $array[] = $array1[$ptr1++];
83
        } else {
84
            $array[] = $array2[$ptr2++];
85
        }
86
    }
87
    // Merge the remainder
88
    while ($ptr1 < count($array1)) {
89
        $array[] = $array1[$ptr1++];
90
    }
91
    while ($ptr2 < count($array2)) {
92
        $array[] = $array2[$ptr2++];
93
    }
94
    return;
95
}
96
97
98
99
/**
100
 * Glob recursivly.
101
 * http://in.php.net/manual/en/function.glob.php#106595
102
 *
103
 * @param string $pattern  pattern to search for
104
 * @param int    $flags    flags to use, as in glob()
105
 *
106
 * @return void
107
 *
108
 */
109
function glob_recursive($pattern, $flags = 0)
110
{
111
    $files = glob($pattern, $flags);
112
    foreach (glob(dirname($pattern) . "/*", GLOB_ONLYDIR|GLOB_NOSORT) as $dir) {
113
        $files = array_merge($files, glob_recursive($dir .  "/" . basename($pattern), $flags));
114
    }
115
    return $files;
116
}
117
118
119
120
/**
121
* array_merge_recursive does indeed merge arrays, but it converts values
122
* with duplicate keys to arrays rather than overwriting the value in the
123
* first array with the duplicate value in the second array, as array_merge
124
* does. I.e., with array_merge_recursive, this happens (documented behavior):
125
*
126
* array_merge_recursive(['key' => 'org value'], ['key' => 'new value']);
127
*     => ['key' => ['org value', 'new value']];
128
*
129
* array_merge_recursive_distinct does not change the datatypes of the values
130
* in the arrays. Matching keys' values in the second array overwrite those
131
* in the first array, as is the case with array_merge, i.e.:
132
*
133
* array_merge_recursive_distinct(['key' => 'org value'], ['key' => 'new value']);
134
*     => ['key' => ['new value']];
135
*
136
* Parameters are passed by reference, though only for performance reasons.
137
* They're not altered by this function.
138
*
139
* @param array $array1
140
* @param array $array2
141
* @return array
142
* @author Daniel <daniel (at) danielsmedegaardbuus (dot) dk>
143
* @author Gabriel Sobrinho <gabriel (dot) sobrinho (at) gmail (dot) com>
144
*/
145
function array_merge_recursive_distinct(array &$array1, array &$array2)
146
{
147
    $merged = $array1;
148
149
    foreach ($array2 as $key => &$value) {
150
        if (is_array($value) && isset($merged [$key]) && is_array($merged [$key])) {
151
            $merged [$key] = array_merge_recursive_distinct($merged [$key], $value);
152
        } else {
153
            $merged [$key] = $value;
154
        }
155
    }
156
157
    return $merged;
158
}
159