TranslationProxy.php 4.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  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 = lcfirst($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. public function __isset($property)
  86. {
  87. return in_array($property, $this->properties);
  88. }
  89. /**
  90. * Returns locale name for the current translation proxy instance.
  91. *
  92. * @return string
  93. */
  94. public function getProxyLocale()
  95. {
  96. return $this->locale;
  97. }
  98. /**
  99. * Returns translated value for specific property.
  100. *
  101. * @param string $property property name
  102. *
  103. * @return mixed
  104. */
  105. public function getTranslatedValue($property)
  106. {
  107. return $this
  108. ->findOrCreateTranslationForProperty($property, $this->getProxyLocale())
  109. ->getValue();
  110. }
  111. /**
  112. * Sets translated value for specific property.
  113. *
  114. * @param string $property property name
  115. * @param string $value value
  116. */
  117. public function setTranslatedValue($property, $value)
  118. {
  119. $this
  120. ->findOrCreateTranslationForProperty($property, $this->getProxyLocale())
  121. ->setValue($value);
  122. }
  123. /**
  124. * Finds existing or creates new translation for specified property
  125. *
  126. * @param string $property object property name
  127. * @param string $locale locale name
  128. *
  129. * @return Translation
  130. */
  131. private function findOrCreateTranslationForProperty($property, $locale)
  132. {
  133. foreach ($this->coll as $translation) {
  134. if ($locale === $translation->getLocale() && $property === $translation->getProperty()) {
  135. return $translation;
  136. }
  137. }
  138. $translation = new $this->class;
  139. $translation->setTranslatable($this->translatable);
  140. $translation->setProperty($property);
  141. $translation->setLocale($locale);
  142. $this->coll->add($translation);
  143. return $translation;
  144. }
  145. }