vendor/sonata-project/intl-bundle/src/Twig/Extension/DateTimeExtension.php line 131

Open in your IDE?
  1. <?php
  2. declare(strict_types=1);
  3. /*
  4.  * This file is part of the Sonata Project package.
  5.  *
  6.  * (c) Thomas Rabaix <thomas.rabaix@sonata-project.org>
  7.  *
  8.  * For the full copyright and license information, please view the LICENSE
  9.  * file that was distributed with this source code.
  10.  */
  11. namespace Sonata\IntlBundle\Twig\Extension;
  12. use Sonata\IntlBundle\Helper\DateTimeFormatterInterface;
  13. use Sonata\IntlBundle\Templating\Helper\DateTimeHelper as TemplatingDateTimeHelper;
  14. use Sonata\IntlBundle\Twig\DateTimeRuntime;
  15. use Twig\Extension\AbstractExtension;
  16. use Twig\TwigFilter;
  17. /**
  18.  * DateTimeExtension extends Twig with localized date/time capabilities.
  19.  *
  20.  * @author Thomas Rabaix <thomas.rabaix@ekino.com>
  21.  */
  22. class DateTimeExtension extends AbstractExtension
  23. {
  24.     /**
  25.      * @var DateTimeFormatterInterface|TemplatingDateTimeHelper
  26.      */
  27.     protected $helper;
  28.     private DateTimeRuntime $dateTimeRuntime;
  29.     /**
  30.      * NEXT_MAJOR: Remove this constructor.
  31.      *
  32.      * @param DateTimeFormatterInterface|TemplatingDateTimeHelper $helper
  33.      */
  34.     public function __construct(object $helper)
  35.     {
  36.         if ($helper instanceof TemplatingDateTimeHelper) {
  37.             @trigger_error(
  38.                 sprintf('The use of %s is deprecated since 2.13, use %s instead.'TemplatingDateTimeHelper::class, DateTimeFormatterInterface::class),
  39.                 \E_USER_DEPRECATED
  40.             );
  41.         } elseif (!$helper instanceof DateTimeFormatterInterface) {
  42.             throw new \TypeError(sprintf('Helper must be an instanceof %s, instanceof %s given'DateTimeFormatterInterface::class, \get_class($helper)));
  43.         }
  44.         $this->helper $helper;
  45.         $this->dateTimeRuntime = new DateTimeRuntime($helper);
  46.     }
  47.     /**
  48.      * @return TwigFilter[]
  49.      */
  50.     public function getFilters()
  51.     {
  52.         return [
  53.             new TwigFilter('format_date', [$this'formatDate'], ['is_safe' => ['html']]), // NEXT_MAJOR: Remove this line
  54.             new TwigFilter('sonata_format_date', [DateTimeRuntime::class, 'formatDate'], ['is_safe' => ['html']]),
  55.             new TwigFilter('format_time', [$this'formatTime'], ['is_safe' => ['html']]), // NEXT_MAJOR: Remove this line
  56.             new TwigFilter('sonata_format_time', [DateTimeRuntime::class, 'formatTime'], ['is_safe' => ['html']]),
  57.             new TwigFilter('format_datetime', [$this'formatDatetime'], ['is_safe' => ['html']]), // NEXT_MAJOR: Remove this line
  58.             new TwigFilter('sonata_format_datetime', [DateTimeRuntime::class, 'formatDatetime'], ['is_safe' => ['html']]),
  59.         ];
  60.     }
  61.     /**
  62.      * NEXT_MAJOR: Remove this method.
  63.      *
  64.      * @param \DateTime|string|int $date
  65.      * @param string|null          $pattern
  66.      * @param string|null          $locale
  67.      * @param string|null          $timezone
  68.      * @param int|null             $dateType
  69.      *
  70.      * @return string
  71.      */
  72.     public function formatDate($date$pattern null$locale null$timezone null$dateType null)
  73.     {
  74.         @trigger_error(
  75.             'The format_date filter is deprecated since 2.12 and will be removed on 3.0. '.
  76.             'Use sonata_format_date instead.',
  77.             \E_USER_DEPRECATED
  78.         );
  79.         return $this->dateTimeRuntime->formatDate($date$pattern$locale$timezone$dateType);
  80.     }
  81.     /**
  82.      * NEXT_MAJOR: Remove this method.
  83.      *
  84.      * @param \DateTime|string|int $time
  85.      * @param string|null          $pattern
  86.      * @param string|null          $locale
  87.      * @param string|null          $timezone
  88.      * @param int|null             $timeType
  89.      *
  90.      * @return string
  91.      */
  92.     public function formatTime($time$pattern null$locale null$timezone null$timeType null)
  93.     {
  94.         @trigger_error(
  95.             'The format_time filter is deprecated since 2.12 and will be removed on 3.0. '.
  96.             'Use sonata_format_time instead.',
  97.             \E_USER_DEPRECATED
  98.         );
  99.         return $this->dateTimeRuntime->formatTime($time$pattern$locale$timezone$timeType);
  100.     }
  101.     /**
  102.      * NEXT_MAJOR: Remove this method.
  103.      *
  104.      * @param \DateTime|string|int $time
  105.      * @param string|null          $pattern
  106.      * @param string|null          $locale
  107.      * @param string|null          $timezone
  108.      * @param int|null             $dateType
  109.      * @param int|null             $timeType
  110.      *
  111.      * @return string
  112.      */
  113.     public function formatDatetime($time$pattern null$locale null$timezone null$dateType null$timeType null)
  114.     {
  115.         @trigger_error(
  116.             'The format_date_time filter is deprecated since 2.12 and will be removed on 3.0. '.
  117.             'Use sonata_format_date_time instead.',
  118.             \E_USER_DEPRECATED
  119.         );
  120.         return $this->dateTimeRuntime->formatDatetime($time$pattern$locale$timezone$timeType);
  121.     }
  122.     /**
  123.      * NEXT_MAJOR: remove this method.
  124.      *
  125.      * @deprecated since sonata-project/intl-bundle 2.8, to be removed in version 3.0.
  126.      *
  127.      * @return string
  128.      */
  129.     public function getName()
  130.     {
  131.         return 'sonata_intl_datetime';
  132.     }
  133. }