Completed
Push — v4.0 ( 3a8f05...488a49 )
by
unknown
03:09
created

util.php ➔ id()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 11
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 7
nc 2
nop 1
dl 0
loc 11
rs 9.4285
c 0
b 0
f 0
1
<?php
2
3
namespace League\Plates\Util;
4
5
use League\Plates\Exception\ComposeException;
6
7
function id($multi = false) {
8
    if ($multi) {
9
        return function(...$args) {
10
            return $args;
11
        };
12
    } else {
13
        return function($arg) {
14
            return $arg;
15
        };
16
    }
17
}
18
19
/** stack a set of functions into each other and returns the stacked func */
20
function stack(array $funcs) {
21
    return array_reduce(array_reverse($funcs), function($next, $func) {
22
        return function(...$args) use ($next, $func) {
23
            $args[] = $next;
24
            return $func(...$args);
25
        };
26
    }, function() {
27
        throw new ComposeException('No handler was able to return a result.');
28
    });
29
}
30
31
function compose(array $funcs, $multi = false) {
32
    if (!$multi) {
33
        return function($arg) use ($funcs) {
34
            return array_reduce($funcs, function($acc, $func) {
35
                return $func($acc);
36
            }, $arg);
37
        };
38
    }
39
40
    return function(...$args) use ($funcs) {
41
        return array_reduce($funcs, function($acc, $func) {
42
            return $func(...$acc);
43
        }, $args);
44
    };
45
}
46
47
48
function joinPath(array $parts, $sep = DIRECTORY_SEPARTOR) {
49
    return array_reduce($parts, function($acc, $part) use ($sep) {
50
        if ($acc === null) {
51
            return rtrim($part, $sep);
52
        }
53
54
        return $acc . $sep . ltrim($part, $sep);
55
    }, null);
56
}