Completed
Push — master ( fe9181...bc0bfa )
by ARCANEDEV
12s
created

Router::transRoute()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
dl 0
loc 4
c 1
b 0
f 0
cc 1
eloc 2
nc 1
nop 1
ccs 2
cts 2
cp 1
crap 1
rs 10
1
<?php namespace Arcanedev\Localization\Routing;
2
3
use Closure;
4
use Illuminate\Routing\Router as IlluminateRouter;
5
6
/**
7
 * Class     Router
8
 *
9
 * @package  Arcanedev\Localization\Routing
10
 * @author   ARCANEDEV <[email protected]>
11
 */
12
class Router extends IlluminateRouter
13
{
14
    /* ------------------------------------------------------------------------------------------------
15
     |  Getters & Setters
16
     | ------------------------------------------------------------------------------------------------
17
     */
18
    /**
19
     * Get active middlewares.
20
     *
21
     * @return array
22
     */
23 192
    protected function getActiveMiddlewares()
24
    {
25 192
        return array_keys(array_filter(
26 192
            config('localization.route.middleware', [])
27 64
        ));
28
    }
29
30
    /* ------------------------------------------------------------------------------------------------
31
     |  Route Functions
32
     | ------------------------------------------------------------------------------------------------
33
     */
34
    /**
35
     * Create a route group with shared attributes.
36
     *
37
     * @param  array     $attributes
38
     * @param  \Closure  $callback
39
     */
40 192
    public function localizedGroup(Closure $callback, $attributes = [])
41
    {
42 192
        $attributes = array_merge($attributes, [
43 192
            'prefix'     => localization()->setLocale(),
44 192
            'middleware' => $this->getActiveMiddlewares(),
45 64
        ]);
46
47 192
        $this->group(array_filter($attributes), $callback);
48 192
    }
49
50
    /**
51
     * Register a new translated GET route with the router.
52
     *
53
     * @param  string                 $trans
54
     * @param  \Closure|array|string  $action
55
     *
56
     * @return \Illuminate\Routing\Route
57
     */
58 192
    public function transGet($trans, $action)
59
    {
60 192
        return $this->get(
61 192
            $this->transRoute($trans), $action
62 64
        );
63
    }
64
65
    /**
66
     * Register a new translated POST route with the router.
67
     *
68
     * @param  string                 $trans
69
     * @param  \Closure|array|string  $action
70
     *
71
     * @return \Illuminate\Routing\Route
72
     */
73 192
    public function transPost($trans, $action)
74
    {
75 192
        return $this->post(
76 192
            $this->transRoute($trans), $action
77 64
        );
78
    }
79
80
    /**
81
     * Register a new translated PUT route with the router.
82
     *
83
     * @param  string                 $trans
84
     * @param  \Closure|array|string  $action
85
     *
86
     * @return \Illuminate\Routing\Route
87
     */
88 192
    public function transPut($trans, $action)
89
    {
90 192
        return $this->put(
91 192
            $this->transRoute($trans), $action
92 64
        );
93
    }
94
95
    /**
96
     * Register a new translated PATCH route with the router.
97
     *
98
     * @param  string                 $trans
99
     * @param  \Closure|array|string  $action
100
     *
101
     * @return \Illuminate\Routing\Route
102
     */
103 192
    public function transPatch($trans, $action)
104
    {
105 192
        return $this->patch(
106 192
            $this->transRoute($trans), $action
107 64
        );
108
    }
109
110
    /**
111
     * Register a new translated DELETE route with the router.
112
     *
113
     * @param  string                 $trans
114
     * @param  \Closure|array|string  $action
115
     *
116
     * @return \Illuminate\Routing\Route
117
     */
118 192
    public function transDelete($trans, $action)
119
    {
120 192
        return $this->delete(
121 192
            $this->transRoute($trans), $action
122 64
        );
123
    }
124
125
    /**
126
     * Register a new translated OPTIONS route with the router.
127
     *
128
     * @param  string                 $trans
129
     * @param  \Closure|array|string  $action
130
     *
131
     * @return \Illuminate\Routing\Route
132
     */
133 192
    public function transOptions($trans, $action)
134
    {
135 192
        return $this->options(
136 192
            $this->transRoute($trans), $action
137 64
        );
138
    }
139
140
    /**
141
     * Register a new translated any route with the router.
142
     *
143
     * @param  string                 $trans
144
     * @param  \Closure|array|string  $action
145
     *
146
     * @return \Illuminate\Routing\Route
147
     */
148 192
    public function transAny($trans, $action)
149
    {
150 192
        return $this->any(
151 192
            $this->transRoute($trans), $action
152 64
        );
153
    }
154
155
    /* -----------------------------------------------------------------
156
     |  Other Methods
157
     | -----------------------------------------------------------------
158
     */
159
    /**
160
     * Translate the route.
161
     *
162
     * @param  string  $key
163
     *
164
     * @return string
165
     */
166 192
    private function transRoute($key)
167
    {
168 192
        return localization()->transRoute($key);
169
    }
170
}
171