Completed
Push — master ( ac854d...92cccc )
by Restu
12:32
created

Router::getRoutes()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 4
rs 10
cc 1
eloc 2
nc 1
nop 0
1
<?php
2
namespace JayaCode\Framework\Core\Router;
3
4
use JayaCode\Framework\Core\Helper\HelperArray;
5
use JayaCode\Framework\Core\Http\Request;
6
use JayaCode\Framework\Core\Http\Response;
7
8
class Router
9
{
10
    /**
11
     * @var array
12
     */
13
    public $routes = array(
14
        [
15
            "id"    => "home",
16
            "path"  => "/",
17
            "controller"    => "HomeController",
18
            "action"        => "index"
19
        ]
20
    );
21
22
    /**
23
     * @var Request
24
     */
25
    public $request;
26
27
    /**
28
     * @var Response
29
     */
30
    public $response;
31
32
    /**
33
     * Router constructor.
34
     * @param Request $request
35
     * @param Response $response
36
     */
37
    public function __construct(Request &$request = null, Response &$response = null)
38
    {
39
        $this->initialize($request, $response);
40
    }
41
42
    /**
43
     * initialize Router
44
     * @param Request $request
45
     * @param Response $response
46
     */
47
    public function initialize(Request &$request = null, Response &$response = null)
48
    {
49
        $this->request = &$request;
50
        $this->response = &$response;
51
    }
52
53
    /**
54
     * Create Router object from static function
55
     * @param Request $request
56
     * @param Response $response
57
     * @return Router
58
     */
59
    public static function create(Request &$request = null, Response &$response = null)
60
    {
61
        return new Router($request, $response);
62
    }
63
64
    /**
65
     * Handle request
66
     */
67
    public function handle()
68
    {
69
        $path = $this->request->path();
70
        $route = $this->getRouteByPath($path);
71
72
        if (!is_null($route)) {
73
            if (arr_has_key($route, "controller") && arr_has_key($route, "action")) {
74
                $controller_class = "App\\Controller\\" . arr_get($route, "controller");
75
76
                if (!class_exists($controller_class)) {
77
                    throw new \InvalidArgumentException("controller " . $controller_class . " not found");
78
                }
79
80
                $controller = new $controller_class($this->request, $this->response);
81
82
                $action = arr_has_key($route, "action");
83
84
                if (!method_exists($controller, $action)) {
85
                    throw new \InvalidArgumentException("method " . $action . " not found");
86
                }
87
88
                $content = $controller->$action();
89
                $this->response->setContent($content);
90
            }
91
        } else {
92
            $this->response->setStatusCode(404);
93
        }
94
95
        return $this->response;
96
    }
97
98
    /**
99
     * @param null $id
100
     * @return array
101
     */
102
    public function getRoute($id = null)
103
    {
104
        if (is_null($id)) {
105
            return $this->routes;
106
        }
107
108
        if (!is_string($id)) {
109
            throw new \InvalidArgumentException("var id must be a string");
110
        }
111
112
        foreach ($this->routes as $route) {
113
            if (arr_has_key($route, "id") && arr_get($route, "id") == $id) {
114
                return $route;
115
            }
116
        }
117
118
        throw new \OutOfBoundsException("not found route with id " . $id);
119
    }
120
121
    /**
122
     * @param null $path
123
     * @return array
124
     */
125
    public function getRouteByPath($path = null)
126
    {
127
        if (!is_string($path)) {
128
            throw new \InvalidArgumentException("var path must be a string");
129
        }
130
131
        foreach ($this->routes as $route) {
132
            if (arr_has_key($route, "path") && arr_get($route, "path") == $path) {
133
                return $route;
134
            }
135
        }
136
137
        return null;
138
    }
139
140
    /**
141
     * @param array $routes
142
     */
143
    public function setRoutes($routes)
144
    {
145
        $this->routes = $routes;
146
    }
147
}
148