1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Pim\Bundle\CustomEntityBundle\Entity; |
4
|
|
|
|
5
|
|
|
use Akeneo\Component\Localization\Model\TranslationInterface; |
6
|
|
|
use Doctrine\Common\Collections\ArrayCollection; |
7
|
|
|
use Akeneo\Component\Localization\Model\TranslatableInterface; |
8
|
|
|
|
9
|
|
|
/** |
10
|
|
|
* @author Antoine Guigan <[email protected]> |
11
|
|
|
* @copyright 2013 Akeneo SAS (http://www.akeneo.com) |
12
|
|
|
* @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0) |
13
|
|
|
*/ |
14
|
|
|
abstract class AbstractTranslatableCustomEntity extends AbstractCustomEntity implements TranslatableInterface |
15
|
|
|
{ |
16
|
|
|
/** |
17
|
|
|
* @var ArrayCollection |
18
|
|
|
*/ |
19
|
|
|
protected $translations; |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* @var string |
23
|
|
|
*/ |
24
|
|
|
protected $locale; |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* Constructor |
28
|
|
|
*/ |
29
|
|
|
public function __construct() |
30
|
|
|
{ |
31
|
|
|
$this->translations = new ArrayCollection(); |
32
|
|
|
} |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* {@inheritdoc} |
36
|
|
|
*/ |
37
|
|
|
public function addTranslation(TranslationInterface $translation) |
38
|
|
|
{ |
39
|
|
|
if (!$this->translations->contains($translation)) { |
40
|
|
|
$this->translations->add($translation); |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
return $this; |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* {@inheritdoc} |
48
|
|
|
*/ |
49
|
|
|
public function getTranslation($locale = null) |
50
|
|
|
{ |
51
|
|
|
$locale = ($locale) ? $locale : $this->locale; |
52
|
|
|
if (!$locale) { |
53
|
|
|
return null; |
54
|
|
|
} |
55
|
|
|
foreach ($this->getTranslations() as $translation) { |
56
|
|
|
if ($translation->getLocale() == $locale) { |
57
|
|
|
return $translation; |
58
|
|
|
} |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
$translationClass = $this->getTranslationFQCN(); |
62
|
|
|
$translation = new $translationClass(); |
63
|
|
|
$translation->setLocale($locale); |
64
|
|
|
$translation->setForeignKey($this); |
65
|
|
|
$this->addTranslation($translation); |
66
|
|
|
|
67
|
|
|
return $translation; |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
/** |
71
|
|
|
* {@inheritdoc} |
72
|
|
|
*/ |
73
|
|
|
public function setLocale($locale) |
74
|
|
|
{ |
75
|
|
|
$this->locale = $locale; |
76
|
|
|
|
77
|
|
|
return $this; |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
/** |
81
|
|
|
* {@inheritdoc} |
82
|
|
|
*/ |
83
|
|
|
public function removeTranslation(TranslationInterface $translation) |
84
|
|
|
{ |
85
|
|
|
$this->translations->removeElement($translation); |
86
|
|
|
|
87
|
|
|
return $this; |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
/** |
91
|
|
|
* {@inheritdoc} |
92
|
|
|
*/ |
93
|
|
|
public function getTranslations() |
94
|
|
|
{ |
95
|
|
|
return $this->translations; |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
/** |
99
|
|
|
* @return string |
100
|
|
|
*/ |
101
|
|
|
public function __toString() |
102
|
|
|
{ |
103
|
|
|
$translation = $this->getTranslation(); |
104
|
|
|
|
105
|
|
|
return ($translation && (string) $translation) |
106
|
|
|
? (string) $translation |
107
|
|
|
: (string) $this->code; |
108
|
|
|
} |
109
|
|
|
} |
110
|
|
|
|