1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Psr7Middlewares\Middleware; |
4
|
|
|
|
5
|
|
|
use Psr7Middlewares\Middleware; |
6
|
|
|
use Psr7Middlewares\Utils; |
7
|
|
|
use Negotiation\LanguageNegotiator as Negotiator; |
8
|
|
|
use Psr\Http\Message\ServerRequestInterface; |
9
|
|
|
use Psr\Http\Message\ResponseInterface; |
10
|
|
|
|
11
|
|
|
/** |
12
|
|
|
* Middleware returns the client preferred language. |
13
|
|
|
*/ |
14
|
|
|
class LanguageNegotiator |
15
|
|
|
{ |
16
|
|
|
use Utils\NegotiateTrait; |
17
|
|
|
use Utils\BasePathTrait; |
18
|
|
|
use Utils\RedirectTrait; |
19
|
|
|
|
20
|
|
|
const KEY = 'LANGUAGE'; |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* @var array Allowed languages |
24
|
|
|
*/ |
25
|
|
|
private $languages = []; |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* @var bool Use the path to detect the language |
29
|
|
|
*/ |
30
|
|
|
private $usePath = false; |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* Returns the language. |
34
|
|
|
* |
35
|
|
|
* @param ServerRequestInterface $request |
36
|
|
|
* |
37
|
|
|
* @return string|null |
38
|
|
|
*/ |
39
|
|
|
public static function getLanguage(ServerRequestInterface $request) |
40
|
|
|
{ |
41
|
|
|
return Middleware::getAttribute($request, self::KEY); |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* Define de available languages. |
46
|
|
|
* |
47
|
|
|
* @param array $languages |
48
|
|
|
*/ |
49
|
|
|
public function __construct(array $languages) |
50
|
|
|
{ |
51
|
|
|
$this->languages = $languages; |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* Use the base path to detect the current language. |
56
|
|
|
* |
57
|
|
|
* @param bool $usePath |
58
|
|
|
* |
59
|
|
|
* @return self |
60
|
|
|
*/ |
61
|
|
|
public function usePath($usePath = true) |
62
|
|
|
{ |
63
|
|
|
$this->usePath = $usePath; |
64
|
|
|
|
65
|
|
|
return $this; |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
/** |
69
|
|
|
* Execute the middleware. |
70
|
|
|
* |
71
|
|
|
* @param ServerRequestInterface $request |
72
|
|
|
* @param ResponseInterface $response |
73
|
|
|
* @param callable $next |
74
|
|
|
* |
75
|
|
|
* @return ResponseInterface |
76
|
|
|
*/ |
77
|
|
|
public function __invoke(ServerRequestInterface $request, ResponseInterface $response, callable $next) |
78
|
|
|
{ |
79
|
|
|
$language = null; |
80
|
|
|
|
81
|
|
|
//Use path |
82
|
|
|
if ($this->usePath) { |
83
|
|
|
$uri = $request->getUri(); |
84
|
|
|
$path = ltrim($this->getPath($uri->getPath()), '/'); |
85
|
|
|
|
86
|
|
|
$dirs = explode('/', $path, 2); |
87
|
|
|
$first = array_shift($dirs); |
88
|
|
|
|
89
|
|
|
if (!empty($first) && in_array($first, $this->languages, true)) { |
90
|
|
|
$language = $first; |
91
|
|
|
|
92
|
|
|
//remove the language in the path |
93
|
|
|
$request = $request->withUri($uri->withPath('/'.array_shift($dirs))); |
94
|
|
|
} |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
//Use http headers |
98
|
|
|
if ($language === null) { |
99
|
|
|
$language = $this->negotiateHeader($request->getHeaderLine('Accept-Language'), new Negotiator(), $this->languages); |
100
|
|
|
|
101
|
|
|
if (empty($language)) { |
102
|
|
|
$language = isset($this->languages[0]) ? $this->languages[0] : null; |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
//Redirect to a path with the language |
106
|
|
|
if ($this->redirectStatus && $this->usePath) { |
|
|
|
|
107
|
|
|
$path = Utils\Helpers::joinPath($this->basePath, $language, $this->getPath($uri->getPath())); |
|
|
|
|
108
|
|
|
|
109
|
|
|
return self::getRedirectResponse($this->redirectStatus, $uri->withPath($path), $response); |
110
|
|
|
} |
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
$request = Middleware::setAttribute($request, self::KEY, $language); |
114
|
|
|
|
115
|
|
|
return $next($request, $response); |
116
|
|
|
} |
117
|
|
|
} |
118
|
|
|
|
In PHP, under loose comparison (like
==
, or!=
, orswitch
conditions), values of different types might be equal.For
integer
values, zero is a special case, in particular the following results might be unexpected: