1
|
|
|
<?php declare(strict_types=1); |
2
|
|
|
|
3
|
|
|
namespace Bone\Server; |
4
|
|
|
|
5
|
|
|
use League\Route\Http\Exception\NotFoundException; |
6
|
|
|
use Locale; |
7
|
|
|
use Psr\Http\Message\ResponseInterface; |
8
|
|
|
use Psr\Http\Message\ServerRequestInterface; |
9
|
|
|
use Psr\Http\Server\MiddlewareInterface; |
10
|
|
|
use Psr\Http\Server\RequestHandlerInterface; |
11
|
|
|
use Zend\I18n\Translator\Translator; |
12
|
|
|
|
13
|
|
|
class I18nHandler |
14
|
|
|
{ |
15
|
|
|
const REGEX_LOCALE = '#^/(?P<locale>[a-z]{2}[-_][a-zA-Z]{2})(?:/|$)#'; |
16
|
|
|
|
17
|
|
|
/** @var Translator$translator */ |
18
|
|
|
private $translator; |
19
|
|
|
|
20
|
|
|
/** @var array $supportedLocales */ |
21
|
|
|
private $supportedLocales; |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* InternationalisationMiddleware constructor. |
25
|
|
|
* @param $helper |
26
|
|
|
* @param string|null $defaultLocale |
|
|
|
|
27
|
|
|
*/ |
28
|
3 |
|
public function __construct(Translator $translator, array $supportedLocales) |
29
|
|
|
{ |
30
|
3 |
|
$this->translator = $translator; |
31
|
3 |
|
$this->supportedLocales = $supportedLocales; |
32
|
3 |
|
} |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* @param ServerRequestInterface $request |
36
|
|
|
* @return ServerRequestInterface |
37
|
|
|
* @throws NotFoundException |
38
|
|
|
*/ |
39
|
2 |
|
public function handleI18n(ServerRequestInterface $request): ServerRequestInterface |
40
|
|
|
{ |
41
|
2 |
|
$uri = $request->getUri(); |
42
|
2 |
|
$path = $uri->getPath(); |
43
|
|
|
|
44
|
2 |
|
if (! preg_match(self::REGEX_LOCALE, $path, $matches)) { |
45
|
|
|
$path = '/' . Locale::getDefault() . $path; |
46
|
|
|
throw new NotFoundException($path); |
47
|
|
|
} |
48
|
|
|
|
49
|
2 |
|
$locale = $matches['locale']; |
50
|
|
|
|
51
|
2 |
|
if (in_array($locale, $this->supportedLocales)) { |
52
|
1 |
|
$locale = Locale::canonicalize($locale); |
53
|
1 |
|
Locale::setDefault($locale); |
54
|
1 |
|
$this->translator->setLocale($locale); |
55
|
1 |
|
$path = substr($path, strlen($locale) + 1); |
56
|
1 |
|
$uri = $uri->withPath($path); |
57
|
1 |
|
$request = $request->withUri($uri); |
58
|
|
|
} |
59
|
|
|
|
60
|
2 |
|
return $request; |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
/** |
64
|
|
|
* @param ServerRequestInterface $request |
65
|
|
|
* @return ServerRequestInterface |
66
|
|
|
*/ |
67
|
1 |
|
public function removeI18n(ServerRequestInterface $request): ServerRequestInterface |
68
|
|
|
{ |
69
|
1 |
|
$uri = $request->getUri(); |
70
|
1 |
|
$path = $uri->getPath(); |
71
|
|
|
|
72
|
1 |
|
if (! preg_match(self::REGEX_LOCALE, $path, $matches)) { |
73
|
|
|
return $request; |
74
|
|
|
} |
75
|
|
|
|
76
|
1 |
|
$locale = $matches['locale']; |
77
|
1 |
|
$path = substr($path, strlen($locale) + 1); |
78
|
1 |
|
$uri = $uri->withPath($path); |
79
|
1 |
|
$request = $request->withUri($uri); |
80
|
|
|
|
81
|
1 |
|
return $request; |
82
|
|
|
} |
83
|
|
|
} |
This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.
Consider the following example. The parameter
$italy
is not defined by the methodfinale(...)
.The most likely cause is that the parameter was removed, but the annotation was not.