Completed
Push — 183-default-layout-ext ( a97107 )
by
unknown
10:37
created

util.php ➔ spliceArrayAtKey()   B

Complexity

Conditions 5
Paths 8

Size

Total Lines 24
Code Lines 17

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 5
eloc 17
nc 8
nop 4
dl 0
loc 24
rs 8.5125
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
/** stack a set of functions into each other and returns the stacked func */
23
function stack(array $funcs) {
24
    return array_reduce(array_reverse($funcs), function($next, $func) {
25
        return function(...$args) use ($next, $func) {
26
            $args[] = $next;
27
            return $func(...$args);
28
        };
29
    }, function() {
30
        throw new StackException('No handler was able to return a result.');
31
    });
32
}
33
34
/** takes a structured array and sorts them by priority. This allows for prioritized stacks.
35
    The structure is just an associative array where the indexes are numeric and the values
36
    are array of stack handlers. The array is sorted by key and then all inner arrays are merged
37
    together */
38
function sortStacks($stacks) {
39
    ksort($stacks);
40
    return array_merge(...$stacks);
41
}
42
43
function stackGroup(array $funcs) {
44
    $end_next = null;
45
    $funcs[] = function(...$args) use (&$end_next) {
46
        return $end_next(...array_slice($args, 0, -1));
47
    };
48
    $next = stack($funcs);
49
    return function(...$args) use ($next, &$end_next) {
50
        $end_next = end($args);
51
        return $next(...array_slice($args, 0, -1));
52
    };
53
}
54
55
function compose(...$funcs) {
56
    return function($arg) use ($funcs) {
57
        return array_reduce($funcs, function($acc, $func) {
58
            return $func($acc);
59
        }, $arg);
60
    };
61
}
62
63
function joinPath(array $parts, $sep = DIRECTORY_SEPARATOR) {
64
    return array_reduce(array_filter($parts), function($acc, $part) use ($sep) {
65
        if ($acc === null) {
66
            return rtrim($part, $sep);
67
        }
68
69
        return $acc . $sep . ltrim($part, $sep);
70
    }, null);
71
}
72
73
/** returns the debug type of an object as string for exception printing */
74
function debugType($v) {
75
    if (is_object($v)) {
76
        return 'object ' . get_class($v);
77
    }
78
79
    return gettype($v);
80
}
81
82
function spliceArrayAtKey(array $array, $key, array $values, $after = true) {
83
    $new_array = [];
84
    $spliced = false;
85
    foreach ($array as $array_key => $val) {
86
        if ($array_key == $key) {
87
            $spliced = true;
88
            if ($after) {
89
                $new_array[$array_key] = $val;
90
                $new_array = array_merge($new_array, $values);
91
            } else {
92
                $new_array = array_merge($new_array, $values);
93
                $new_array[$array_key] = $val;
94
            }
95
        } else {
96
            $new_array[$array_key] = $val;
97
        }
98
    }
99
100
    if (!$spliced) {
101
        throw new PlatesException('Could not find key ' . $key . ' in array.');
102
    }
103
104
    return $new_array;
105
}
106
107
function cachedFileExists(Psr\SimpleCache\CacheInterface $cache, $ttl = 3600, $file_exists = 'file_exists') {
108
    return function($path) use ($cache, $ttl, $file_exists) {
109
        $key = 'League.Plates.file_exists.' . $path;
110
        $res = $cache->get($key);
111
        if (!$res) {
112
            $res = $file_exists($path);
113
            $cache->set($key, $res, $ttl);
114
        }
115
        return $res;
116
    };
117
}
118