/
ReduceImplementation.php
156 lines (142 loc) · 4.07 KB
/
ReduceImplementation.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
156
<?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;
/**
* Reduce an array to a single value.
*
* //fusionPath items *Collection
* //fusionPath itemReducer the Fusion object which is triggered for each item
*/
class ReduceImplementation extends AbstractFusionObject
{
/**
* The number of rendered nodes, filled only after evaluate() was called.
*
* @var integer
*/
protected $numberOfRenderedNodes;
/**
* The list items that shall be reduced to a single value
*
* @return iterable
*/
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');
}
/**
* @return string
*/
public function getCarryName()
{
return $this->fusionValue('carryName');
}
/**
* @return string
*/
public function getInitialValue()
{
return $this->fusionValue('initialValue');
}
/**
* 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');
}
/**
* Reduce the given items to a single value
*
* @return mixed
* @throws FusionException
*/
public function evaluate()
{
$items = $this->getItems();
$value = $this->getInitialValue();
if ($items === null) {
return $value;
}
$this->numberOfRenderedNodes = 0;
$itemName = $this->getItemName();
if ($itemName === null) {
throw new FusionException('The Reduction needs an itemName to be set.', 1537890155);
}
$carryName = $this->getCarryName();
if ($carryName === null) {
throw new FusionException('The Reduction needs an carryName to be set.', 1537890148);
}
$itemKeyName = $this->getItemKey();
$iterationName = $this->getIterationName();
$collectionTotalCount = count($items);
foreach ($items as $itemKey => $item) {
$context = $this->runtime->getCurrentContext();
$context[$itemName] = $item;
$context[$carryName] = $value;
if ($itemKeyName !== null) {
$context[$itemKeyName] = $itemKey;
}
if ($iterationName !== null) {
$context[$iterationName] = $this->prepareIterationInformation($collectionTotalCount);
}
$this->runtime->pushContextArray($context);
$value = $this->runtime->render($this->path . '/itemReducer');
$this->runtime->popContext();
$this->numberOfRenderedNodes++;
}
return $value;
}
/**
* @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;
}
}