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

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 $languageNamespace;
  32.     /**
  33.      * @var string
  34.      */
  35.     private $structureType Structure::TYPE_PAGE;
  36.     public function __construct(
  37.         $names,
  38.         $languageNamespace,
  39.         $namespace ''
  40.     ) {
  41.         $this->languageNamespace $languageNamespace;
  42.         $this->properties = [];
  43.         foreach ($names as $name) {
  44.             $propertyName = (!empty($namespace) ? $namespace '-' '') . $name;
  45.             $this->properties[$name] = new Property($propertyName, [], 'none'falsetrue);
  46.         }
  47.     }
  48.     /**
  49.      * set language of translated property names.
  50.      *
  51.      * @param string $languageKey
  52.      */
  53.     public function setLanguage($languageKey)
  54.     {
  55.         $this->translatedProperties = [];
  56.         foreach ($this->properties as $key => $property) {
  57.             $this->translatedProperties[$key] = new TranslatedProperty(
  58.                 $property,
  59.                 $languageKey,
  60.                 $this->languageNamespace
  61.             );
  62.         }
  63.     }
  64.     /**
  65.      * returns translated property name.
  66.      *
  67.      * @param string $key
  68.      *
  69.      * @return string
  70.      *
  71.      * @throws \Sulu\Component\Content\Exception\NoSuchPropertyException
  72.      */
  73.     public function getName($key)
  74.     {
  75.         // templates do not translate the template key
  76.         if (Structure::TYPE_SNIPPET === $this->structureType) {
  77.             if ('template' === $key) {
  78.                 return $key;
  79.             }
  80.         }
  81.         if (isset($this->translatedProperties[$key])) {
  82.             return $this->translatedProperties[$key]->getName();
  83.         } else {
  84.             throw new NoSuchPropertyException($key);
  85.         }
  86.     }
  87.     /**
  88.      * Set the structure type.
  89.      *
  90.      * @param string $structureType
  91.      */
  92.     public function setStructureType($structureType)
  93.     {
  94.         $this->structureType $structureType;
  95.     }
  96. }