Completed
Pull Request — master (#253)
by Pascale
03:33
created

FormatUtcDateViewHelper   A

Complexity

Total Complexity 2

Size/Duplication

Total Lines 29
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Importance

Changes 0
Metric Value
wmc 2
lcom 1
cbo 1
dl 0
loc 29
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A initializeArguments() 0 6 1
A render() 0 14 1
1
<?php
2
3
/**
4
 * Provide strftime function in UTC context.
5
 */
6
declare(strict_types=1);
7
8
namespace HDNET\Calendarize\ViewHelpers\DateTime;
9
10
use HDNET\Calendarize\ViewHelpers\AbstractViewHelper;
11
12
/**
13
 * Check if the given Index is on the given day.
14
 */
15
class FormatUtcDateViewHelper extends AbstractViewHelper
16
{
17
    public function initializeArguments()
18
    {
19
        parent::initializeArguments();
20
        $this->registerArgument('date', \DateTimeInterface::class, 'DateTimeInterface Object to format', true);
21
        $this->registerArgument('format', 'string', 'format passed to strftime', false, '');
22
    }
23
24
    /**
25
     * Format dateTime using strftime() with UTC timezone.
26
     *
27
     * @return string
28
     */
29
    public function render()
30
    {
31
        // save configured timezone
32
        $timezone = \date_default_timezone_get();
33
        // set timezone to UTC
34
        \date_default_timezone_set('UTC');
35
36
        $result = \strftime($this->arguments['format'], (int) $this->arguments['date']->format('U'));
37
38
        // restore timezone setting
39
        \date_default_timezone_set($timezone);
40
41
        return $result;
42
    }
43
}
44