Passed
Push — master ( 957c9d...4b4e9a )
by Бабичев
37:59 queued 32:50
created

route()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 10
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 5
nc 1
nop 2
dl 0
loc 10
rs 9.4285
c 0
b 0
f 0
1
<?php
2
3
namespace Bavix\Router;
4
5
/**
6
 * @param string $variableName
7
 * @param mixed  $default
8
 *
9
 * @return mixed
10
 */
11
function server($variableName, $default = null)
12
{
13
    static $storage = [];
14
15
    if (!isset($storage[$variableName]))
16
    {
17
        $storage[$variableName] =
18
            filter_input(INPUT_SERVER, $variableName) ??
19
            $default;
20
    }
21
22
    return $storage[$variableName];
23
}
24
25
/**
26
 * @return string
27
 */
28
function method()
29
{
30
    return server('REQUEST_METHOD');
31
}
32
33
/**
34
 * @return string
35
 */
36
function protocol()
37
{
38
    $protocol = server('HTTP_CF_VISITOR'); // cloudFlare
39
40
    if ($protocol)
41
    {
42
        /**
43
         * { scheme: "https" }
44
         *
45
         * @var string $protocol
46
         */
47
        $protocol = json_decode($protocol);
48
    }
49
50
    return $protocol['scheme'] ??
51
        server('HTTP_X_FORWARDED_PROTO') ??
52
        server('REQUEST_SCHEME');
53
}
54
55
/**
56
 * @return string
57
 */
58
function host()
59
{
60
    return server('HTTP_HOST');
61
}
62
63
/**
64
 * @return string
65
 */
66
function isAjax()
67
{
68
    return server('HTTP_X_REQUESTED_WITH') === 'xmlhttprequest';
69
}
70
71
/**
72
 * @return string
73
 */
74
function path()
75
{
76
    return parse_url(server('REQUEST_URI'), PHP_URL_PATH);
77
}
78
79
/**
80
 * @param Route $route
81
 * @param array $attributes
82
 *
83
 * @return string
84
 */
85
function route(Route $route, array $attributes = [])
86
{
87
    $attributes += $route->getDefaults();
88
89
    $path = preg_replace_callback('~\<(\w+)\>~', function ($matches) use ($attributes)
90
    {
91
        return $attributes[$matches[1]] ?? null;
92
    }, $route->getFilterPath());
93
94
    return preg_replace('~(\(/\)|\(|\)|//)~', '', $path);
95
}
96