vendor/ezsystems/ezplatform-user/src/lib/UserSetting/Setting/ShortDateTimeFormat.php line 20

Open in your IDE?
  1. <?php
  2. /**
  3.  * @copyright Copyright (C) eZ Systems 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 EzSystems\EzPlatformUser\UserSetting\Setting;
  8. use eZ\Publish\Core\MVC\ConfigResolverInterface;
  9. use EzSystems\EzPlatformUser\Form\DataTransformer\DateTimeFormatTransformer;
  10. use EzSystems\EzPlatformUser\Form\Type\UserSettings\ShortDateTimeFormatType;
  11. use EzSystems\EzPlatformUser\UserSetting\Setting\Value\DateTimeFormat;
  12. use EzSystems\EzPlatformUser\UserSetting\DateTimeFormat\FormatterInterface;
  13. use EzSystems\EzPlatformUser\UserSetting\ValueDefinitionInterface;
  14. use Symfony\Component\Form\FormBuilderInterface;
  15. use Symfony\Contracts\Translation\TranslatorInterface;
  16. class ShortDateTimeFormat extends AbstractDateTimeFormat
  17. {
  18.     /** @var \Symfony\Contracts\Translation\TranslatorInterface */
  19.     private $translator;
  20.     /** @var \eZ\Publish\Core\MVC\ConfigResolverInterface */
  21.     private $configResolver;
  22.     /**
  23.      * @param \EzSystems\EzPlatformUser\UserSetting\Setting\DateTimeFormatSerializer $serializer
  24.      * @param \Symfony\Contracts\Translation\TranslatorInterface $translator
  25.      * @param \eZ\Publish\Core\MVC\ConfigResolverInterface $configResolver
  26.      * @param \EzSystems\EzPlatformUser\UserSetting\DateTimeFormat\FormatterInterface $formatter
  27.      */
  28.     public function __construct(
  29.         DateTimeFormatSerializer $serializer,
  30.         TranslatorInterface $translator,
  31.         ConfigResolverInterface $configResolver,
  32.         FormatterInterface $formatter
  33.     ) {
  34.         parent::__construct($serializer$formatter);
  35.         $this->translator $translator;
  36.         $this->configResolver $configResolver;
  37.     }
  38.     /**
  39.      * {@inheritdoc}
  40.      */
  41.     public function getDefaultValue(): string
  42.     {
  43.         $format $this->configResolver->getParameter('user_preferences.short_datetime_format');
  44.         return $this->serializer->serialize(
  45.             new DateTimeFormat($format['date_format'], $format['time_format'])
  46.         );
  47.     }
  48.     /**
  49.      * {@inheritdoc}
  50.      */
  51.     public function mapFieldForm(
  52.         FormBuilderInterface $formBuilder,
  53.         ValueDefinitionInterface $value
  54.     ): FormBuilderInterface {
  55.         $valueForm $formBuilder->create(
  56.             'value',
  57.             ShortDateTimeFormatType::class,
  58.             [
  59.                 'label' => false,
  60.             ]
  61.         );
  62.         $valueForm->addModelTransformer(new DateTimeFormatTransformer($this->serializer));
  63.         return $valueForm;
  64.     }
  65.     /**
  66.      * {@inheritdoc}
  67.      */
  68.     protected function getTranslatedName(): string
  69.     {
  70.         return $this->translator->trans(
  71.             /** @Desc("Short date and time format") */
  72.             'settings.short_datetime_format.value.title',
  73.             [],
  74.             'user_settings'
  75.         );
  76.     }
  77.     /**
  78.      * {@inheritdoc}
  79.      */
  80.     protected function getTranslatedDescription(): string
  81.     {
  82.         return $this->translator->trans(
  83.             /** @Desc("Date and time format") */
  84.             'settings.short_datetime_format.value.description',
  85.             [],
  86.             'user_settings'
  87.         );
  88.     }
  89.     /**
  90.      * @return string[]
  91.      */
  92.     protected function getAllowedTimeFormats(): array
  93.     {
  94.         return $this->configResolver->getParameter('user_preferences.allowed_short_time_formats');
  95.     }
  96.     /**
  97.      * @return string[]
  98.      */
  99.     protected function getAllowedDateFormats(): array
  100.     {
  101.         return $this->configResolver->getParameter('user_preferences.allowed_short_date_formats');
  102.     }
  103. }