Completed
Push — master ( e6cea4...6d9945 )
by Dmytro
01:56
created

Route::getUrl()   B

Complexity

Conditions 5
Paths 8

Size

Total Lines 19
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 5
eloc 12
c 1
b 0
f 0
nc 8
nop 3
dl 0
loc 19
rs 8.8571
1
<?php
2
3
namespace Asymptix\core;
4
5
use Asymptix\web\Request;
6
7
/**
8
 * Main route functionality class.
9
 *
10
 * @category Asymptix PHP Framework
11
 * @author Dmytro Zarezenko <[email protected]>
12
 * @copyright (c) 2011 - 2017, Dmytro Zarezenko
13
 *
14
 * @git https://github.com/Asymptix/Framework
15
 * @license http://opensource.org/licenses/MIT
16
 */
17
class Route {
18
19
    public $controller = null;
20
    public $action = null;
21
    public $id = null;
22
    public $customFields = [];
23
24
    public $isBackend = false;
25
26
    /**
27
     * Constructor that creates Route object from URL request string.
28
     *
29
     * @param string $request URL request string without GET params.
30
     */
31
    public function __construct($request = "") {
32
        $route = array_values(array_filter(explode("/", $request)));
33
34
        // result array must contain keys starts from 0
35
        if (isset($route[0]) && $route[0] == "admin") {
36
            array_shift($route);
37
            $this->isBackend = true;
38
        }
39
        if (count($route) < 3) {
40
            $route = array_merge($route, array_fill(0, 3 - count($route), ""));
41
        } else {
42
            $route = array_values($route);
43
        }
44
45
        list($this->controller, $this->action, $this->id) = $route;
46
47
        if (count($route) > 3) {
48
            $this->customFields[] = array_slice($route, 3);
49
        }
50
    }
51
52
    /**
53
     * Detects if current Route controller present in aliases list (used for
54
     * example for Menu functionality).
55
     *
56
     * @param array<string> $aliases List of aliases.
57
     *
58
     * @return bool
59
     */
60
    public function isOneOf($aliases) {
61
        return in_array($this->controller, $aliases);
62
    }
63
64
    /**
65
     * Detects current controller action.
66
     * Priority is Request Field value, Route->action field value, $defaultAction.
67
     *
68
     * @param string $actionFieldName $_REQUEST action field name if form was
69
     *           submitted (default: 'action').
70
     * @param string $defaultAction Default action if no action detected
71
     *           (default: 'list').
72
     *
73
     * @return string Action name.
74
     */
75
    public function getAction($actionFieldName = 'action', $defaultAction = 'list') {
76
        $action = Request::getFieldValue($actionFieldName);
77
        if (empty($action)) {
78
            if (!empty($this->action)) {
79
                $action = $this->action;
80
            } else {
81
                $action = $defaultAction;
82
            }
83
        }
84
        $this->action = $action;
85
86
        return $this->action;
87
    }
88
89
    /**
90
     * Generates URL for current Route with action, id and GET params.
91
     *
92
     * @param string $action Action.
93
     * @param mixed $id Id of the record (optional).
94
     * @param array $getParams List of GET params (optional).
95
     *
96
     * @return string URL.
97
     */
98
    public function getUrl($action = null, $id = null, $getParams = []) {
99
        $url = $this->controller . "/";
100
101
        $action = is_null($action) ? $this->action : $action;
102
        if (!is_null($id)) {
103
            $url.= $action . "/" . $id;
104
        } else {
105
            $url.= $action;
106
        }
107
108
        if (!empty($getParams)) {
109
            $url.= "?";
110
            foreach ($getParams as $key => $value) {
111
                $url.= $key . "=" . $value;
112
            }
113
        }
114
115
        return $url;
116
    }
117
118
    /**
119
     * Generates default template path for current route.
120
     *
121
     * @return string
122
     */
123
    public function tplPath() {
124
        $path = $this->controller . "/" . $this->controller;
125
        if (!empty($this->action)) {
126
            $path.= "_" . $this->action;
127
        }
128
129
        return $path;
130
    }
131
132
}
133