Completed
Pull Request — master (#452)
by
unknown
01:54
created

FormatUtcDateViewHelper::renderStatic()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 26

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 26
rs 9.504
c 0
b 0
f 0
cc 3
nc 3
nop 3
1
<?php
2
declare(strict_types=1);
3
4
namespace HDNET\Calendarize\ViewHelpers\DateTime;
5
6
use HDNET\Calendarize\Utility\DateTimeUtility;
7
use TYPO3\CMS\Fluid\ViewHelpers\Format\DateViewHelper;
8
use TYPO3Fluid\Fluid\Core\Rendering\RenderingContextInterface;
9
use TYPO3Fluid\Fluid\Core\ViewHelper\Exception;
10
11
/**
12
 * Formats the date to UTC.
13
 */
14
class FormatUtcDateViewHelper extends DateViewHelper
15
{
16
    /**
17
     * Format the date to the UTC timezone.
18
     *
19
     * @param array                     $arguments
20
     * @param \Closure                  $renderChildrenClosure
21
     * @param RenderingContextInterface $renderingContext
22
     *
23
     * @return string
24
     *
25
     * @throws Exception
26
     */
27
    public static function renderStatic(array $arguments, \Closure $renderChildrenClosure, RenderingContextInterface $renderingContext)
28
    {
29
        if ($arguments['date'] instanceof \DateTimeImmutable) {
30
            // Workaround, because the date cannot be modified and passed to the parent.
31
            // The parent gets the date from $renderChildrenClosure
32
            $immutableDate = $arguments['date']->setTimezone(DateTimeUtility::getUtcTimeZone());
33
34
            return $immutableDate->format($arguments['format'] ?? 'Y-m-d');
35
        }
36
37
        // save configured timezone
38
        $timezone = date_default_timezone_get();
39
        // set timezone to UTC
40
        date_default_timezone_set('UTC');
41
42
        if ($arguments['date'] instanceof \DateTime) {
43
            $arguments['date']->setTimezone(DateTimeUtility::getUtcTimeZone());
44
        }
45
46
        $result = parent::renderStatic($arguments, $renderChildrenClosure, $renderingContext);
47
48
        // restore timezone setting
49
        date_default_timezone_set($timezone);
50
51
        return $result;
52
    }
53
}
54