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

AbstractLoopViewHelper::initializeArguments()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 6
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 0
1
<?php
2
3
/**
4
 * Abstraction for loop view helper.
5
 */
6
declare(strict_types=1);
7
8
namespace HDNET\Calendarize\ViewHelpers\Loop;
9
10
use HDNET\Calendarize\ViewHelpers\AbstractViewHelper;
11
12
/**
13
 * Abstraction for loop view helper.
14
 */
15
abstract class AbstractLoopViewHelper extends AbstractViewHelper
16
{
17
    /**
18
     * Specifies whether the escaping interceptors should be disabled or enabled for the result of renderChildren() calls within this ViewHelper.
19
     *
20
     * @see isChildrenEscapingEnabled()
21
     *
22
     * Note: If this is NULL the value of $this->escapingInterceptorEnabled is considered for backwards compatibility
23
     *
24
     * @var bool
25
     *
26
     * @api
27
     */
28
    protected $escapeChildren = false;
29
30
    /**
31
     * Specifies whether the escaping interceptors should be disabled or enabled for the render-result of this ViewHelper.
32
     *
33
     * @see isOutputEscapingEnabled()
34
     *
35
     * @var bool
36
     *
37
     * @api
38
     */
39
    protected $escapeOutput = false;
40
41
    public function initializeArguments()
42
    {
43
        parent::initializeArguments();
44
        $this->registerArgument('date', \DateTimeInterface::class, 'DateTimeInterface Object', true);
45
        $this->registerArgument('iteration', 'string', 'Iterator', true);
46
    }
47
48
    /**
49
     * Render the element.
50
     *
51
     * @throws \TYPO3\CMS\Fluid\Core\ViewHelper\Exception
52
     *
53
     * @return string
54
     */
55
    public function render()
56
    {
57
        $variableContainer = $this->renderingContext->getVariableProvider();
58
59
        // clone: take care that the getItems method do not manipulate the original
60
        $items = $this->getItems(clone $this->arguments['date']);
61
62
        $iterationData = [
63
            'index' => 0,
64
            'cycle' => 1,
65
            'total' => \count($items),
66
        ];
67
68
        $output = '';
69
        foreach ($items as $item) {
70
            $iterationData['isFirst'] = 1 === $iterationData['cycle'];
71
            $iterationData['isLast'] = $iterationData['cycle'] === $iterationData['total'];
72
            $iterationData['isEven'] = $iterationData['cycle'] % 2 === 0;
73
            $iterationData['isOdd'] = !$iterationData['isEven'];
74
            $iterationData['calendar'] = $item;
75
76
            $variableContainer->add($this->arguments['iteration'], $iterationData);
77
78
            $output .= $this->renderChildren();
79
80
            $variableContainer->remove($this->arguments['iteration']);
81
            ++$iterationData['index'];
82
            ++$iterationData['cycle'];
83
        }
84
85
        return $output;
86
    }
87
88
    /**
89
     * Get the items.
90
     *
91
     * @param \DateTime $date
92
     *
93
     * @return array
94
     */
95
    abstract protected function getItems(\DateTime $date);
96
}
97