Passed
Push — main ( 917548...d03d31 )
by Alex
01:13
created

Resource::setRequest()   A

Complexity

Conditions 4
Paths 8

Size

Total Lines 17
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 4
eloc 10
c 1
b 0
f 0
nc 8
nop 1
dl 0
loc 17
rs 9.9332
1
<?php
2
3
namespace Erykai\Routes;
4
5
/**
6
 *
7
 */
8
abstract class Resource
9
{
10
    use TraitRoute;
11
    /**
12
     * @var array
13
     */
14
    protected array $route;
15
    /**
16
     * @var string
17
     */
18
    protected string $method;
19
    /**
20
     * @var string
21
     */
22
    protected string $request;
23
    /**
24
     * @var array|null
25
     */
26
    protected ?array $query = null;
27
    /**
28
     * @var string
29
     */
30
    protected string $namespace;
31
    /**
32
     * @var array
33
     */
34
    protected array $namespaceArray;
35
    /**
36
     * @var array
37
     */
38
    protected array $patterns;
39
    /**
40
     * @var bool
41
     */
42
    protected bool $notFound = true;
43
    /**
44
     * @var object
45
     */
46
    private object $response;
47
    /**
48
     * @var array
49
     */
50
    protected array $callback;
51
    /**
52
     * @var array
53
     */
54
    protected array $controller;
55
    /**
56
     * @var array
57
     */
58
    protected array $middleware;
59
    /**
60
     * @var array
61
     */
62
    protected array $type;
63
    /**
64
     * @var array
65
     */
66
    protected array $verb;
67
    protected function callback($callback, $controller, $verb, $middleware, $type): void
68
    {
69
        if ($this->setRequest($callback)) {
70
            $this->setRoute($callback);
71
            $this->setPatterns();
72
            $this->controller[] = $controller;
73
            $this->middleware[] = $middleware;
74
            $this->type[] = $type;
75
            $this->verb[] = $verb;
76
            $this->namespaceArray[] = $this->getNamespace();
77
78
        }
79
    }
80
    /**
81
     * @param string $namespace
82
     */
83
    protected function setNamespace(string $namespace): void
84
    {
85
        $this->namespace = $namespace;
86
    }
87
    /**
88
     * @param string $route
89
     */
90
    protected function setRoute(string $route): void
91
    {
92
        $this->route[] = $route;
93
    }
94
    /**
95
     * create pattern
96
     */
97
    protected function setPatterns(): void
98
    {
99
        $this->patterns = preg_replace('~{([^}]*)}~', "([^/]+)", $this->getRoute());
100
    }
101
    /**
102
     * define method global server
103
     */
104
    protected function setMethod(): void
105
    {
106
        $this->method = filter_input(INPUT_SERVER, 'REQUEST_METHOD', FILTER_DEFAULT);
107
    }
108
    /**
109
     * @param string $route
110
     * @return bool
111
     */
112
    protected function setRequest(string $route): bool
113
    {
114
        $uri = parse_url(filter_input(INPUT_SERVER, 'REQUEST_URI', FILTER_DEFAULT));
115
        $this->request = $uri['path'];
116
117
        $callbackCount = count(explode("/", $route));
118
        $requestCount = count(explode("/", $this->request));
119
120
        if (str_ends_with($this->request, '/')) {
121
            $this->request = substr($this->request, 0, -1);
122
        }
123
        if (isset($uri['query'])) {
124
            parse_str($uri['query'], $query);
125
            $this->setQuery($query);
126
        }
127
128
        return $callbackCount === $requestCount || $this->request === '/';
129
130
    }
131
    /**
132
     * @param array|null $query
133
     */
134
    protected function setQuery(?array $query): void
135
    {
136
        $this->query = $query;
137
    }
138
    /**
139
     * @param int $code
140
     * @param string $type
141
     * @param string $message
142
     * @param object|null $data
143
     * @param string|null $dynamic
144
     */
145
    protected function setResponse(int $code, string $type, string $message, ?object $data = null, ?string $dynamic = null): void
146
    {
147
        $this->response = (object)[
148
            "code" => $code,
149
            "type" => $type,
150
            "message" => $message,
151
            "data" => $data,
152
            "dynamic" => $dynamic
153
        ];
154
    }
155
    /**
156
     * @param bool $notFound
157
     */
158
    protected function setNotFound(bool $notFound): void
159
    {
160
        $this->notFound = $notFound;
161
    }
162
163
}