Completed
Pull Request — master (#452)
by
unknown
02:30 queued 35s
created

FormatUtcDateViewHelper::renderStatic()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 22

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 22
rs 9.568
c 0
b 0
f 0
cc 2
nc 2
nop 3
1
<?php
2
3
declare(strict_types=1);
4
5
namespace HDNET\Calendarize\ViewHelpers\DateTime;
6
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 dateTime 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
        // save configured timezone
30
        $timezone = date_default_timezone_get();
31
        // set timezone to UTC
32
        date_default_timezone_set('UTC');
33
34
        $date = $arguments['date'];
35
        if ($date instanceof \DateTimeInterface) {
36
            $renderChildrenClosure = function () use ($date) {
37
                // Convert date to timestamp, so that it can be reparsed.
38
                return $date->getTimestamp();
39
            };
40
        }
41
42
        $result = parent::renderStatic($arguments, $renderChildrenClosure, $renderingContext);
43
44
        // restore timezone setting
45
        date_default_timezone_set($timezone);
46
47
        return $result;
48
    }
49
}
50