vendor/shopware/core/Framework/DataAbstractionLayer/Write/WriteContext.php line 77

Open in your IDE?
  1. <?php declare(strict_types=1);
  2. namespace Shopware\Core\Framework\DataAbstractionLayer\Write;
  3. use Shopware\Core\Framework\Context;
  4. use Shopware\Core\Framework\Struct\StateAwareTrait;
  5. use Shopware\Core\Framework\Uuid\Uuid;
  6. use Shopware\Core\System\Language\LanguageDefinition;
  7. /**
  8.  * @deprecated tag:v6.5.0 - reason:becomes-internal - Will be internal
  9.  */
  10. class WriteContext
  11. {
  12.     use StateAwareTrait;
  13.     private const SPACER '::';
  14.     /**
  15.      * @var array
  16.      */
  17.     private $paths = [];
  18.     /**
  19.      * @var Context
  20.      */
  21.     private $context;
  22.     /**
  23.      * @var array
  24.      */
  25.     private $languages;
  26.     /**
  27.      * @var string[]|null
  28.      */
  29.     private $languageCodeIdMapping;
  30.     /**
  31.      * @var WriteException
  32.      */
  33.     private $exceptions;
  34.     private function __construct(Context $context)
  35.     {
  36.         $this->context $context;
  37.         $this->exceptions = new WriteException();
  38.     }
  39.     public function setLanguages(array $languages): void
  40.     {
  41.         $this->languages $languages;
  42.         $this->languageCodeIdMapping null;
  43.     }
  44.     public function getLanguages(): array
  45.     {
  46.         if (empty($this->languages)) {
  47.             throw new \RuntimeException('languages not initialized');
  48.         }
  49.         return $this->languages;
  50.     }
  51.     public function getLanguageId(string $identifier): ?string
  52.     {
  53.         if (Uuid::isValid($identifier)) {
  54.             return $this->getLanguages()[mb_strtolower($identifier)]['id'] ?? null;
  55.         }
  56.         $mapping $this->getLanguageCodeToIdMapping();
  57.         return $mapping[mb_strtolower($identifier)] ?? null;
  58.     }
  59.     public static function createFromContext(Context $context): self
  60.     {
  61.         $self = new self($context);
  62.         $self->set(LanguageDefinition::ENTITY_NAME'id'$context->getLanguageId());
  63.         return $self;
  64.     }
  65.     public function set(string $entitystring $propertyNamestring $value): void
  66.     {
  67.         $this->paths[$this->buildPathName($entity$propertyName)] = $value;
  68.     }
  69.     public function get(string $entitystring $propertyName)
  70.     {
  71.         $path $this->buildPathName($entity$propertyName);
  72.         if (!$this->has($entity$propertyName)) {
  73.             throw new \InvalidArgumentException(sprintf('Unable to load %s: %s'$pathprint_r($this->pathstrue)));
  74.         }
  75.         return $this->paths[$path];
  76.     }
  77.     public function has(string $entitystring $propertyName): bool
  78.     {
  79.         $path $this->buildPathName($entity$propertyName);
  80.         return isset($this->paths[$path]);
  81.     }
  82.     public function getContext(): Context
  83.     {
  84.         return $this->context;
  85.     }
  86.     public function resetPaths(): void
  87.     {
  88.         $this->paths = [];
  89.         $this->set(LanguageDefinition::ENTITY_NAME'id'$this->context->getLanguageId());
  90.     }
  91.     public function createWithVersionId(string $versionId): self
  92.     {
  93.         return self::createFromContext($this->getContext()->createWithVersionId($versionId));
  94.     }
  95.     public function getExceptions(): WriteException
  96.     {
  97.         return $this->exceptions;
  98.     }
  99.     public function scope(string $scope, callable $callback): void
  100.     {
  101.         $originalContext $this->context;
  102.         $this->context->scope($scope, function (Context $context) use ($callback$originalContext): void {
  103.             $this->context $context;
  104.             $callback($this);
  105.             $this->context $originalContext;
  106.         });
  107.     }
  108.     public function resetExceptions(): void
  109.     {
  110.         $this->exceptions = new WriteException();
  111.     }
  112.     private function getLanguageCodeToIdMapping(): array
  113.     {
  114.         if ($this->languageCodeIdMapping !== null) {
  115.             return $this->languageCodeIdMapping;
  116.         }
  117.         $mapping = [];
  118.         $languages $this->getLanguages();
  119.         foreach ($languages as $language) {
  120.             if (!$language['code']) {
  121.                 continue;
  122.             }
  123.             $mapping[mb_strtolower($language['code'])] = $language['id'];
  124.         }
  125.         return $this->languageCodeIdMapping $mapping;
  126.     }
  127.     private function buildPathName(string $classNamestring $propertyName): string
  128.     {
  129.         return $className self::SPACER $propertyName;
  130.     }
  131. }