/
MapImplementation.php
155 lines (132 loc) · 4.22 KB
/
MapImplementation.php
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
<?php
namespace Neos\Fusion\FusionObjects;
/*
* This file is part of the Neos.Fusion package.
*
* (c) Contributors of the Neos Project - www.neos.io
*
* This package is Open Source Software. For the full copyright and license
* information, please view the LICENSE file which was distributed with this
* source code.
*/
use Neos\Fusion\Exception as FusionException;
/**
* Map a collection of items using the itemRenderer
*
* //fusionPath items *Collection
* //fusionPath itemRenderer the Fusion object which is triggered for each item
* //fusionPath keyRenderer the Fusion object which is triggered for each item to render the key in the result collection
*/
class MapImplementation extends AbstractFusionObject
{
/**
* The number of rendered nodes, filled only after evaluate() was called.
*
* @var integer
*/
protected $numberOfRenderedNodes;
/**
* @return array
*/
public function getItems()
{
return $this->fusionValue('items');
}
/**
* @return string
*/
public function getItemName()
{
return $this->fusionValue('itemName');
}
/**
* @return string
*/
public function getItemKey()
{
return $this->fusionValue('itemKey');
}
/**
* If set iteration data (index, cycle, isFirst, isLast) is available in context with the name given.
*
* @return string
*/
public function getIterationName()
{
return $this->fusionValue('iterationName');
}
/**
* Evaluate the collection nodes as array
*
* @return array
* @throws FusionException
*/
public function evaluate()
{
$collection = $this->getItems();
$result = [];
if ($collection === null) {
return $result;
}
$this->numberOfRenderedNodes = 0;
$itemName = $this->getItemName();
if ($itemName === null) {
throw new FusionException('The Collection needs an itemName to be set.', 1344325771);
}
$itemKey = $this->getItemKey();
$iterationName = $this->getIterationName();
$collectionTotalCount = count($collection);
$keyRenderPath = $this->path . '/keyRenderer';
$keyRendererIsAvailable = $this->runtime->canRender($keyRenderPath);
$itemRenderPath = $this->path . '/itemRenderer';
$fallbackRenderPath = $this->path . '/content';
if ($this->runtime->canRender($itemRenderPath) === false && $this->runtime->canRender($fallbackRenderPath)) {
$itemRenderPath = $fallbackRenderPath;
}
foreach ($collection as $collectionKey => $collectionElement) {
$context = $this->runtime->getCurrentContext();
$context[$itemName] = $collectionElement;
if ($itemKey !== null) {
$context[$itemKey] = $collectionKey;
}
if ($iterationName !== null) {
$context[$iterationName] = $this->prepareIterationInformation($collectionTotalCount);
}
$this->runtime->pushContextArray($context);
if ($keyRendererIsAvailable) {
$collectionKey = $this->runtime->render($keyRenderPath);
}
$result[$collectionKey] = $this->runtime->render($itemRenderPath);
$this->runtime->popContext();
$this->numberOfRenderedNodes++;
}
return $result;
}
/**
* @param integer $collectionCount
* @return array
*/
protected function prepareIterationInformation($collectionCount)
{
$iteration = [
'index' => $this->numberOfRenderedNodes,
'cycle' => ($this->numberOfRenderedNodes + 1),
'isFirst' => false,
'isLast' => false,
'isEven' => false,
'isOdd' => false
];
if ($this->numberOfRenderedNodes === 0) {
$iteration['isFirst'] = true;
}
if (($this->numberOfRenderedNodes + 1) === $collectionCount) {
$iteration['isLast'] = true;
}
if (($this->numberOfRenderedNodes + 1) % 2 === 0) {
$iteration['isEven'] = true;
} else {
$iteration['isOdd'] = true;
}
return $iteration;
}
}