PageRenderTime 41ms CodeModel.GetById 17ms RepoModel.GetById 0ms app.codeStats 0ms

/lib/Gedmo/Translator/TranslationProxy.php

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