vendor/ibexa/user/src/lib/Form/ChoiceList/Loader/AvailableLocaleChoiceLoader.php line 57

Open in your IDE?
  1. <?php
  2. /**
  3. * @copyright Copyright (C) Ibexa AS. All rights reserved.
  4. * @license For full copyright and license information view LICENSE file distributed with this source code.
  5. */
  6. declare(strict_types=1);
  7. namespace Ibexa\User\Form\ChoiceList\Loader;
  8. use Ibexa\Contracts\Core\SiteAccess\ConfigResolverInterface;
  9. use Symfony\Component\Form\ChoiceList\ArrayChoiceList;
  10. use Symfony\Component\Form\ChoiceList\Loader\ChoiceLoaderInterface;
  11. use Symfony\Component\Intl\Locales;
  12. use Symfony\Component\Validator\Constraints\Locale;
  13. use Symfony\Component\Validator\Validator\ValidatorInterface;
  14. class AvailableLocaleChoiceLoader implements ChoiceLoaderInterface
  15. {
  16. // Acholi dialect is used by In-Context translation
  17. // and should not be present on the list of available translations.
  18. private const EXCLUDED_TRANSLATIONS = ['ach-UG'];
  19. /** @var \Symfony\Component\Validator\Validator\ValidatorInterface */
  20. private $validator;
  21. /** @var \Ibexa\Contracts\Core\SiteAccess\ConfigResolverInterface */
  22. private $configResolver;
  23. /** @var string[] */
  24. private $availableTranslations;
  25. /**
  26. * @param \Symfony\Component\Validator\Validator\ValidatorInterface $validator
  27. * @param \Ibexa\Contracts\Core\SiteAccess\ConfigResolverInterface $configResolver
  28. * @param string[] $availableTranslations
  29. */
  30. public function __construct(
  31. ValidatorInterface $validator,
  32. ConfigResolverInterface $configResolver,
  33. array $availableTranslations
  34. ) {
  35. $this->validator = $validator;
  36. $this->availableTranslations = $availableTranslations;
  37. $this->configResolver = $configResolver;
  38. }
  39. public function getChoiceList(): array
  40. {
  41. $choices = [];
  42. $additionalTranslations = $this->configResolver->getParameter('user_preferences.additional_translations');
  43. $availableLocales = array_unique(array_merge($this->availableTranslations, $additionalTranslations));
  44. $locales = array_diff($availableLocales, self::EXCLUDED_TRANSLATIONS);
  45. foreach ($locales as $locale) {
  46. if (0 === $this->validator->validate($locale, new Locale())->count()) {
  47. $choices[Locales::getName($locale)] = $locale;
  48. }
  49. }
  50. return $choices;
  51. }
  52. public function loadChoiceList($value = null)
  53. {
  54. return new ArrayChoiceList($this->getChoiceList(), $value);
  55. }
  56. public function loadChoicesForValues(array $values, $value = null)
  57. {
  58. // Optimize
  59. $values = array_filter($values);
  60. if (empty($values)) {
  61. return [];
  62. }
  63. return $this->loadChoiceList($value)->getChoicesForValues($values);
  64. }
  65. public function loadValuesForChoices(array $choices, $value = null)
  66. {
  67. // Optimize
  68. $choices = array_filter($choices);
  69. if (empty($choices)) {
  70. return [];
  71. }
  72. // If no callable is set, choices are the same as values
  73. if (null === $value) {
  74. return $choices;
  75. }
  76. return $this->loadChoiceList($value)->getValuesForChoices($choices);
  77. }
  78. }
  79. class_alias(AvailableLocaleChoiceLoader::class, 'EzSystems\EzPlatformUser\Form\ChoiceList\Loader\AvailableLocaleChoiceLoader');