vendor/sulu/sulu/src/Sulu/Component/Content/Mapper/Translation/MultipleTranslatedProperties.php line 42

Open in your IDE?
  1. <?php
  2. /*
  3. * This file is part of Sulu.
  4. *
  5. * (c) Sulu GmbH
  6. *
  7. * This source file is subject to the MIT license that is bundled
  8. * with this source code in the file LICENSE.
  9. */
  10. namespace Sulu\Component\Content\Mapper\Translation;
  11. use Sulu\Component\Content\Compat\Property;
  12. use Sulu\Component\Content\Compat\PropertyInterface;
  13. use Sulu\Component\Content\Compat\Structure;
  14. use Sulu\Component\Content\Exception\NoSuchPropertyException;
  15. /**
  16. * enables to translate multiple properties.
  17. */
  18. class MultipleTranslatedProperties
  19. {
  20. /**
  21. * @var PropertyInterface[]
  22. */
  23. private $properties = [];
  24. /**
  25. * @var PropertyInterface[]
  26. */
  27. private $translatedProperties;
  28. /**
  29. * @var string
  30. */
  31. private $structureType = Structure::TYPE_PAGE;
  32. /**
  33. * @param string $languageNamespace
  34. */
  35. public function __construct(
  36. $names,
  37. private $languageNamespace,
  38. $namespace = ''
  39. ) {
  40. $this->properties = [];
  41. foreach ($names as $name) {
  42. $propertyName = (!empty($namespace) ? $namespace . '-' : '') . $name;
  43. $this->properties[$name] = new Property($propertyName, [], 'none', false, true);
  44. }
  45. }
  46. /**
  47. * set language of translated property names.
  48. *
  49. * @param string $languageKey
  50. */
  51. public function setLanguage($languageKey)
  52. {
  53. $this->translatedProperties = [];
  54. foreach ($this->properties as $key => $property) {
  55. $this->translatedProperties[$key] = new TranslatedProperty(
  56. $property,
  57. $languageKey,
  58. $this->languageNamespace
  59. );
  60. }
  61. }
  62. /**
  63. * returns translated property name.
  64. *
  65. * @param string $key
  66. *
  67. * @return string
  68. *
  69. * @throws NoSuchPropertyException
  70. */
  71. public function getName($key)
  72. {
  73. // templates do not translate the template key
  74. if (Structure::TYPE_SNIPPET === $this->structureType) {
  75. if ('template' === $key) {
  76. return $key;
  77. }
  78. }
  79. if (isset($this->translatedProperties[$key])) {
  80. return $this->translatedProperties[$key]->getName();
  81. } else {
  82. throw new NoSuchPropertyException($key);
  83. }
  84. }
  85. /**
  86. * Set the structure type.
  87. *
  88. * @param string $structureType
  89. */
  90. public function setStructureType($structureType)
  91. {
  92. $this->structureType = $structureType;
  93. }
  94. }