Completed
Push — master ( 6d3eb9...bd657a )
by
unknown
14:36
created

util.php ➔ when()   A

Complexity

Conditions 2
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 0
Metric Value
cc 2
eloc 3
nc 1
nop 2
dl 0
loc 5
ccs 0
cts 0
cp 0
crap 6
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 28
        return $arg;
11 28
    };
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 12
        echo $v;
19 20
    };
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($funcs, function($next, $func) {
25
        return function(...$args) use ($next, $func) {
26 52
            $args[] = $next;
27 52
            return $func(...$args);
28 52
        };
29
    }, function() {
30 4
        throw new StackException('No handler was able to return a result.');
31 56
    });
32
}
33
34
function stackGroup(array $funcs) {
35 20
    $end_next = null;
36
    array_unshift($funcs, function(...$args) use (&$end_next) {
37 4
        return $end_next(...array_slice($args, 0, -1));
38 20
    });
39 20
    $next = stack($funcs);
40
    return function(...$args) use ($next, &$end_next) {
41 16
        $end_next = end($args);
42 16
        return $next(...array_slice($args, 0, -1));
43 20
    };
44
}
45
46
/** compose(f, g)(x) = f(g(x)) */
47
function compose(...$funcs) {
48 16
    return pipe(...array_reverse($funcs));
49
}
50
51
/** pipe(f, g)(x) = g(f(x)) */
52
function pipe(...$funcs) {
53
    return function($arg) use ($funcs) {
54
        return array_reduce($funcs, function($acc, $func) {
55 16
            return $func($acc);
56 16
        }, $arg);
57 16
    };
58
}
59
60
function joinPath(array $parts, $sep = DIRECTORY_SEPARATOR) {
61
    return array_reduce(array_filter($parts), function($acc, $part) use ($sep) {
62 28
        if ($acc === null) {
63 28
            return rtrim($part, $sep);
64
        }
65
66 28
        return $acc . $sep . ltrim($part, $sep);
67 28
    }, null);
68
}
69
70
function isAbsolutePath($path) {
71 32
    return strpos($path, '/') === 0;
72
}
73
function isRelativePath($path) {
74 28
    return strpos($path, './') === 0 || strpos($path, '../') === 0;
75
}
76
function isResourcePath($path) {
77 16
    return strpos($path, '://') !== false;
78
}
79
function isPath($path) {
80 24
    return isAbsolutePath($path) || isRelativePath($path) || isResourcePath($path);
81
}
82
83
/** returns the debug type of an object as string for exception printing */
84
function debugType($v) {
85 8
    if (is_object($v)) {
86 4
        return 'object ' . get_class($v);
87
    }
88
89 4
    return gettype($v);
90
}
91
92
function spliceArrayAtKey(array $array, $key, array $values, $after = true) {
93
    $new_array = [];
94
    $spliced = false;
95
    foreach ($array as $array_key => $val) {
96
        if ($array_key == $key) {
97
            $spliced = true;
98
            if ($after) {
99
                $new_array[$array_key] = $val;
100
                $new_array = array_merge($new_array, $values);
101
            } else {
102
                $new_array = array_merge($new_array, $values);
103
                $new_array[$array_key] = $val;
104
            }
105
        } else {
106
            $new_array[$array_key] = $val;
107
        }
108
    }
109
110
    if (!$spliced) {
111
        throw new PlatesException('Could not find key ' . $key . ' in array.');
112
    }
113
114
    return $new_array;
115
}
116
117
function cachedFileExists(Psr\SimpleCache\CacheInterface $cache, $ttl = 3600, $file_exists = 'file_exists') {
118
    return function($path) use ($cache, $ttl, $file_exists) {
119
        $key = 'League.Plates.file_exists.' . $path;
120
        $res = $cache->get($key);
121
        if (!$res) {
122
            $res = $file_exists($path);
123
            $cache->set($key, $res, $ttl);
124
        }
125
        return $res;
126
    };
127
}
128
129
/** Invokes the callable if the predicate returns true. Returns the value otherwise. */
130
function when(callable $predicate, callable $fn) {
131
    return function($arg) use ($predicate, $fn) {
132
        return $predicate($arg) ? $fn($arg) : $arg;
133
    };
134
}
135