Completed
Push — master ( 40fd89...070ab8 )
by Chin
01:04
created

MultilingualRegistrar::generateUriFromKey()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
nc 3
nop 2
dl 0
loc 8
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace ChinLeung\LaravelMultilingualRoutes;
4
5
use Illuminate\Routing\Route;
6
use Illuminate\Routing\RouteCollection;
7
use Illuminate\Routing\Router;
8
use Illuminate\Support\Arr;
9
10
class MultilingualRegistrar
11
{
12
    /**
13
     * The router instance.
14
     *
15
     * @var \Illuminate\Routing\Router
16
     */
17
    protected $router;
18
19
    /**
20
     * Constructor of the class.
21
     *
22
     * @param  \Illuminate\Routing\Router  $router
23
     */
24
    public function __construct(Router $router)
25
    {
26
        $this->router = $router;
27
    }
28
29
    /**
30
     * Register the routes.
31
     *
32
     * @param  string  $key
33
     * @param  mixed  $handle
34
     * @param  array  $locales
35
     * @param  array  $options
36
     * @return \Illuminate\Routing\RouteCollection
37
     */
38
    public function register(string $key, $handle, array $locales, array $options) : RouteCollection
39
    {
40
        $collection = new RouteCollection;
41
42
        foreach ($locales as $locale) {
43
            $collection->add(
44
                $this
45
                    ->registerRoute($key, $handle, $locale, $options)
46
                    ->name($this->generateNameForLocaleFromOptions($locale, $key, $options))
47
            );
48
        }
49
50
        return $collection;
51
    }
52
53
    /**
54
     * Register a single route.
55
     *
56
     * @param  string  $key
57
     * @param  mixed  $handle
58
     * @param  string  $locale
59
     * @param  array  $options
60
     * @return \Illuminate\Routing\Route
61
     */
62
    protected function registerRoute(string $key, $handle, string $locale, array $options) : Route
63
    {
64
        $route = $this->router->addRoute(
65
            $this->getRequestMethodFromOptions($options),
66
            $this->applyUniqueRegistrationKey(
67
                $this->generateUriFromKey($key, $locale),
68
                $locale
69
            ),
70
            $handle
71
        );
72
73
        if ($prefix = $this->generatePrefixForLocale($key, $locale)) {
74
            $route->setUri("{$prefix}/{$route->uri}");
75
        }
76
77
        return $this->cleanUniqueRegistrationKey($route, $locale);
78
    }
79
80
    /**
81
     * Retrieve the request method from the options.
82
     *
83
     * @param  array  $options
84
     * @return array
85
     */
86
    protected function getRequestMethodFromOptions(array $options) : array
87
    {
88
        $method = $options['method'] ?? 'get';
89
90
        if ($method == 'get') {
91
            return ['GET', 'HEAD'];
92
        }
93
94
        return [strtoupper($method)];
95
    }
96
97
    /**
98
     * Generate the name of the route based on the options.
99
     *
100
     * @param  string  $locale
101
     * @param  string  $key
102
     * @param  array  $options
103
     * @return string
104
     */
105
    protected function generateNameForLocaleFromOptions(string $locale, string $key, array $options) : string
106
    {
107
        if ($name = Arr::get($options, "names.$locale")) {
108
            return "$locale.$name";
109
        }
110
111
        return sprintf(
112
            '%s.%s',
113
            $locale,
114
            Arr::get($options, 'name', $key)
115
        );
116
    }
117
118
    /**
119
     * Generate the prefix of the route based on the options.
120
     *
121
     * @param  string  $key
122
     * @param  string  $locale
123
     * @return string|null
124
     */
125
    protected function generatePrefixForLocale(string $key, string $locale) : ?string
126
    {
127
        if ($key == '/') {
128
            return null;
129
        }
130
131
        if ($locale != config('app.fallback_locale')) {
132
            return $locale;
133
        }
134
135
        return config('laravel-multilingual-routes.prefix_fallback')
136
            ? $locale
137
            : null;
138
    }
139
140
    /**
141
     * Generate the route uri from the translation key and locale.
142
     *
143
     * @param  string  $key
144
     * @param  string  $locale
145
     * @return string
146
     */
147
    protected function generateUriFromKey(string $key, string $locale) : string
148
    {
149
        if ($key == '/') {
150
            return $locale == config('app.fallback_locale') ? '/' : "/$locale";
151
        }
152
153
        return trans("routes.$key", [], $locale);
154
    }
155
156
    /**
157
     * Apply the unique registration key to make sure the route is registered.
158
     *
159
     * @param  string  $uri
160
     * @param  string  $locale
161
     * @return string
162
     */
163
    protected function applyUniqueRegistrationKey(string $uri, string $locale) : string
164
    {
165
        return "__{$locale}__".$uri;
166
    }
167
168
    /**
169
     * Clean the unique registration key from the route uri after it has been
170
     * registered in the router.
171
     *
172
     * @param  \Illuminate\Routing\Route  $route
173
     * @param  string  $locale
174
     * @return \Illuminate\Routing\Route
175
     */
176
    protected function cleanUniqueRegistrationKey(Route $route, string $locale) : Route
177
    {
178
        return $route->setUri(str_replace("__{$locale}__", '', $route->uri));
179
    }
180
}
181