forked from TYPO3/Fluid
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGroupedForViewHelper.php
More file actions
178 lines (167 loc) · 6 KB
/
GroupedForViewHelper.php
File metadata and controls
178 lines (167 loc) · 6 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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
<?php
namespace TYPO3Fluid\Fluid\ViewHelpers;
/*
* This file belongs to the package "TYPO3 Fluid".
* See LICENSE.txt that was shipped with this package.
*/
use TYPO3Fluid\Fluid\Core\Rendering\RenderingContextInterface;
use TYPO3Fluid\Fluid\Core\Variables\VariableExtractor;
use TYPO3Fluid\Fluid\Core\ViewHelper;
use TYPO3Fluid\Fluid\Core\ViewHelper\AbstractViewHelper;
use TYPO3Fluid\Fluid\Core\ViewHelper\Traits\CompileWithRenderStatic;
/**
* Grouped loop ViewHelper.
* Loops through the specified values.
*
* The groupBy argument also supports property paths.
*
* Using this ViewHelper can be a sign of weak architecture. If you end up
* using it extensively you might want to fine-tune your "view model" (the
* data you assign to the view).
*
* Examples
* ========
*
* Simple
* ------
*
* ::
*
* <f:groupedFor each="{0: {name: 'apple', color: 'green'}, 1: {name: 'cherry', color: 'red'}, 2: {name: 'banana', color: 'yellow'}, 3: {name: 'strawberry', color: 'red'}}"
* as="fruitsOfThisColor" groupBy="color"
* >
* <f:for each="{fruitsOfThisColor}" as="fruit">
* {fruit.name}
* </f:for>
* </f:groupedFor>
*
* Output::
*
* apple cherry strawberry banana
*
* Two dimensional list
* --------------------
*
* ::
*
* <ul>
* <f:groupedFor each="{0: {name: 'apple', color: 'green'}, 1: {name: 'cherry', color: 'red'}, 2: {name: 'banana', color: 'yellow'}, 3: {name: 'strawberry', color: 'red'}}" as="fruitsOfThisColor" groupBy="color" groupKey="color">
* <li>
* {color} fruits:
* <ul>
* <f:for each="{fruitsOfThisColor}" as="fruit" key="label">
* <li>{label}: {fruit.name}</li>
* </f:for>
* </ul>
* </li>
* </f:groupedFor>
* </ul>
*
* Output::
*
* <ul>
* <li>green fruits
* <ul>
* <li>0: apple</li>
* </ul>
* </li>
* <li>red fruits
* <ul>
* <li>1: cherry</li>
* </ul>
* <ul>
* <li>3: strawberry</li>
* </ul>
* </li>
* <li>yellow fruits
* <ul>
* <li>2: banana</li>
* </ul>
* </li>
* </ul>
*
* @api
*/
class GroupedForViewHelper extends AbstractViewHelper
{
use CompileWithRenderStatic;
/**
* @var boolean
*/
protected $escapeOutput = false;
/**
* @return void
*/
public function initializeArguments()
{
parent::initializeArguments();
$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('groupBy', 'string', 'Group by this property', true);
$this->registerArgument('groupKey', 'string', 'The name of the variable to store the current group', false, 'groupKey');
}
/**
* @param array $arguments
* @param \Closure $renderChildrenClosure
* @param RenderingContextInterface $renderingContext
* @return mixed
*/
public static function renderStatic(array $arguments, \Closure $renderChildrenClosure, RenderingContextInterface $renderingContext)
{
$each = $arguments['each'];
$as = $arguments['as'];
$groupBy = $arguments['groupBy'];
$groupKey = $arguments['groupKey'];
$output = '';
if ($each === null) {
return '';
}
if (is_object($each)) {
if (!$each instanceof \Traversable) {
throw new ViewHelper\Exception('GroupedForViewHelper only supports arrays and objects implementing \Traversable interface', 1253108907);
}
$each = iterator_to_array($each);
}
$groups = static::groupElements($each, $groupBy);
$templateVariableContainer = $renderingContext->getVariableProvider();
foreach ($groups['values'] as $currentGroupIndex => $group) {
$templateVariableContainer->add($groupKey, $groups['keys'][$currentGroupIndex]);
$templateVariableContainer->add($as, $group);
$output .= $renderChildrenClosure();
$templateVariableContainer->remove($groupKey);
$templateVariableContainer->remove($as);
}
return $output;
}
/**
* Groups the given array by the specified groupBy property.
*
* @param array $elements The array / traversable object to be grouped
* @param string $groupBy Group by this property
* @return array The grouped array in the form array('keys' => array('key1' => [key1value], 'key2' => [key2value], ...), 'values' => array('key1' => array([key1value] => [element1]), ...), ...)
* @throws ViewHelper\Exception
*/
protected static function groupElements(array $elements, $groupBy)
{
$extractor = new VariableExtractor();
$groups = ['keys' => [], 'values' => []];
foreach ($elements as $key => $value) {
if (is_array($value)) {
$currentGroupIndex = isset($value[$groupBy]) ? $value[$groupBy] : null;
} elseif (is_object($value)) {
$currentGroupIndex = $extractor->getByPath($value, $groupBy);
} else {
throw new ViewHelper\Exception('GroupedForViewHelper only supports multi-dimensional arrays and objects', 1253120365);
}
$currentGroupKeyValue = $currentGroupIndex;
if ($currentGroupIndex instanceof \DateTime) {
$currentGroupIndex = $currentGroupIndex->format(\DateTime::RFC850);
} elseif (is_object($currentGroupIndex)) {
$currentGroupIndex = spl_object_hash($currentGroupIndex);
}
$groups['keys'][$currentGroupIndex] = $currentGroupKeyValue;
$groups['values'][$currentGroupIndex][$key] = $value;
}
return $groups;
}
}