1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace Locastic\SymfonyTranslationBundle\Model; |
6
|
|
|
|
7
|
|
|
class Translation implements TranslationInterface |
8
|
|
|
{ |
9
|
|
|
private ?string $domainName = null; |
10
|
|
|
|
11
|
|
|
private ?string $key = null; |
12
|
|
|
|
13
|
|
|
/** @var array|TranslationValueInterface[] */ |
14
|
|
|
private array $values = []; |
15
|
|
|
|
16
|
|
|
public function getDomainName(): ?string |
17
|
|
|
{ |
18
|
|
|
return $this->domainName; |
19
|
|
|
} |
20
|
|
|
|
21
|
|
|
public function setDomainName(?string $domainName): void |
22
|
|
|
{ |
23
|
|
|
$this->domainName = $domainName; |
24
|
|
|
} |
25
|
|
|
|
26
|
|
|
public function getKey(): ?string |
27
|
|
|
{ |
28
|
|
|
return $this->key; |
29
|
|
|
} |
30
|
|
|
|
31
|
|
|
public function setKey(?string $key): void |
32
|
|
|
{ |
33
|
|
|
$this->key = $key; |
34
|
|
|
} |
35
|
|
|
|
36
|
|
|
public function getValues(): array |
37
|
|
|
{ |
38
|
|
|
return $this->values; |
39
|
|
|
} |
40
|
|
|
|
41
|
|
|
public function getValuesByTheme(string $themeName): array |
42
|
|
|
{ |
43
|
|
|
return \array_filter($this->getValues(), function (TranslationValueInterface $translationValue) use ($themeName): bool { |
44
|
|
|
return $themeName === $translationValue->getTheme(); |
45
|
|
|
}); |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
public function getKeyByLocaleAndTheme(string $localeCode, string $themeName): ?int |
49
|
|
|
{ |
50
|
|
|
foreach ($this->getValues() as $key => $value) { |
51
|
|
|
if ($value->getLocaleCode() === $localeCode && $value->getTheme() === $themeName) { |
52
|
|
|
return $key; |
53
|
|
|
} |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
return null; |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
public function hasLocaleAndTheme(string $localeCode, string $themeName): bool |
60
|
|
|
{ |
61
|
|
|
return null !== $this->getKeyByLocaleAndTheme($localeCode, $themeName); |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
public function hasValue(TranslationValueInterface $translationValue): bool |
65
|
|
|
{ |
66
|
|
|
return $this->hasLocaleAndTheme($translationValue->getLocaleCode(), $translationValue->getTheme()) !== null; |
|
|
|
|
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
public function addValue(TranslationValueInterface $translationValue): void |
70
|
|
|
{ |
71
|
|
|
if (!$this->hasLocaleAndTheme($translationValue->getLocaleCode(), $translationValue->getTheme())) { |
|
|
|
|
72
|
|
|
$this->values[] = $translationValue; |
73
|
|
|
$translationValue->setTranslation($this); |
74
|
|
|
} |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
public function removeValue(TranslationValueInterface $translationValue): void |
78
|
|
|
{ |
79
|
|
|
if (null !== $key = $this->getKeyByLocaleAndTheme($translationValue->getLocaleCode(), $translationValue->getTheme())) { |
|
|
|
|
80
|
|
|
unset($this->values[$key]); |
81
|
|
|
$translationValue->setTranslation(null); |
82
|
|
|
} |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
public function getValueByLocaleAndTheme(string $localeCode, string $themeName): ?string |
86
|
|
|
{ |
87
|
|
|
if (null !== $key = $this->getKeyByLocaleAndTheme($localeCode, $themeName)) { |
88
|
|
|
return $this->values[$key]; |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
return null; |
92
|
|
|
} |
93
|
|
|
} |
94
|
|
|
|