Request   A
last analyzed

Complexity

Total Complexity 21

Size/Duplication

Total Lines 154
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 21
eloc 66
c 1
b 0
f 0
dl 0
loc 154
ccs 62
cts 62
cp 1
rs 10

7 Methods

Rating   Name   Duplication   Size   Complexity  
A pathToSegments() 0 30 4
A parseParamsToArray() 0 4 1
A getData() 0 8 1
A __construct() 0 4 1
A updateVirtualKeys() 0 23 6
A explodeInput() 0 3 2
A parseVirtualPath() 0 23 6
1
<?php
2
3
namespace kalanis\kw_routed_paths\Sources;
4
5
6
use kalanis\kw_paths\Interfaces\IPaths;
7
use kalanis\kw_routed_paths\Support;
8
9
10
/**
11
 * Class Request
12
 * @package kalanis\kw_routed_paths\Sources
13
 * Input source is Request Uri in preset variables
14
 * This one is for accessing content with url rewrite engines
15
 */
16
class Request extends ASource
17
{
18
    protected string $requestUri = '';
19
    protected ?string $virtualDir = '';
20
21
    /**
22
     * @param string $requestUri
23
     * @param string|null $virtualDir
24
     *
25
     * virtual dir has following variants:
26
     * empty string - cut on start of query string - cut on first possible position
27
     * null - cut on end of query string - don't know when cut
28
     * something - cut where you find that "prefix"
29
     */
30 47
    public function __construct(string $requestUri, ?string $virtualDir = null)
31
    {
32 47
        $this->requestUri = $requestUri;
33 47
        $this->virtualDir = $virtualDir;
34 47
    }
35
36
    /**
37
     * @return array<string|int, mixed|null> $params
38
     * path is composed from:
39
     * static part - for subdirectory on server
40
     * virtual prefix - something to split the string
41
     * path and other keys - after splitting to describe what client want
42
     */
43 47
    public function getData(): array
44
    {
45 47
        list($path, $params) = $this->explodeInput($this->requestUri);
46 47
        list($staticPath, $virtualPrefix, $virtualParamPath) = $this->pathToSegments(urldecode($path), $this->virtualDir);
47 47
        return array_merge(
48 47
                compact('staticPath', 'virtualPrefix'),
49 47
                $this->updateVirtualKeys($this->parseVirtualPath($virtualParamPath)),
50 47
                $this->parseParamsToArray($params)
51
        );
52
    }
53
54
    /**
55
     * @param string $uri
56
     * @return string[]
57
     */
58 47
    protected function explodeInput(string $uri): array
59
    {
60 47
        return (false !== strpos($uri, IPaths::SPLITTER_QUOTE)) ? explode(IPaths::SPLITTER_QUOTE, $uri, 2) : [$uri, ''];
61
    }
62
63
    /**
64
     * @param string $path
65
     * @param string|null $fakeDir
66
     * @return array<string|null>
67
     */
68 47
    protected function pathToSegments(string $path, ?string $fakeDir): array
69
    {
70 47
        if (!empty($fakeDir)) {
71 17
            $splitPos = mb_strpos($path, $fakeDir);
72 17
            if (false !== $splitPos) {
73
                // got rewrite!
74 15
                $statical = mb_substr($path, 0, $splitPos);
75 15
                $mask = $fakeDir;
76 15
                $virtual = mb_substr($path, $splitPos + mb_strlen($fakeDir));
77
            } else {
78
                // no rewrite!
79 2
                $statical = $path;
80 2
                $mask = null;
81 17
                $virtual = null;
82
            }
83
        } else {
84 30
            if (is_null($fakeDir)) {
0 ignored issues
show
introduced by
The condition is_null($fakeDir) is always true.
Loading history...
85
                // no rewrite!
86 12
                $statical = $path;
87 12
                $mask = null;
88 12
                $virtual = null;
89
            } else {
90
                // no rewrite!
91 18
                $statical = null;
92 18
                $mask = null;
93 18
                $virtual = $path;
94
            }
95
        }
96
97 47
        return [$statical, $mask, $virtual];
98
    }
99
100
    /**
101
     * @param string $param
102
     * @return array<string|int, array<mixed>|string|int|bool>
103
     */
104 47
    protected function parseParamsToArray(string $param): array
105
    {
106 47
        parse_str(html_entity_decode($param, ENT_QUOTES | ENT_HTML5, 'UTF-8'), $result);
107 47
        return $result;
108
    }
109
110
    /**
111
     * @param string|null $path
112
     * @return array<string|int, mixed>
113
     */
114 47
    protected function parseVirtualPath(?string $path): array
115
    {
116 47
        $params = [];
117 47
        if (is_null($path)) {
118 14
            return $params;
119
        }
120 33
        if (false != preg_match_all('#([a-zA-Z0-9_-]+):([a-zA-Z0-9_-]*)#ui', $path, $matches)) {
0 ignored issues
show
Bug Best Practice introduced by
It seems like you are loosely comparing preg_match_all('#([a-zA-...)#ui', $path, $matches) of type integer|null against false; this is ambiguous if the integer can be zero. Consider using a strict comparison !== instead.
Loading history...
121
            // for each containing colons
122 18
            foreach ($matches[0] as $index => $match) {
123 18
                $params[$matches[1][$index]] = $matches[2][$index];
124
            }
125
            // lookup for last part which does not contain colon
126 18
            if (false != preg_match_all('#:[a-zA-Z0-9_-]*\/([^:]+)#ui', $path, $found)) {
0 ignored issues
show
Bug Best Practice introduced by
It seems like you are loosely comparing preg_match_all('#:[a-zA-...]+)#ui', $path, $found) of type integer|null against false; this is ambiguous if the integer can be zero. Consider using a strict comparison !== instead.
Loading history...
127 12
                $foundPath = end($found[1]);
128 12
                if (false !== $foundPath) {
129 18
                    $params['path'] = $foundPath;
130
                }
131
            }
132
        } else {
133
            // no colon, just path
134 15
            $params['path'] = $path;
135
        }
136 33
        return $params;
137
    }
138
139
    /**
140
     * @param array<string|int, mixed> $params
141
     * @return array<string|int, mixed>
142
    // U:user
143
    // M:module
144
    // MS:solo_module
145
    // L:lang
146
     */
147 47
    protected function updateVirtualKeys(array $params): array
148
    {
149 47
        $result = [];
150 47
        foreach ($params as $key => $param) {
151 33
            switch (strtolower(strval($key))) {
152
                case Support::PREFIX_MOD_SINGLE:
153 7
                    $result['module'] = $param;
154 7
                    $result['single'] = true;
155 7
                    break;
156
                case Support::PREFIX_MOD_NORMAL:
157 8
                    $result['module'] = $param;
158 8
                    break;
159
                case Support::PREFIX_USER:
160 11
                    $result['user'] = $param;
161 11
                    break;
162
                case Support::PREFIX_LANG:
163 4
                    $result['lang'] = $param;
164 4
                    break;
165
                default:
166 27
                    $result[$key] = $param;
167
            }
168
        }
169 47
        return $result;
170
    }
171
}
172