1
|
|
|
<?php |
2
|
|
|
declare(strict_types=1); |
3
|
|
|
|
4
|
|
|
namespace Nexendrie\Translation\Resolvers; |
5
|
|
|
|
6
|
|
|
use Nette\Http\Session; |
7
|
|
|
use Nette\Http\SessionSection; |
8
|
|
|
use Nette\Http\RequestFactory; |
9
|
|
|
use Nette\Http\Response; |
10
|
|
|
use Nexendrie\Translation\ISettableLocaleResolver; |
11
|
|
|
|
12
|
|
|
/** |
13
|
|
|
* SessionLocaleResolver |
14
|
|
|
* |
15
|
|
|
* @author Jakub Konečný |
16
|
|
|
* @property string|null $lang |
17
|
|
|
*/ |
18
|
1 |
|
final class SessionLocaleResolver implements ISettableLocaleResolver { |
19
|
|
|
use \Nette\SmartObject; |
20
|
|
|
|
21
|
|
|
private Session $session; |
22
|
|
|
private SessionSection $section; |
23
|
|
|
public string $varName = "lang"; |
24
|
|
|
|
25
|
|
|
public function __construct(Session $session = null) { |
26
|
1 |
|
if($session === null) { |
27
|
1 |
|
$request = (new RequestFactory())->fromGlobals(); |
28
|
1 |
|
$response = new Response(); |
29
|
1 |
|
$session = new Session($request, $response); |
30
|
|
|
} |
31
|
1 |
|
$this->session = $session; |
32
|
1 |
|
$this->section = $session->getSection(get_class($this)); |
33
|
1 |
|
} |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* @deprecated Access the property directly |
37
|
|
|
*/ |
38
|
|
|
public function getLang(): ?string { |
39
|
1 |
|
$lang = (string) $this->section->{$this->varName}; |
40
|
1 |
|
if($lang === '') { |
41
|
1 |
|
return null; |
42
|
|
|
} |
43
|
1 |
|
return $lang; |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* @deprecated Access the property directly |
48
|
|
|
*/ |
49
|
|
|
public function setLang(?string $lang): void { |
50
|
1 |
|
$this->section->{$this->varName} = $lang; |
51
|
1 |
|
} |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* @deprecated Access the property directly |
55
|
|
|
*/ |
56
|
|
|
public function getVarName(): string { |
57
|
|
|
return $this->varName; |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
/** |
61
|
|
|
* @deprecated Access the property directly |
62
|
|
|
*/ |
63
|
|
|
public function setVarName(string $varName): void { |
64
|
|
|
$this->varName = $varName; |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
public function resolve(): ?string { |
68
|
1 |
|
return $this->getLang(); |
|
|
|
|
69
|
|
|
} |
70
|
|
|
} |
71
|
|
|
?> |
This function has been deprecated. The supplier of the function has supplied an explanatory message.
The explanatory message should give you some clue as to whether and when the function will be removed and what other function to use instead.