-
Notifications
You must be signed in to change notification settings - Fork 107
Expand file tree
/
Copy pathForViewHelper.php
More file actions
148 lines (142 loc) · 5.36 KB
/
Copy pathForViewHelper.php
File metadata and controls
148 lines (142 loc) · 5.36 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
<?php
/*
* This file belongs to the package "TYPO3 Fluid".
* See LICENSE.txt that was shipped with this package.
*/
namespace TYPO3Fluid\Fluid\ViewHelpers;
use TYPO3Fluid\Fluid\Core\Variables\ScopedVariableProvider;
use TYPO3Fluid\Fluid\Core\Variables\StandardVariableProvider;
use TYPO3Fluid\Fluid\Core\ViewHelper\AbstractViewHelper;
use TYPO3Fluid\Fluid\Core\ViewHelper\InvalidArgumentValueException;
/**
* Loop ViewHelper which can be used to iterate over arrays.
* Implements what a basic PHP ``foreach()`` does.
*
* Examples
* ========
*
* Simple Loop
* -----------
*
* ::
*
* <f:for each="{0:1, 1:2, 2:3, 3:4}" as="foo">{foo}</f:for>
*
* Output::
*
* 1234
*
* Output array key
* ----------------
*
* ::
*
* <ul>
* <f:for each="{fruit1: 'apple', fruit2: 'pear', fruit3: 'banana', fruit4: 'cherry'}"
* as="fruit" key="label"
* >
* <li>{label}: {fruit}</li>
* </f:for>
* </ul>
*
* Output::
*
* <ul>
* <li>fruit1: apple</li>
* <li>fruit2: pear</li>
* <li>fruit3: banana</li>
* <li>fruit4: cherry</li>
* </ul>
*
* Iteration information
* ---------------------
*
* ::
*
* <ul>
* <f:for each="{0:1, 1:2, 2:3, 3:4}" as="foo" iteration="fooIterator">
* <li>Index: {fooIterator.index} Cycle: {fooIterator.cycle} Total: {fooIterator.total}{f:if(condition: fooIterator.isEven, then: ' Even')}{f:if(condition: fooIterator.isOdd, then: ' Odd')}{f:if(condition: fooIterator.isFirst, then: ' First')}{f:if(condition: fooIterator.isLast, then: ' Last')}</li>
* </f:for>
* </ul>
*
* Output::
*
* <ul>
* <li>Index: 0 Cycle: 1 Total: 4 Odd First</li>
* <li>Index: 1 Cycle: 2 Total: 4 Even</li>
* <li>Index: 2 Cycle: 3 Total: 4 Odd</li>
* <li>Index: 3 Cycle: 4 Total: 4 Even Last</li>
* </ul>
*
* @api
* @see https://docs.typo3.org/permalink/fluid:typo3fluid-fluid-for
*/
class ForViewHelper extends AbstractViewHelper
{
/**
* @var bool
*/
protected $escapeOutput = false;
public function initializeArguments(): void
{
$this->registerArgument('each', 'array', 'The array or \SplObjectStorage to iterated over', true);
$this->registerArgument('as', 'string', 'The name of the iteration variable', true);
$this->registerArgument('key', 'string', 'Variable to assign array key to');
$this->registerArgument('reverse', 'boolean', 'If true, iterates in reverse', false, false);
$this->registerArgument('iteration', 'string', 'The name of the variable to store iteration information (index, cycle, total, isFirst, isLast, isEven, isOdd)');
}
public function render(): string
{
if (!isset($this->arguments['each'])) {
return '';
}
if (is_object($this->arguments['each']) && !$this->arguments['each'] instanceof \Traversable) {
throw new InvalidArgumentValueException('ForViewHelper only supports arrays and objects implementing \Traversable interface', 1248728393);
}
$isKeyValuePair = false;
if ($this->arguments['reverse'] === true || (isset($this->arguments['iteration'])) && !is_countable($this->arguments['each'])) {
$isKeyValuePair = true;
$traversable = $this->arguments['each'];
$this->arguments['each'] = [];
foreach ($traversable as $key => $singleElement) {
// unpack the traversable, but keep duplicate keys.
$this->arguments['each'][] = [$key, $singleElement];
}
}
if ($this->arguments['reverse'] === true) {
$this->arguments['each'] = array_reverse($this->arguments['each'], true);
}
if (isset($this->arguments['iteration'])) {
$iterationData = [
'index' => 0,
'cycle' => 1,
'total' => count($this->arguments['each']),
];
}
$globalVariableProvider = $this->renderingContext->getVariableProvider();
$localVariableProvider = new StandardVariableProvider();
$this->renderingContext->setVariableProvider(new ScopedVariableProvider($globalVariableProvider, $localVariableProvider));
$output = '';
foreach ($this->arguments['each'] as $keyValue => $singleElement) {
if ($isKeyValuePair === true) {
[$keyValue, $singleElement] = $singleElement;
}
$localVariableProvider->add($this->arguments['as'], $singleElement);
if (isset($this->arguments['key'])) {
$localVariableProvider->add($this->arguments['key'], $keyValue);
}
if (isset($this->arguments['iteration'])) {
$iterationData['isFirst'] = $iterationData['cycle'] === 1;
$iterationData['isLast'] = $iterationData['cycle'] === $iterationData['total'];
$iterationData['isEven'] = $iterationData['cycle'] % 2 === 0;
$iterationData['isOdd'] = !$iterationData['isEven'];
$localVariableProvider->add($this->arguments['iteration'], $iterationData);
$iterationData['index']++;
$iterationData['cycle']++;
}
$output .= $this->renderChildren();
}
$this->renderingContext->setVariableProvider($globalVariableProvider);
return $output;
}
}