TranslationProxy.php 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  1. <?php
  2. namespace Gedmo\Translator;
  3. use Doctrine\Common\Collections\Collection;
  4. /**
  5. * Proxy class for Entity/Document translations.
  6. *
  7. * @author Konstantin Kudryashov <ever.zet@gmail.com>
  8. * @link http://www.gediminasm.org
  9. * @license MIT License (http://www.opensource.org/licenses/mit-license.php)
  10. */
  11. class TranslationProxy
  12. {
  13. protected $locale;
  14. protected $translatable;
  15. protected $properties = array();
  16. protected $class;
  17. protected $coll;
  18. /**
  19. * Initializes translations collection
  20. *
  21. * @param Object $translatable object to translate
  22. * @param string $locale translation name
  23. * @param array $properties object properties to translate
  24. * @param string $class translation entity|document class
  25. * @param Collection $coll translations collection
  26. */
  27. public function __construct($translatable, $locale, array $properties, $class, Collection $coll)
  28. {
  29. $this->translatable = $translatable;
  30. $this->locale = $locale;
  31. $this->properties = $properties;
  32. $this->class = $class;
  33. $this->coll = $coll;
  34. $translationClass = new \ReflectionClass($class);
  35. if (!$translationClass->implementsInterface('Gedmo\Translator\TranslationInterface')) {
  36. throw new \InvalidArgumentException(sprintf(
  37. 'Translation class should implement Gedmo\Translator\TranslationInterface, "%s" given',
  38. $class
  39. ));
  40. }
  41. }
  42. public function __call($method, $arguments)
  43. {
  44. $matches = array();
  45. if (preg_match('/^(set|get)(.*)$/', $method, $matches)) {
  46. $property = strtolower($matches[2]);
  47. if (in_array($property, $this->properties)) {
  48. switch ($matches[1]) {
  49. case 'get':
  50. return $this->getTranslatedValue($property);
  51. case 'set':
  52. if (isset($arguments[0])) {
  53. $this->setTranslatedValue($property, $arguments[0]);
  54. return $this;
  55. }
  56. }
  57. }
  58. }
  59. $return = call_user_func_array(array($this->translatable, $method), $arguments);
  60. if ($this->translatable === $return) {
  61. return $this;
  62. }
  63. return $return;
  64. }
  65. public function __get($property)
  66. {
  67. if (in_array($property, $this->properties)) {
  68. if (method_exists($this, $getter = 'get'.ucfirst($property))) {
  69. return $this->$getter;
  70. }
  71. return $this->getTranslatedValue($property);
  72. }
  73. return $this->translatable->$property;
  74. }
  75. public function __set($property, $value)
  76. {
  77. if (in_array($property, $this->properties)) {
  78. if (method_exists($this, $setter = 'set'.ucfirst($property))) {
  79. return $this->$setter($value);
  80. }
  81. return $this->setTranslatedValue($property, $value);
  82. }
  83. $this->translatable->$property = $value;
  84. }
  85. /**
  86. * Returns locale name for the current translation proxy instance.
  87. *
  88. * @return string
  89. */
  90. public function getProxyLocale()
  91. {
  92. return $this->locale;
  93. }
  94. /**
  95. * Returns translated value for specific property.
  96. *
  97. * @param string $property property name
  98. *
  99. * @return mixed
  100. */
  101. public function getTranslatedValue($property)
  102. {
  103. return $this
  104. ->findOrCreateTranslationForProperty($property, $this->getProxyLocale())
  105. ->getValue();
  106. }
  107. /**
  108. * Sets translated value for specific property.
  109. *
  110. * @param string $property property name
  111. * @param string $value value
  112. */
  113. public function setTranslatedValue($property, $value)
  114. {
  115. $this
  116. ->findOrCreateTranslationForProperty($property, $this->getProxyLocale())
  117. ->setValue($value);
  118. }
  119. /**
  120. * Finds existing or creates new translation for specified property
  121. *
  122. * @param string $property object property name
  123. * @param string $locale locale name
  124. *
  125. * @return Translation
  126. */
  127. private function findOrCreateTranslationForProperty($property, $locale)
  128. {
  129. foreach ($this->coll as $translation) {
  130. if ($locale === $translation->getLocale() && $property === $translation->getProperty()) {
  131. return $translation;
  132. }
  133. }
  134. $translation = new $this->class;
  135. $translation->setTranslatable($this->translatable);
  136. $translation->setProperty($property);
  137. $translation->setLocale($locale);
  138. $this->coll->add($translation);
  139. return $translation;
  140. }
  141. }