Completed
Push — b0.25.0 ( 08d9c9...6b8d05 )
by Sebastian
04:56
created

Route::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
eloc 1
dl 0
loc 3
ccs 2
cts 2
cp 1
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 1
crap 1
1
<?php
2
3
/**
4
 * Linna Framework.
5
 *
6
 * @author Sebastian Rapetti <[email protected]>
7
 * @copyright (c) 2018, Sebastian Rapetti
8
 * @license http://opensource.org/licenses/MIT MIT License
9
 */
10
declare(strict_types=1);
11
12
namespace Linna\Router;
13
14
/**
15
 * Describe valid routes.
16
 */
17
class Route implements RouteInterface
18
{
19
    /**
20
     * @var array Route default vales
21
     */
22
    protected $route = [
23
        'name'       => '',
24
        'method'     => '',
25
        'url'        => '',
26
        'model'      => '',
27
        'view'       => '',
28
        'controller' => '',
29
        'action'     => '',
30
        'default'    => false,
31
        'param'      => [],
32
        'callback'   => false,
33
    ];
34
35
    /**
36
     * Constructor.
37
     *
38
     * @param array $route
39
     */
40 85
    public function __construct(array $route = [])
41
    {
42 85
        $this->route = array_replace_recursive($this->route, $route);
43 85
    }
44
45
    /**
46
     * Return route name.
47
     *
48
     * @return string
49
     */
50 6
    public function getName(): string
51
    {
52 6
        return $this->route['name'];
53
    }
54
55
    /**
56
     * Return route method.
57
     *
58
     * @return string
59
     */
60 1
    public function getMethod(): string
61
    {
62 1
        return $this->route['method'];
63
    }
64
65
    /**
66
     * Return route url.
67
     *
68
     * @return string
69
     */
70 1
    public function getUrl(): string
71
    {
72 1
        return $this->route['url'];
73
    }
74
75
    /**
76
     * Return model name.
77
     *
78
     * @return string
79
     */
80 1
    public function getModel(): string
81
    {
82 1
        return $this->route['model'];
83
    }
84
85
    /**
86
     * Return view name.
87
     *
88
     * @return string
89
     */
90 1
    public function getView(): string
91
    {
92 1
        return $this->route['view'];
93
    }
94
95
    /**
96
     * Return controller.
97
     *
98
     * @return string
99
     */
100 1
    public function getController(): string
101
    {
102 1
        return $this->route['controller'];
103
    }
104
105
    /**
106
     * Return action name.
107
     *
108
     * @return string
109
     */
110 2
    public function getAction(): string
111
    {
112 2
        return $this->route['action'];
113
    }
114
115
    /**
116
     * Return parameters.
117
     *
118
     * @return array
119
     */
120 2
    public function getParam(): array
121
    {
122 2
        return $this->route['param'];
123
    }
124
125
    /**
126
     * Return if route is set as default.
127
     *
128
     * @return bool
129
     */
130 1
    public function isDefault(): bool
131
    {
132 1
        return $this->route['default'];
133
    }
134
135
    /**
136
     * Return route callback.
137
     *
138
     * @return callable
139
     */
140 11
    public function getCallback(): callable
141
    {
142 11
        if (is_callable($this->route['callback'])) {
143 10
            return $this->route['callback'];
144
        }
145
146
        return function () {
147 1
        };
148
    }
149
150
    /**
151
     * Return route array.
152
     *
153
     * @return array
154
     */
155 72
    public function toArray(): array
156
    {
157 72
        return $this->route;
158
    }
159
}
160