Completed
Push — master ( ed8b78...efa770 )
by Chin
62:39 queued 59:16
created

MultilingualRegistrar::generateRoute()   A

Complexity

Conditions 4
Paths 6

Size

Total Lines 14

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 4
nc 6
nop 2
dl 0
loc 14
rs 9.7998
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
        $method = $this->getRequestMethodFromOptions($options);
43
44
        foreach ($locales as $locale) {
45
            $collection->add(
46
                $this
47
                    ->registerRoute($method, $key, $handle, $locale, $options)
48
                    ->name($this->generateNameForLocaleFromOptions($locale, $key, $options))
49
            );
50
        }
51
52
        return $collection;
53
    }
54
55
    /**
56
     * Register a single route.
57
     *
58
     * @param  string  $method
59
     * @param  string  $key
60
     * @param  mixed  $handle
61
     * @param  string  $locale
62
     * @param  array  $options
63
     * @return \Illuminate\Routing\Route
64
     */
65
    protected function registerRoute(string $method, string $key, $handle, string $locale, array $options) : Route
66
    {
67
        $route = $this->generateRoute($key, $locale);
68
69
        if (is_null($handle)) {
70
            return $this->router->view($route, Arr::get($options, 'view', $key));
71
        }
72
73
        return $this->router->{strtolower($method)}(
74
            $route,
75
            $handle
76
        );
77
    }
78
79
    /**
80
     * Retrieve the request method from the options.
81
     *
82
     * @param  array  $options
83
     * @return string
84
     */
85
    protected function getRequestMethodFromOptions(array $options) : string
86
    {
87
        return $options['method'] ?? 'get';
88
    }
89
90
    /**
91
     * Generate the name of the route based on the options.
92
     *
93
     * @param  string  $locale
94
     * @param  string  $key
95
     * @param  array  $options
96
     * @return string
97
     */
98
    protected function generateNameForLocaleFromOptions(string $locale, string $key, array $options) : string
99
    {
100
        if ($name = Arr::get($options, "names.$locale")) {
101
            return "$locale.$name";
102
        }
103
104
        return sprintf(
105
            '%s.%s',
106
            $locale,
107
            Arr::get($options, 'name', $key)
108
        );
109
    }
110
111
    /**
112
     * Generate the prefix of the route based on the options.
113
     *
114
     * @param  string  $key
115
     * @param  string  $locale
116
     * @return string|null
117
     */
118
    protected function generatePrefixForLocale(string $key, string $locale) : ?string
119
    {
120
        if ($key == '/') {
121
            return null;
122
        }
123
124
        if ($locale != config('app.fallback_locale')) {
125
            return $locale;
126
        }
127
128
        return config('laravel-multilingual-routes.prefix_fallback')
129
            ? $locale
130
            : null;
131
    }
132
133
    /**
134
     * Generate a route from the key and locale.
135
     *
136
     * @param  string  $key
137
     * @param  string  $locale
138
     * @return string
139
     */
140
    protected function generateRoute(string $key, string $locale) : string
141
    {
142
        if ($key == '/') {
143
            $route = $locale == config('app.fallback_locale') ? '/' : "/$locale";
144
        } else {
145
            $route = trans("routes.$key", [], $locale);
146
        }
147
148
        if ($prefix = $this->generatePrefixForLocale($key, $locale)) {
149
            $route = "{$prefix}/{$route}";
150
        }
151
152
        return $route;
153
    }
154
}
155