-
Notifications
You must be signed in to change notification settings - Fork 0
/
ExceptionTranslator.php
70 lines (59 loc) · 1.96 KB
/
ExceptionTranslator.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
<?php
/*
* This file is part of the Klipper package.
*
* (c) François Pluchino <[email protected]>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Klipper\Component\Translation;
use Klipper\Component\Resource\Domain\DomainUtil;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Contracts\Translation\TranslatorInterface;
/**
* Exception translator.
*
* @author François Pluchino <[email protected]>
*/
class ExceptionTranslator implements ExceptionTranslatorInterface
{
protected TranslatorInterface $translator;
protected bool $debug;
/**
* @param TranslatorInterface $translator The translator
* @param bool $debug The debug mode
*/
public function __construct(TranslatorInterface $translator, bool $debug = false)
{
$this->translator = $translator;
$this->debug = $debug;
}
public function trans(string $message): string
{
if (\in_array($message, $this->getAvailableMessage(), true)) {
$id = str_replace([' ', '-', '\''], '_', strtolower($message));
$id = str_replace(['(', ')'], '', $id);
$message = $this->translator->trans($id, [], 'exceptions');
}
return $message;
}
public function transMessage(string $message, array $params = []): string
{
return $this->translator->trans($message, $params, 'exceptions');
}
public function transDomainThrowable(\Throwable $throwable): string
{
return DomainUtil::getThrowableMessage($this->translator, $throwable, $this->debug);
}
protected function getAvailableMessage(): array
{
return [
Response::$statusTexts[400],
Response::$statusTexts[401],
Response::$statusTexts[403],
Response::$statusTexts[404],
Response::$statusTexts[415],
];
}
}