AbstractTranslatable.php 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  1. <?php
  2. /*
  3. * This file is part of the Sylius package.
  4. *
  5. * (c) Paweł Jędrzejewski
  6. *
  7. * For the full copyright and license information, please view the LICENSE
  8. * file that was distributed with this source code.
  9. */
  10. namespace Sylius\Component\Translation\Model;
  11. use Doctrine\Common\Collections\ArrayCollection;
  12. /**
  13. * @author Gonzalo Vilaseca <gvilaseca@reiss.co.uk>
  14. */
  15. abstract class AbstractTranslatable implements TranslatableInterface
  16. {
  17. /**
  18. * Translations
  19. *
  20. * @var TranslationInterface[]
  21. */
  22. protected $translations;
  23. /**
  24. * Current locale
  25. *
  26. * @var string
  27. */
  28. protected $currentLocale;
  29. /**
  30. * Cache current translation. Useful in Doctrine 2.4+
  31. *
  32. * @var string
  33. */
  34. protected $currentTranslation;
  35. /**
  36. * Fallback locale
  37. *
  38. * @var string
  39. */
  40. protected $fallbackLocale;
  41. /**
  42. * Constructor.
  43. */
  44. public function __construct()
  45. {
  46. $this->translations = new ArrayCollection();
  47. }
  48. /**
  49. * {@inheritdoc}
  50. */
  51. public function getTranslations()
  52. {
  53. return $this->translations;
  54. }
  55. /**
  56. * {@inheritdoc}
  57. */
  58. public function addTranslation(TranslationInterface $translation)
  59. {
  60. if (!$this->translations->containsKey($translation->getLocale())) {
  61. $this->translations->set($translation->getLocale(), $translation);
  62. $translation->setTranslatable($this);
  63. }
  64. return $this;
  65. }
  66. /**
  67. * {@inheritdoc}
  68. */
  69. public function removeTranslation(TranslationInterface $translation)
  70. {
  71. if ($this->translations->removeElement($translation)) {
  72. $translation->setTranslatable(null);
  73. }
  74. return $this;
  75. }
  76. /**
  77. * @param TranslationInterface $translation
  78. *
  79. * @return bool
  80. */
  81. public function hasTranslation(TranslationInterface $translation)
  82. {
  83. return $this->translations->containsKey($translation->getLocale());
  84. }
  85. /**
  86. * @param string $currentLocale
  87. *
  88. * @return $this
  89. */
  90. public function setCurrentLocale($currentLocale)
  91. {
  92. $this->currentLocale = $currentLocale;
  93. return $this;
  94. }
  95. /**
  96. * @return string
  97. */
  98. public function getCurrentLocale()
  99. {
  100. return $this->currentLocale;
  101. }
  102. /**
  103. * @param TranslationInterface $currentTranslation
  104. *
  105. * @return $this
  106. */
  107. public function setCurrentTranslation(TranslationInterface $currentTranslation)
  108. {
  109. $this->currentTranslation = $currentTranslation;
  110. return $this;
  111. }
  112. /**
  113. * @return TranslationInterface
  114. */
  115. public function getCurrentTranslation()
  116. {
  117. return $this->currentTranslation;
  118. }
  119. /**
  120. * @param string $fallbackLocale
  121. *
  122. * @return $this
  123. */
  124. public function setFallbackLocale($fallbackLocale)
  125. {
  126. $this->fallbackLocale = $fallbackLocale;
  127. return $this;
  128. }
  129. /**
  130. * @return string
  131. */
  132. public function getFallbackLocale()
  133. {
  134. return $this->fallbackLocale;
  135. }
  136. /**
  137. * Translation helper method
  138. *
  139. * @param string $locale
  140. *
  141. * @return TranslationInterface
  142. *
  143. * @throws \RuntimeException
  144. */
  145. public function translate($locale = null)
  146. {
  147. if (null === $locale) {
  148. $locale = $this->currentLocale;
  149. }
  150. if (!$locale) {
  151. throw new \RuntimeException('No locale has been set and currentLocale is empty');
  152. }
  153. if ($this->currentTranslation && $this->currentTranslation->getLocale() === $locale) {
  154. return $this->currentTranslation;
  155. }
  156. // TODO Throw exception? Get default translation?
  157. if (!$translation = $this->translations->get($locale)) {
  158. if (!$fallbackTranslation = $this->translations->get($this->getFallbackLocale())) {
  159. $className = $this->getTranslationEntityClass();
  160. $translation = new $className();
  161. $translation->setLocale($locale);
  162. $this->addTranslation($translation);
  163. } else {
  164. $translation = clone $fallbackTranslation;
  165. }
  166. }
  167. $this->currentTranslation = $translation;
  168. return $translation;
  169. }
  170. /**
  171. * Return translation entity class
  172. *
  173. * @return string
  174. */
  175. abstract protected function getTranslationEntityClass();
  176. }