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

Resource::setNotFound()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 1
c 1
b 0
f 0
nc 1
nop 1
dl 0
loc 3
rs 10
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
    /**
68
     * construct
69
     */
70
    public function __construct()
71
    {
72
        $this->setMethod();
73
        $this->setResponse(200, "success", "return correct route");
74
    }
75
    protected function callback($callback, $controller, $verb, $middleware, $type): void
76
    {
77
        if ($this->setRequest($callback)) {
78
            $this->setRoute($callback);
79
            $this->setPatterns();
80
            $this->controller[] = $controller;
81
            $this->middleware[] = $middleware;
82
            $this->type[] = $type;
83
            $this->verb[] = $verb;
84
            $this->namespaceArray[] = $this->getNamespace();
85
86
        }
87
    }
88
    /**
89
     * @param string $namespace
90
     */
91
    protected function setNamespace(string $namespace): void
92
    {
93
        $this->namespace = $namespace;
94
    }
95
    /**
96
     * @param string $route
97
     */
98
    protected function setRoute(string $route): void
99
    {
100
        $this->route[] = $route;
101
    }
102
    /**
103
     * create pattern
104
     */
105
    protected function setPatterns(): void
106
    {
107
        $this->patterns = preg_replace('~{([^}]*)}~', "([^/]+)", $this->getRoute());
108
    }
109
    /**
110
     * define method global server
111
     */
112
    protected function setMethod(): void
113
    {
114
        $this->method = filter_input(INPUT_SERVER, 'REQUEST_METHOD', FILTER_DEFAULT);
115
    }
116
    /**
117
     * @param string $route
118
     * @return bool
119
     */
120
    protected function setRequest(string $route): bool
121
    {
122
        $uri = parse_url(filter_input(INPUT_SERVER, 'REQUEST_URI', FILTER_DEFAULT));
123
        $this->request = $uri['path'];
124
125
        $callbackCount = count(explode("/", $route));
126
        $requestCount = count(explode("/", $this->request));
127
128
        if (str_ends_with($this->request, '/')) {
129
            $this->request = substr($this->request, 0, -1);
130
        }
131
        if (isset($uri['query'])) {
132
            parse_str($uri['query'], $query);
133
            $this->setQuery($query);
134
        }
135
136
        return $callbackCount === $requestCount || $this->request === '/';
137
138
    }
139
    /**
140
     * @param array|null $query
141
     */
142
    protected function setQuery(?array $query): void
143
    {
144
        $this->query = $query;
145
    }
146
    /**
147
     * @param int $code
148
     * @param string $type
149
     * @param string $message
150
     * @param object|null $data
151
     * @param string|null $dynamic
152
     */
153
    protected function setResponse(int $code, string $type, string $message, ?object $data = null, ?string $dynamic = null): void
154
    {
155
        $this->response = (object)[
156
            "code" => $code,
157
            "type" => $type,
158
            "message" => $message,
159
            "data" => $data,
160
            "dynamic" => $dynamic
161
        ];
162
    }
163
    /**
164
     * @param bool $notFound
165
     */
166
    protected function setNotFound(bool $notFound): void
167
    {
168
        $this->notFound = $notFound;
169
    }
170
}