ObjectTranslator.php 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. <?php
  2. namespace Gedmo\Translator;
  3. use Doctrine\Common\Collections\Collection;
  4. /**
  5. * TranslationsCollection
  6. *
  7. * @author Konstantin Kudryashov <ever.zet@gmail.com>
  8. * @package Gedmo.Translator
  9. * @link http://www.gediminasm.org
  10. * @license MIT License (http://www.opensource.org/licenses/mit-license.php)
  11. */
  12. class ObjectTranslator
  13. {
  14. private $translatable;
  15. private $properties;
  16. private $class;
  17. private $coll;
  18. private $defaultValues = array();
  19. private $currentLocale;
  20. /**
  21. * Initializes translations collection
  22. *
  23. * @param Object $translatable object to translate
  24. * @param array $properties object properties to translate
  25. * @param string $class translation entity|document class
  26. * @param Collection $coll translations collection
  27. */
  28. public function __construct($translatable, array $properties, $class, Collection $coll)
  29. {
  30. $this->translatable = $translatable;
  31. $this->properties = $properties;
  32. $this->class = $class;
  33. $this->coll = $coll;
  34. $translatableClass = new \ReflectionObject($translatable);
  35. foreach ($properties as $property) {
  36. $translatableProperty = $translatableClass->getProperty($property);
  37. $translatableProperty->setAccessible(true);
  38. $this->defaultValues[$property] = $translatableProperty->getValue($translatable);
  39. }
  40. }
  41. /**
  42. * Change translatable properties of translatable entity|document to localized ones
  43. *
  44. * @param string $locale locale (null === default)
  45. */
  46. public function translate($locale = null)
  47. {
  48. $locale = null !== $locale ? strtolower($locale) : null;
  49. if ($locale === $this->currentLocale) {
  50. return $this->translatable;
  51. }
  52. $translatableClass = new \ReflectionObject($this->translatable);
  53. // iterate over translatable properties
  54. foreach ($this->properties as $property) {
  55. $translatableProperty = $translatableClass->getProperty($property);
  56. $translatableProperty->setAccessible(true);
  57. $value = $translatableProperty->getValue($this->translatable);
  58. // save current locale value
  59. if (null === $this->currentLocale) {
  60. $this->defaultValues[$property] = $value;
  61. } else {
  62. $translation = $this->getTranslationForProperty($property, $this->currentLocale);
  63. $translation->setValue($value);
  64. }
  65. // load new locale value
  66. if (null === $locale) {
  67. $value = $this->defaultValues[$property];
  68. } else {
  69. $translation = $this->getOrCreateTranslationForProperty($property, $locale);
  70. $value = $translation->getValue();
  71. }
  72. $translatableProperty->setValue($this->translatable, $value);
  73. }
  74. $this->currentLocale = $locale;
  75. return $this->translatable;
  76. }
  77. /**
  78. * Finds or creates new translation for specified property
  79. *
  80. * @param string $property object property name
  81. * @param string $locale locale name
  82. *
  83. * @return Translation
  84. */
  85. public function getOrCreateTranslationForProperty($property, $locale)
  86. {
  87. if (!($translation = $this->getTranslationForProperty($property, $locale))) {
  88. $translation = new $this->class;
  89. $translation->setTranslatable($this->translatable);
  90. $translation->setProperty($property);
  91. $translation->setLocale($locale);
  92. $this->coll->add($translation);
  93. }
  94. return $translation;
  95. }
  96. /**
  97. * Finds translation for specified property
  98. *
  99. * @param string $property object property name
  100. * @param string $locale locale name
  101. *
  102. * @return Translation
  103. */
  104. public function getTranslationForProperty($property, $locale)
  105. {
  106. foreach ($this->coll as $translation) {
  107. if ($locale === $translation->getLocale() && $property === $translation->getProperty()) {
  108. return $translation;
  109. }
  110. }
  111. }
  112. }