Completed
Push — auto-escape ( 46bb2f...7a06cf )
by
unknown
38:05 queued 23:10
created

util.php ➔ escape()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 3
nc 1
nop 2
dl 0
loc 5
rs 9.4285
c 0
b 0
f 0
1
<?php
2
3
namespace League\Plates\Util;
4
5
use League\Plates\Exception\PlatesException;
6
use League\Plates\Exception\StackException;
7
8
function id() {
9
    return function($arg) {
10
        return $arg;
11
    };
12
}
13
14
/** simple utility that wraps php echo which allows for stubbing out the
15
    echo func for testing */
16
function phpEcho() {
17
    return function($v) {
18
        echo $v;
19
    };
20
}
21
22
function escape($flags = ENT_COMPAT | ENT_HTML401, $encoding = 'UTF-8') {
23
    return function($v) use ($flags, $encoding) {
24
        return htmlspecialchars($v, $flags, $encoding);
25
    };
26
}
27
28
/** stack a set of functions into each other and returns the stacked func */
29
function stack(array $funcs) {
30
    return array_reduce(array_reverse($funcs), function($next, $func) {
31
        return function(...$args) use ($next, $func) {
32
            $args[] = $next;
33
            return $func(...$args);
34
        };
35
    }, function() {
36
        throw new StackException('No handler was able to return a result.');
37
    });
38
}
39
40
/** takes a structured array and sorts them by priority. This allows for prioritized stacks.
41
    The structure is just an associative array where the indexes are numeric and the values
42
    are array of stack handlers. The array is sorted by key and then all inner arrays are merged
43
    together */
44
function sortStacks($stacks) {
45
    ksort($stacks);
46
    return array_merge(...$stacks);
47
}
48
49
function stackGroup(array $funcs) {
50
    $end_next = null;
51
    $funcs[] = function(...$args) use (&$end_next) {
52
        return $end_next(...array_slice($args, 0, -1));
53
    };
54
    $next = stack($funcs);
55
    return function(...$args) use ($next, &$end_next) {
56
        $end_next = end($args);
57
        return $next(...array_slice($args, 0, -1));
58
    };
59
}
60
61
function compose(...$funcs) {
62
    return function($arg) use ($funcs) {
63
        return array_reduce($funcs, function($acc, $func) {
64
            return $func($acc);
65
        }, $arg);
66
    };
67
}
68
69
function joinPath(array $parts, $sep = DIRECTORY_SEPARATOR) {
70
    return array_reduce(array_filter($parts), function($acc, $part) use ($sep) {
71
        if ($acc === null) {
72
            return rtrim($part, $sep);
73
        }
74
75
        return $acc . $sep . ltrim($part, $sep);
76
    }, null);
77
}
78
79
function isAbsolutePath($path) {
80
    return strpos($path, '/') === 0;
81
}
82
function isRelativePath($path) {
83
    return strpos($path, './') === 0 || strpos($path, '../') === 0;
84
}
85
function isResourcePath($path) {
86
    return strpos($path, '://') !== false;
87
}
88
function isPath($path) {
89
    return isAbsolutePath($path) || isRelativePath($path) || isResourcePath($path);
90
}
91
92
/** returns the debug type of an object as string for exception printing */
93
function debugType($v) {
94
    if (is_object($v)) {
95
        return 'object ' . get_class($v);
96
    }
97
98
    return gettype($v);
99
}
100
101
function spliceArrayAtKey(array $array, $key, array $values, $after = true) {
102
    $new_array = [];
103
    $spliced = false;
104
    foreach ($array as $array_key => $val) {
105
        if ($array_key == $key) {
106
            $spliced = true;
107
            if ($after) {
108
                $new_array[$array_key] = $val;
109
                $new_array = array_merge($new_array, $values);
110
            } else {
111
                $new_array = array_merge($new_array, $values);
112
                $new_array[$array_key] = $val;
113
            }
114
        } else {
115
            $new_array[$array_key] = $val;
116
        }
117
    }
118
119
    if (!$spliced) {
120
        throw new PlatesException('Could not find key ' . $key . ' in array.');
121
    }
122
123
    return $new_array;
124
}
125
126
function cachedFileExists(Psr\SimpleCache\CacheInterface $cache, $ttl = 3600, $file_exists = 'file_exists') {
127
    return function($path) use ($cache, $ttl, $file_exists) {
128
        $key = 'League.Plates.file_exists.' . $path;
129
        $res = $cache->get($key);
130
        if (!$res) {
131
            $res = $file_exists($path);
132
            $cache->set($key, $res, $ttl);
133
        }
134
        return $res;
135
    };
136
}
137