1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Thinktomorrow\Locale; |
4
|
|
|
|
5
|
|
|
use Illuminate\Contracts\Routing\UrlGenerator; |
6
|
|
|
use Thinktomorrow\Locale\Parsers\RouteParser; |
7
|
|
|
use Thinktomorrow\Locale\Parsers\UrlParser; |
8
|
|
|
|
9
|
|
|
class LocaleUrl |
10
|
|
|
{ |
11
|
|
|
/** |
12
|
|
|
* @var Locale |
13
|
|
|
*/ |
14
|
|
|
private $locale; |
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* @var UrlParser |
18
|
|
|
*/ |
19
|
|
|
private $urlparser; |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* @var null|string |
23
|
|
|
*/ |
24
|
|
|
private $placeholder; |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* @var RouteParser |
28
|
|
|
*/ |
29
|
|
|
private $routeparser; |
30
|
|
|
|
31
|
141 |
|
public function __construct(Locale $locale, UrlParser $urlparser, RouteParser $routeparser, $config = []) |
32
|
|
|
{ |
33
|
141 |
|
$this->locale = $locale; |
34
|
141 |
|
$this->urlparser = $urlparser; |
35
|
141 |
|
$this->routeparser = $routeparser; |
36
|
|
|
|
37
|
141 |
|
$this->placeholder = isset($config['placeholder']) ? $config['placeholder'] : 'locale_slug'; |
38
|
141 |
|
} |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* Generate a localized url |
42
|
|
|
* |
43
|
|
|
* @param $url |
44
|
|
|
* @param null $locale |
45
|
|
|
* @param array $parameters |
46
|
|
|
* @param null $secure |
47
|
|
|
* @return mixed |
48
|
|
|
*/ |
49
|
33 |
|
public function to($url, $locale = null, $parameters = [], $secure = null) |
50
|
|
|
{ |
51
|
33 |
|
return $this->urlparser->set($url) |
52
|
33 |
|
->localize($locale) |
53
|
33 |
|
->parameters($parameters) |
54
|
33 |
|
->secure($secure) |
|
|
|
|
55
|
33 |
|
->get(); |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
/** |
59
|
|
|
* Generate a localized route. |
60
|
|
|
* Note that unlike the Illuminate route() no parameter for 'absolute' path is available |
61
|
|
|
* since urls will always be rendered as absolute ones. |
62
|
|
|
* |
63
|
|
|
* @param $name |
64
|
|
|
* @param null $locale |
65
|
|
|
* @param array $parameters |
66
|
|
|
* @return mixed |
67
|
|
|
*/ |
68
|
87 |
|
public function route($name, $locale = null, $parameters = []) |
69
|
|
|
{ |
70
|
|
|
// Locale should be passed as second parameter but in case it is passed as array |
71
|
|
|
// alongside other parameters, we will try to extract it |
72
|
87 |
|
if(!is_array($locale)) $locale = [$this->placeholder => $locale]; |
73
|
|
|
|
74
|
87 |
|
$parameters = array_merge($locale,(array)$parameters); |
75
|
|
|
|
76
|
87 |
|
$locale = $this->extractLocaleFromParameters($parameters); |
77
|
|
|
|
78
|
87 |
|
return $this->routeparser->set($name) |
79
|
87 |
|
->localize($locale) |
|
|
|
|
80
|
87 |
|
->parameters($parameters) |
81
|
87 |
|
->get(); |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
/** |
85
|
|
|
* Isolate locale value from parameters |
86
|
|
|
* |
87
|
|
|
* @param array $parameters |
88
|
|
|
* @return null|string |
89
|
|
|
*/ |
90
|
87 |
|
private function extractLocaleFromParameters(array &$parameters = []) |
91
|
|
|
{ |
92
|
87 |
|
$locale = null; |
|
|
|
|
93
|
|
|
|
94
|
87 |
|
if(!array_key_exists($this->placeholder,$parameters)) |
95
|
87 |
|
{ |
96
|
27 |
|
return $this->locale->get(); |
97
|
|
|
} |
98
|
|
|
|
99
|
69 |
|
$locale = $this->locale->get($parameters[$this->placeholder]); |
100
|
|
|
|
101
|
|
|
// If locale parameter is not a 'real' parameter, we ignore this value and use the current locale instead |
102
|
|
|
// The 'wrong' parameter will be used without key |
103
|
69 |
|
if($locale != $parameters[$this->placeholder]) |
104
|
69 |
|
{ |
105
|
24 |
|
$parameters[] = $parameters[$this->placeholder]; |
106
|
24 |
|
} |
107
|
|
|
|
108
|
69 |
|
unset($parameters[$this->placeholder]); |
109
|
|
|
|
110
|
69 |
|
return $locale; |
111
|
|
|
} |
112
|
|
|
} |
It seems like the type of the argument is not accepted by the function/method which you are calling.
In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.
We suggest to add an explicit type cast like in the following example: