|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/* |
|
4
|
|
|
* This file is part of the Sylius package. |
|
5
|
|
|
* |
|
6
|
|
|
* (c) Paweł Jędrzejewski |
|
7
|
|
|
* |
|
8
|
|
|
* For the full copyright and license information, please view the LICENSE |
|
9
|
|
|
* file that was distributed with this source code. |
|
10
|
|
|
*/ |
|
11
|
|
|
|
|
12
|
|
|
declare(strict_types=1); |
|
13
|
|
|
|
|
14
|
|
|
namespace Sylius\Bundle\LocaleBundle\Templating\Helper; |
|
15
|
|
|
|
|
16
|
|
|
use Sylius\Component\Locale\Context\LocaleContextInterface; |
|
17
|
|
|
use Sylius\Component\Locale\Context\LocaleNotFoundException; |
|
18
|
|
|
use Sylius\Component\Locale\Converter\LocaleConverterInterface; |
|
19
|
|
|
use Symfony\Component\Templating\Helper\Helper; |
|
20
|
|
|
|
|
21
|
|
|
final class LocaleHelper extends Helper implements LocaleHelperInterface |
|
22
|
|
|
{ |
|
23
|
|
|
/**@var LocaleConverterInterface */ |
|
24
|
|
|
private $localeConverter; |
|
25
|
|
|
|
|
26
|
|
|
/** @var LocaleContextInterface|null */ |
|
27
|
|
|
private $localeContext; |
|
28
|
|
|
|
|
29
|
|
|
public function __construct(LocaleConverterInterface $localeConverter, ?LocaleContextInterface $localeContext = null) |
|
30
|
|
|
{ |
|
31
|
|
|
if (null === $localeContext) { |
|
32
|
|
|
@trigger_error('Not passing LocaleContextInterface explicitly as the second argument is deprecated since 1.4 and will be prohibited in 2.0', \E_USER_DEPRECATED); |
|
|
|
|
|
|
33
|
|
|
} |
|
34
|
|
|
|
|
35
|
|
|
$this->localeConverter = $localeConverter; |
|
36
|
|
|
$this->localeContext = $localeContext; |
|
37
|
|
|
} |
|
38
|
|
|
|
|
39
|
|
|
/** |
|
40
|
|
|
* {@inheritdoc} |
|
41
|
|
|
*/ |
|
42
|
|
|
public function convertCodeToName(string $code, ?string $localeCode = null): ?string |
|
43
|
|
|
{ |
|
44
|
|
|
return $this->localeConverter->convertCodeToName($code, $this->getLocaleCode($localeCode)); |
|
45
|
|
|
} |
|
46
|
|
|
|
|
47
|
|
|
/** |
|
48
|
|
|
* {@inheritdoc} |
|
49
|
|
|
*/ |
|
50
|
|
|
public function getName(): string |
|
51
|
|
|
{ |
|
52
|
|
|
return 'sylius_locale'; |
|
53
|
|
|
} |
|
54
|
|
|
|
|
55
|
|
|
private function getLocaleCode(?string $localeCode): ?string |
|
|
|
|
|
|
56
|
|
|
{ |
|
57
|
|
|
if (null !== $localeCode) { |
|
58
|
|
|
return $localeCode; |
|
59
|
|
|
} |
|
60
|
|
|
|
|
61
|
|
|
if (null === $this->localeContext) { |
|
62
|
|
|
return null; |
|
63
|
|
|
} |
|
64
|
|
|
|
|
65
|
|
|
try { |
|
66
|
|
|
return $this->localeContext->getLocaleCode(); |
|
67
|
|
|
} catch (LocaleNotFoundException $exception) { |
|
68
|
|
|
return null; |
|
69
|
|
|
} |
|
70
|
|
|
} |
|
71
|
|
|
} |
|
72
|
|
|
|
If you suppress an error, we recommend checking for the error condition explicitly: