|
| 1 | +<?php |
| 2 | +/** |
| 3 | + * For the full copyright and license information, please view the |
| 4 | + * docs/licenses/LICENSE.txt file that was distributed with this source code. |
| 5 | + */ |
| 6 | + |
| 7 | +declare(strict_types=1); |
| 8 | + |
| 9 | +namespace PrestaShop\PrestaShop\Core\Language; |
| 10 | + |
| 11 | +use PrestaShop\PrestaShop\Adapter\Language\LanguageDataProvider; |
| 12 | +use PrestaShop\PrestaShop\Core\ConfigurationInterface; |
| 13 | + |
| 14 | +/** |
| 15 | + * Fills empty/missing localized values with the default language value, for every active language. |
| 16 | + * |
| 17 | + * This reproduces the legacy back office auto-fill behavior at the CQRS level, so it applies whatever |
| 18 | + * builds the command: a form, an ajax call that only provides the current language, or the Admin API. |
| 19 | + */ |
| 20 | +class LocalizedNamesFiller |
| 21 | +{ |
| 22 | + public function __construct( |
| 23 | + private readonly LanguageDataProvider $languageDataProvider, |
| 24 | + private readonly ConfigurationInterface $configuration, |
| 25 | + ) { |
| 26 | + } |
| 27 | + |
| 28 | + /** |
| 29 | + * Returns the localized values with every active language filled in. |
| 30 | + * |
| 31 | + * Non-empty values from $localizedValues are applied on top of $existingValues, so a partial |
| 32 | + * update (e.g. the ajax call that only sends the current language) keeps the languages it does |
| 33 | + * not touch. Languages that are still empty afterwards are filled with the default value. |
| 34 | + * |
| 35 | + * @param array<int, string> $localizedValues lang-ID-keyed values to apply |
| 36 | + * @param array<int, string> $existingValues lang-ID-keyed values already stored (empty on creation) |
| 37 | + * |
| 38 | + * @return array<int, string> |
| 39 | + */ |
| 40 | + public function fill(array $localizedValues, array $existingValues = []): array |
| 41 | + { |
| 42 | + $filledValues = $existingValues; |
| 43 | + foreach ($localizedValues as $languageId => $value) { |
| 44 | + if (!empty($value)) { |
| 45 | + $filledValues[(int) $languageId] = $value; |
| 46 | + } |
| 47 | + } |
| 48 | + |
| 49 | + $defaultValue = $this->resolveDefaultValue($filledValues); |
| 50 | + if (null === $defaultValue) { |
| 51 | + return $filledValues; |
| 52 | + } |
| 53 | + |
| 54 | + foreach ($this->languageDataProvider->getLanguages(false, false, true) as $languageId) { |
| 55 | + if (empty($filledValues[(int) $languageId])) { |
| 56 | + $filledValues[(int) $languageId] = $defaultValue; |
| 57 | + } |
| 58 | + } |
| 59 | + |
| 60 | + return $filledValues; |
| 61 | + } |
| 62 | + |
| 63 | + /** |
| 64 | + * Returns the value used to fill the empty languages: the default language value when set, |
| 65 | + * otherwise the first non-empty value provided (e.g. the ajax call only sends the current language). |
| 66 | + * |
| 67 | + * @param array<int, string> $localizedValues |
| 68 | + */ |
| 69 | + private function resolveDefaultValue(array $localizedValues): ?string |
| 70 | + { |
| 71 | + $defaultLanguageId = (int) $this->configuration->get('PS_LANG_DEFAULT'); |
| 72 | + if (!empty($localizedValues[$defaultLanguageId])) { |
| 73 | + return $localizedValues[$defaultLanguageId]; |
| 74 | + } |
| 75 | + |
| 76 | + foreach ($localizedValues as $value) { |
| 77 | + if (!empty($value)) { |
| 78 | + return $value; |
| 79 | + } |
| 80 | + } |
| 81 | + |
| 82 | + return null; |
| 83 | + } |
| 84 | +} |
0 commit comments