-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTranslationUsageDescriptor.php
100 lines (86 loc) · 2.62 KB
/
TranslationUsageDescriptor.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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
<?php
namespace Yokai\SafeDelete\Usage\Descriptor\Adapter\Symfony\Translation;
use Symfony\Contracts\Translation\TranslatorInterface;
use Yokai\SafeDelete\Usage\Descriptor\ObjectDescriptorAwareInterface;
use Yokai\SafeDelete\Usage\Descriptor\ObjectDescriptorAwareTrait;
use Yokai\SafeDelete\Usage\Descriptor\SupportsObjectDescriptorInterface;
use Yokai\SafeDelete\Usage\SupportUsageTrait;
use Yokai\SafeDelete\Usage\Usage;
class TranslationUsageDescriptor implements SupportsObjectDescriptorInterface, ObjectDescriptorAwareInterface
{
use SupportUsageTrait, ObjectDescriptorAwareTrait;
/**
* @var TranslatorInterface
*/
private $translator;
/**
* @var string
*/
private $transId;
/**
* @var string|null
*/
private $transDomain;
/**
* @var string|null
*/
private $subjectObjectClass;
/**
* @var string|null
*/
private $relatedObjectClass;
/**
* @param TranslatorInterface $translator
* @param string $transId
* @param string|null $transDomain
* @param string|null $subjectObjectClass
* @param string|null $relatedObjectClass
*/
public function __construct(
TranslatorInterface $translator,
string $transId,
string $transDomain = null,
string $subjectObjectClass = null,
string $relatedObjectClass = null
) {
$this->translator = $translator;
$this->transId = $transId;
$this->transDomain = $transDomain;
$this->subjectObjectClass = $subjectObjectClass;
$this->relatedObjectClass = $relatedObjectClass;
}
/**
* @inheritdoc
*/
public function supports(object $object): bool
{
return $this->isValidUsage($object, $this->subjectObjectClass, $this->relatedObjectClass);
}
/**
* @inheritdoc
*/
public function describe(object $object): string
{
if (!$object instanceof Usage) {
throw new \BadMethodCallException();
}
return $this->translator->trans(
$this->transId,
[
'%subject%' => $this->safelyDescribe($object->getSubject()),
'%relation%' => $this->safelyDescribe($object->getRelation()),
'%related%' => $this->safelyDescribe($object->getRelated()),
]
,
$this->transDomain
);
}
private function safelyDescribe(object $object): ?string
{
try {
return $this->objectDescriptor->describe($object);
} catch (\Exception $exception) {
}
return null;
}
}