vendor/lexik/translation-bundle/Model/TransUnit.php line 15

Open in your IDE?
  1. <?php
  2. namespace Lexik\Bundle\TranslationBundle\Model;
  3. use Doctrine\Common\Collections\Collection;
  4. use Doctrine\Common\Collections\ArrayCollection;
  5. use Lexik\Bundle\TranslationBundle\Manager\TranslationInterface;
  6. use Symfony\Component\Validator\Constraints as Assert;
  7. /**
  8. * This class represent a trans unit which contain translations for a given domain and key.
  9. *
  10. * @author Cédric Girard <c.girard@lexik.fr>
  11. */
  12. abstract class TransUnit
  13. {
  14. /**
  15. * @var int
  16. */
  17. protected $id;
  18. /**
  19. * @var string
  20. *
  21. * @Assert\NotBlank()
  22. */
  23. protected $key;
  24. /**
  25. * @var string
  26. *
  27. * @Assert\NotBlank()
  28. */
  29. protected $domain;
  30. /**
  31. * @var \Doctrine\Common\Collections\Collection
  32. */
  33. protected $translations;
  34. /**
  35. * @var \DateTime
  36. */
  37. protected $createdAt;
  38. /**
  39. * @var \DateTime
  40. */
  41. protected $updatedAt;
  42. /**
  43. * Construct.
  44. */
  45. public function __construct()
  46. {
  47. $this->domain = 'messages';
  48. $this->translations = new ArrayCollection();
  49. }
  50. /**
  51. * Get id
  52. *
  53. * @return integer
  54. */
  55. public function getId()
  56. {
  57. return $this->id;
  58. }
  59. /**
  60. * Set key name
  61. *
  62. * @param string $key
  63. */
  64. public function setKey($key)
  65. {
  66. $this->key = $key;
  67. }
  68. /**
  69. * Get key name
  70. *
  71. * @return string
  72. */
  73. public function getKey()
  74. {
  75. return $this->key;
  76. }
  77. /**
  78. * Set domain
  79. *
  80. * @param string $domain
  81. */
  82. public function setDomain($domain)
  83. {
  84. $this->domain = $domain;
  85. }
  86. /**
  87. * Get domain
  88. *
  89. * @return string
  90. */
  91. public function getDomain()
  92. {
  93. return $this->domain;
  94. }
  95. /**
  96. * Add translations
  97. *
  98. * @param \Lexik\Bundle\TranslationBundle\Model\Translation $translations
  99. */
  100. public function addTranslation(\Lexik\Bundle\TranslationBundle\Model\Translation $translation)
  101. {
  102. $this->translations[] = $translation;
  103. }
  104. /**
  105. * Remove translations
  106. *
  107. * @param \Lexik\Bundle\TranslationBundle\Model\Translation $translations
  108. */
  109. public function removeTranslation(\Lexik\Bundle\TranslationBundle\Model\Translation $translation)
  110. {
  111. $this->translations->removeElement($translation);
  112. }
  113. /**
  114. * Get translations
  115. *
  116. * @return \Doctrine\Common\Collections\Collection
  117. */
  118. public function getTranslations()
  119. {
  120. return $this->translations;
  121. }
  122. /**
  123. * Return true if this object has a translation for the given locale.
  124. *
  125. * @param string $locale
  126. * @return boolean
  127. */
  128. public function hasTranslation($locale)
  129. {
  130. return null !== $this->getTranslation($locale);
  131. }
  132. /**
  133. * Return the content of translation for the given locale.
  134. *
  135. * @param string $locale
  136. * @return \Lexik\Bundle\TranslationBundle\Model\Translation
  137. */
  138. public function getTranslation($locale)
  139. {
  140. foreach ($this->getTranslations() as $translation) {
  141. if ($translation->getLocale() == $locale) {
  142. return $translation;
  143. }
  144. }
  145. return null;
  146. }
  147. /**
  148. * Set translations collection
  149. *
  150. * @param Collection $collection
  151. */
  152. public function setTranslations(Collection $collection)
  153. {
  154. $this->translations = new ArrayCollection();
  155. foreach ($collection as $translation) {
  156. $this->addTranslation($translation);
  157. }
  158. }
  159. /**
  160. * Return transaltions with not blank content.
  161. *
  162. * @return \Doctrine\Common\Collections\Collection
  163. */
  164. public function filterNotBlankTranslations()
  165. {
  166. return $this->getTranslations()->filter(function (TranslationInterface $translation) {
  167. $content = $translation->getContent();
  168. return !empty($content);
  169. });
  170. }
  171. /**
  172. * Get createdAt
  173. *
  174. * @return datetime $createdAt
  175. */
  176. public function getCreatedAt()
  177. {
  178. return $this->createdAt;
  179. }
  180. /**
  181. * Get updatedAt
  182. *
  183. * @return datetime $updatedAt
  184. */
  185. public function getUpdatedAt()
  186. {
  187. return $this->updatedAt;
  188. }
  189. }