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

IndexOnDayViewHelper::initializeArguments()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 8
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 0
1
<?php
2
3
/**
4
 * Check if the given Index is on the given day.
5
 */
6
declare(strict_types=1);
7
8
namespace HDNET\Calendarize\ViewHelpers\DateTime;
9
10
use HDNET\Calendarize\Domain\Model\Index;
11
use HDNET\Calendarize\Utility\DateTimeUtility;
12
use HDNET\Calendarize\Utility\IndexUtility;
13
use HDNET\Calendarize\ViewHelpers\AbstractViewHelper;
14
15
/**
16
 * Check if the given Index is on the given day.
17
 */
18
class IndexOnDayViewHelper extends AbstractViewHelper
19
{
20
    public function initializeArguments()
21
    {
22
        parent::initializeArguments();
23
        $this->registerArgument('day', \DateTimeInterface::class, 'Day to check against the Indices', true);
24
        $this->registerArgument('index', Index::class, 'Index to check against day', false, null);
25
        $this->registerArgument('indices', \Iterator::class, 'Indices to check against day', false, []);
26
        $this->registerArgument('modification', 'string', 'Apply Modifier to the Date', false, '');
27
    }
28
29
    /**
30
     * Check if the index or one of the given indices is on the given day.
31
     *
32
     * @return bool
33
     */
34
    public function render()
35
    {
36
        /** @var \DateTimeInterface $day */
37
        $day = $this->arguments['day'];
38
        /** @var ?Index $index */
0 ignored issues
show
Documentation introduced by
The doc-type ?Index could not be parsed: Unknown type name "?Index" at position 0. (view supported doc-types)

This check marks PHPDoc comments that could not be parsed by our parser. To see which comment annotations we can parse, please refer to our documentation on supported doc-types.

Loading history...
39
        $index = $this->arguments['index'];
40
        /** @var array<Index> $indices */
41
        $indices = $this->arguments['indices'];
42
        /** @var string $modification */
43
        $modification = $this->arguments['modification'];
44
45
        $day = DateTimeUtility::normalizeDateTimeSingle($day->format('d.m.Y'));
46
        $baseDay = clone $day;
47
        if ($modification !== '') {
48
            $baseDay->modify($modification);
49
        }
50
51
        $baseDay->setTime(0, 0, 0);
52
        $startTime = clone $baseDay;
53
        $baseDay->setTime(23, 59, 59);
54
        $endTime = clone $baseDay;
55
56
        if ($index instanceof Index) {
57
            $indices[] = $index;
58
        }
59
        foreach ($indices as $index) {
60
            /** @var $index Index */
61
            if (IndexUtility::isIndexInRange($index, $startTime, $endTime)) {
62
                return true;
63
            }
64
        }
65
66
        return false;
67
    }
68
}
69