Completed
Push — master ( efc964...49d6db )
by Mikael
02:34
created

functions.php ➔ t()   A

Complexity

Conditions 5
Paths 5

Size

Total Lines 25

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 8
CRAP Score 5.2

Importance

Changes 0
Metric Value
cc 5
nc 5
nop 2
dl 0
loc 25
ccs 8
cts 10
cp 0.8
crap 5.2
rs 9.2088
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 3
    if (!empty($args)) {
31 2
        foreach ($args as $key => $val) {
32 2
            switch ($key[0]) {
33
                case "@":
34 1
                    $args[$key] = htmlentities($val);
35 1
                    break;
36
37
                case "!":
38
                default: /* pass through */
39 2
                    break;
40
            }
41
        }
42 2
        return strtr($str, $args);
43
    }
44 1
    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
 * @codeCoverageIgnore
59
 */
60
function mergesort(&$array, $cmpFunction)
61
{
62
    // Arrays of size < 2 require no action.
63
    if (count($array) < 2) {
64
        return;
65
    }
66
    // Split the array in half
67
    $halfway = count($array) / 2;
68
    $array1 = array_slice($array, 0, $halfway);
69
    $array2 = array_slice($array, $halfway);
70
    // Recurse to sort the two halves
71
    mergesort($array1, $cmpFunction);
72
    mergesort($array2, $cmpFunction);
73
    // If all of $array1 is <= all of $array2, just append them.
74
    if (call_user_func($cmpFunction, end($array1), $array2[0]) < 1) {
75
        $array = array_merge($array1, $array2);
76
        return;
77
    }
78
    // Merge the two sorted arrays into a single sorted array
79
    $array = array();
80
    $ptr1 = $ptr2 = 0;
81
    while ($ptr1 < count($array1) && $ptr2 < count($array2)) {
82
        if (call_user_func($cmpFunction, $array1[$ptr1], $array2[$ptr2]) < 1) {
83
            $array[] = $array1[$ptr1++];
84
        } else {
85
            $array[] = $array2[$ptr2++];
86
        }
87
    }
88
    // Merge the remainder
89
    while ($ptr1 < count($array1)) {
90
        $array[] = $array1[$ptr1++];
91
    }
92
    while ($ptr2 < count($array2)) {
93
        $array[] = $array2[$ptr2++];
94
    }
95
    return;
96
}
97
98
99
100
/**
101
 * Glob recursivly.
102
 * http://in.php.net/manual/en/function.glob.php#106595
103
 *
104
 * @param string $pattern  pattern to search for
105
 * @param int    $flags    flags to use, as in glob()
106
 *
107
 * @return void
108
 *
109
 * @codeCoverageIgnore
110
 */
111
function glob_recursive($pattern, $flags = 0)
112
{
113
    $files = glob($pattern, $flags);
114
    foreach (glob(dirname($pattern) . "/*", GLOB_ONLYDIR|GLOB_NOSORT) as $dir) {
115
        $files = array_merge($files, glob_recursive($dir .  "/" . basename($pattern), $flags));
116
    }
117
    return $files;
118
}
119
120
121
122
/**
123
* array_merge_recursive does indeed merge arrays, but it converts values
124
* with duplicate keys to arrays rather than overwriting the value in the
125
* first array with the duplicate value in the second array, as array_merge
126
* does. I.e., with array_merge_recursive, this happens (documented behavior):
127
*
128
* array_merge_recursive(['key' => 'org value'], ['key' => 'new value']);
129
*     => ['key' => ['org value', 'new value']];
130
*
131
* array_merge_recursive_distinct does not change the datatypes of the values
132
* in the arrays. Matching keys' values in the second array overwrite those
133
* in the first array, as is the case with array_merge, i.e.:
134
*
135
* array_merge_recursive_distinct(['key' => 'org value'], ['key' => 'new value']);
136
*     => ['key' => ['new value']];
137
*
138
* Parameters are passed by reference, though only for performance reasons.
139
* They're not altered by this function.
140
*
141
* @param array $array1
142
* @param array $array2
143
* @return array
144
* @author Daniel <daniel (at) danielsmedegaardbuus (dot) dk>
145
* @author Gabriel Sobrinho <gabriel (dot) sobrinho (at) gmail (dot) com>
146
*
147
* @codeCoverageIgnore
148
*/
149
function array_merge_recursive_distinct(array &$array1, array &$array2)
150
{
151
    $merged = $array1;
152
153
    foreach ($array2 as $key => &$value) {
154
        if (is_array($value) && isset($merged [$key]) && is_array($merged [$key])) {
155
            $merged [$key] = array_merge_recursive_distinct($merged [$key], $value);
156
        } else {
157
            $merged [$key] = $value;
158
        }
159
    }
160
161
    return $merged;
162
}
163