TranslationProxy.php 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  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. private $translatable;
  15. private $properties = array();
  16. private $class;
  17. private $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 translated value for specific property.
  87. *
  88. * @param string $property property name
  89. *
  90. * @return mixed
  91. */
  92. public function getTranslatedValue($property)
  93. {
  94. return $this->findOrCreateTranslationForProperty($property, $this->locale)->getValue();
  95. }
  96. /**
  97. * Sets translated value for specific property.
  98. *
  99. * @param string $property property name
  100. * @param string $value value
  101. */
  102. public function setTranslatedValue($property, $value)
  103. {
  104. $this->findOrCreateTranslationForProperty($property, $this->locale)->setValue($value);
  105. }
  106. /**
  107. * Finds existing or creates new translation for specified property
  108. *
  109. * @param string $property object property name
  110. * @param string $locale locale name
  111. *
  112. * @return Translation
  113. */
  114. private function findOrCreateTranslationForProperty($property, $locale)
  115. {
  116. foreach ($this->coll as $translation) {
  117. if ($locale === $translation->getLocale() && $property === $translation->getProperty()) {
  118. return $translation;
  119. }
  120. }
  121. $translation = new $this->class;
  122. $translation->setTranslatable($this->translatable);
  123. $translation->setProperty($property);
  124. $translation->setLocale($locale);
  125. $this->coll->add($translation);
  126. return $translation;
  127. }
  128. }