|
| 1 | +<?php |
| 2 | +/** |
| 3 | + * DISCLAIMER |
| 4 | + * |
| 5 | + * Do not edit or add to this file if you wish to upgrade Gally to newer versions in the future. |
| 6 | + * |
| 7 | + * @package Gally |
| 8 | + * @author Stephan Hochdörfer <S.Hochdoerfer@bitexpert.de>, Gally Team <elasticsuite@smile.fr> |
| 9 | + * @copyright 2022-present Smile |
| 10 | + * @license Open Software License v. 3.0 (OSL-3.0) |
| 11 | + */ |
| 12 | + |
| 13 | +declare(strict_types=1); |
| 14 | + |
| 15 | +namespace Gally\SyliusPlugin\Indexer\Provider; |
| 16 | + |
| 17 | +use Doctrine\Common\Collections\Collection; |
| 18 | +use Gally\Sdk\Entity\Label; |
| 19 | +use Gally\Sdk\Entity\LocalizedCatalog; |
| 20 | +use Gally\Sdk\Entity\SourceField; |
| 21 | +use Gally\Sdk\Entity\SourceFieldOption; |
| 22 | +use Sylius\Component\Product\Model\ProductOptionValueTranslation; |
| 23 | + |
| 24 | +/** |
| 25 | + * Shared source field option building logic for the product attribute and product option providers. |
| 26 | + */ |
| 27 | +abstract class AbstractSourceFieldOptionProvider implements ProviderInterface |
| 28 | +{ |
| 29 | + /** @var LocalizedCatalog[] */ |
| 30 | + private array $localizedCatalogs = []; |
| 31 | + |
| 32 | + public function __construct(protected CatalogProvider $catalogProvider) |
| 33 | + { |
| 34 | + foreach ($this->catalogProvider->provide() as $localizedCatalog) { |
| 35 | + $this->localizedCatalogs[] = $localizedCatalog; |
| 36 | + } |
| 37 | + } |
| 38 | + |
| 39 | + public function getEntity(): string |
| 40 | + { |
| 41 | + return 'sourceFieldOption'; |
| 42 | + } |
| 43 | + |
| 44 | + /** |
| 45 | + * @param Collection<int, ProductOptionValueTranslation>|list<array<string, string>> $translations |
| 46 | + */ |
| 47 | + public function buildSourceFieldOption( |
| 48 | + SourceField $sourceField, |
| 49 | + string $code, |
| 50 | + string $defaultLabel, |
| 51 | + Collection|array $translations, |
| 52 | + int $position, |
| 53 | + ): SourceFieldOption { |
| 54 | + /** @var Label[] $labels */ |
| 55 | + $labels = $this->getLabels($translations, $defaultLabel); |
| 56 | + |
| 57 | + return new SourceFieldOption( |
| 58 | + $sourceField, |
| 59 | + $code, |
| 60 | + $position, |
| 61 | + $defaultLabel, |
| 62 | + $labels, |
| 63 | + ); |
| 64 | + } |
| 65 | + |
| 66 | + /** |
| 67 | + * @param Collection<int, ProductOptionValueTranslation>|list<array<string, string>> $translations |
| 68 | + */ |
| 69 | + protected function getLabels(Collection|array $translations, string $defaultLabel): array |
| 70 | + { |
| 71 | + $labelsByLocal = []; |
| 72 | + foreach ($translations as $translation) { |
| 73 | + $locale = str_replace( |
| 74 | + '-', |
| 75 | + '_', |
| 76 | + $translation instanceof ProductOptionValueTranslation |
| 77 | + ? (string) $translation->getLocale() |
| 78 | + : $translation['locale'] |
| 79 | + ); |
| 80 | + $labelsByLocal[$locale] = $translation instanceof ProductOptionValueTranslation |
| 81 | + ? $translation->getValue() |
| 82 | + : $translation['translation']; |
| 83 | + } |
| 84 | + |
| 85 | + $labels = []; |
| 86 | + foreach ($this->localizedCatalogs as $localizedCatalog) { |
| 87 | + $label = $labelsByLocal[$localizedCatalog->getLocale()] ?? null; |
| 88 | + if (null !== $label && '' !== $label && $label !== $defaultLabel) { |
| 89 | + $labels[] = new Label($localizedCatalog, $label); |
| 90 | + } |
| 91 | + } |
| 92 | + |
| 93 | + return $labels; |
| 94 | + } |
| 95 | +} |
0 commit comments