Route::getControllerNamespace()   A
last analyzed

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
cc 1
eloc 2
c 1
b 0
f 0
nc 1
nop 0
dl 0
loc 4
rs 10
1
<?php
2
3
    namespace rAPId\Routing;
4
5
    use rAPId\Config\Config;
6
7
    class Route
8
    {
9
        private $controller;
10
        private $action;
11
        private $args;
12
13
        /**
14
         * Route constructor.
15
         *
16
         * @param string $controller
17
         * @param string $action
18
         * @param array  $args
19
         */
20
        public function __construct($controller = '', $action = '', array $args = []) {
21
            $this->controller = $controller;
22
            $this->action = $action;
23
            $this->args = $args;
24
        }
25
26
27
        /**
28
         * @param $url
29
         *
30
         * @return Route
31
         */
32
        public static function parse($url) {
33
            list($controller, $action, $args) = self::splitUrl($url);
34
35
            $fully_qualified_controller = self::getControllerNamespace() . '\\' . studly_case($controller);
36
37
            // If the controller doesn't exist, it could be the name of a method for the default controller
38
            if (!class_exists($fully_qualified_controller)) {
39
                $args = merge_non_empty($action, $args);
40
                $action = $controller;
41
                $fully_qualified_controller = Config::val('default_controller');
42
            }
43
44
            $action_method = camel_case($action);
45
46
            // If $action is not a method in the controller, add it to the args
47
            if (!method_exists($fully_qualified_controller, $action_method)) {
48
                $args = merge_non_empty($action, $args);
49
                $action_method = 'index';
50
            }
51
52
            return new self($fully_qualified_controller, $action_method, $args);
53
        }
54
55
        /**
56
         * @param string $url
57
         *
58
         * @return array [$controller,$action,$args]
59
         */
60
        private static function splitUrl($url) {
61
            $full_path = explode('/', $url, 3);
62
63
            $controller = $full_path[0];
64
            $action = array_get($full_path, 1);
65
66
            $args = [];
67
            if (!empty(array_get($full_path, 2))) {
68
                $args = explode('/', $full_path[2]);
69
            }
70
71
            return [$controller, $action, $args];
72
        }
73
74
        private static function getControllerNamespace() {
75
            $class = new \ReflectionClass(Config::val('default_controller'));
76
77
            return $class->getNamespaceName();
78
        }
79
80
81
        /**
82
         * @return string
83
         */
84
        public function getController() {
85
            return $this->controller;
86
        }
87
88
        /**
89
         * @return string
90
         */
91
        public function getAction() {
92
            return $this->action;
93
        }
94
95
        /**
96
         * @return array
97
         */
98
        public function getArgs() {
99
            return $this->args;
100
        }
101
    }