[TASK] Merge submodule linkvalidator into core
[Packages/TYPO3.CMS.git] / typo3 / sysext / fluid / Classes / ViewHelpers / ForViewHelper.php
1 <?php
2 namespace TYPO3\CMS\Fluid\ViewHelpers;
3
4 /* *
5 * This script is backported from the TYPO3 Flow package "TYPO3.Fluid". *
6 * *
7 * It is free software; you can redistribute it and/or modify it under *
8 * the terms of the GNU Lesser General Public License, either version 3 *
9 * of the License, or (at your option) any later version. *
10 * *
11 * The TYPO3 project - inspiring people to share! *
12 * */
13
14 /**
15 * Loop view helper which can be used to interate over array.
16 * Implements what a basic foreach()-PHP-method does.
17 *
18 * = Examples =
19 *
20 * <code title="Simple Loop">
21 * <f:for each="{0:1, 1:2, 2:3, 3:4}" as="foo">{foo}</f:for>
22 * </code>
23 * <output>
24 * 1234
25 * </output>
26 *
27 * <code title="Output array key">
28 * <ul>
29 * <f:for each="{fruit1: 'apple', fruit2: 'pear', fruit3: 'banana', fruit4: 'cherry'}" as="fruit" key="label">
30 * <li>{label}: {fruit}</li>
31 * </f:for>
32 * </ul>
33 * </code>
34 * <output>
35 * <ul>
36 * <li>fruit1: apple</li>
37 * <li>fruit2: pear</li>
38 * <li>fruit3: banana</li>
39 * <li>fruit4: cherry</li>
40 * </ul>
41 * </output>
42 *
43 * <code title="Iteration information">
44 * <ul>
45 * <f:for each="{0:1, 1:2, 2:3, 3:4}" as="foo" iteration="fooIterator">
46 * <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>
47 * </f:for>
48 * </ul>
49 * </code>
50 * <output>
51 * <ul>
52 * <li>Index: 0 Cycle: 1 Total: 4 Odd First</li>
53 * <li>Index: 1 Cycle: 2 Total: 4 Even</li>
54 * <li>Index: 2 Cycle: 3 Total: 4 Odd</li>
55 * <li>Index: 3 Cycle: 4 Total: 4 Even Last</li>
56 * </ul>
57 * </output>
58 *
59 * @api
60 */
61 class ForViewHelper extends \TYPO3\CMS\Fluid\Core\ViewHelper\AbstractViewHelper implements \TYPO3\CMS\Fluid\Core\ViewHelper\Facets\CompilableInterface {
62
63 /**
64 * Iterates through elements of $each and renders child nodes
65 *
66 * @param array $each The array or \TYPO3\CMS\Extbase\Persistence\ObjectStorage to iterated over
67 * @param string $as The name of the iteration variable
68 * @param string $key The name of the variable to store the current array key
69 * @param boolean $reverse If enabled, the iterator will start with the last element and proceed reversely
70 * @param string $iteration The name of the variable to store iteration information (index, cycle, isFirst, isLast, isEven, isOdd)
71 * @return string Rendered string
72 * @api
73 */
74 public function render($each, $as, $key = '', $reverse = FALSE, $iteration = NULL) {
75 return self::renderStatic($this->arguments, $this->buildRenderChildrenClosure(), $this->renderingContext);
76 }
77
78 /**
79 * @param array $arguments
80 * @param \Closure $renderChildrenClosure
81 * @param \TYPO3\CMS\Fluid\Core\Rendering\RenderingContextInterface $renderingContext
82 * @return string
83 * @throws \TYPO3\CMS\Fluid\Core\ViewHelper\Exception
84 */
85 static public function renderStatic(array $arguments, \Closure $renderChildrenClosure, \TYPO3\CMS\Fluid\Core\Rendering\RenderingContextInterface $renderingContext) {
86 $templateVariableContainer = $renderingContext->getTemplateVariableContainer();
87 if ($arguments['each'] === NULL) {
88 return '';
89 }
90 if (is_object($arguments['each']) && !$arguments['each'] instanceof \Traversable) {
91 throw new \TYPO3\CMS\Fluid\Core\ViewHelper\Exception('ForViewHelper only supports arrays and objects implementing \Traversable interface', 1248728393);
92 }
93
94 if ($arguments['reverse'] === TRUE) {
95 // array_reverse only supports arrays
96 if (is_object($arguments['each'])) {
97 $arguments['each'] = iterator_to_array($arguments['each']);
98 }
99 $arguments['each'] = array_reverse($arguments['each']);
100 }
101 $iterationData = array(
102 'index' => 0,
103 'cycle' => 1,
104 'total' => count($arguments['each'])
105 );
106
107 $output = '';
108 foreach ($arguments['each'] as $keyValue => $singleElement) {
109 $templateVariableContainer->add($arguments['as'], $singleElement);
110 if ($arguments['key'] !== '') {
111 $templateVariableContainer->add($arguments['key'], $keyValue);
112 }
113 if ($arguments['iteration'] !== NULL) {
114 $iterationData['isFirst'] = $iterationData['cycle'] === 1;
115 $iterationData['isLast'] = $iterationData['cycle'] === $iterationData['total'];
116 $iterationData['isEven'] = $iterationData['cycle'] % 2 === 0;
117 $iterationData['isOdd'] = !$iterationData['isEven'];
118 $templateVariableContainer->add($arguments['iteration'], $iterationData);
119 $iterationData['index']++;
120 $iterationData['cycle']++;
121 }
122 $output .= $renderChildrenClosure();
123 $templateVariableContainer->remove($arguments['as']);
124 if ($arguments['key'] !== '') {
125 $templateVariableContainer->remove($arguments['key']);
126 }
127 if ($arguments['iteration'] !== NULL) {
128 $templateVariableContainer->remove($arguments['iteration']);
129 }
130 }
131 return $output;
132 }
133 }
134
135 ?>