@@ -176,7 +176,6 @@ protected function filterFor($value)
176
176
{
177
177
if (preg_match_all (self ::LOOP_PATTERN , $ value , $ constructions , PREG_SET_ORDER )) {
178
178
foreach ($ constructions as $ construction ) {
179
-
180
179
if (!$ this ->isValidLoop ($ construction )) {
181
180
return $ value ;
182
181
}
@@ -188,45 +187,7 @@ protected function filterFor($value)
188
187
$ loopItemVariableName = preg_replace ('/\s+/ ' , '' , $ construction ['loopItem ' ]);
189
188
190
189
if (is_array ($ loopData ) || $ loopData instanceof \Traversable) {
191
-
192
- $ loopText = [];
193
- $ loopIndex = 0 ;
194
- $ loopDataObject = new \Magento \Framework \DataObject ();
195
-
196
- foreach ($ loopData as $ loopItemDataObject ) {
197
- // Loop item can be an array or DataObject.
198
- // If loop item is an array, convert it to DataObject
199
- // to have unified interface if the collection
200
- if (!$ loopItemDataObject instanceof \Magento \Framework \DataObject) {
201
- if (!is_array ($ loopItemDataObject )) {
202
- continue ;
203
- }
204
- $ loopItemDataObject = new \Magento \Framework \DataObject ($ loopItemDataObject );
205
- }
206
-
207
- $ loopDataObject ->setData ('index ' , $ loopIndex ++);
208
- $ this ->templateVars ['loop ' ] = $ loopDataObject ;
209
- $ this ->templateVars [$ loopItemVariableName ] = $ loopItemDataObject ;
210
-
211
- if (preg_match_all (
212
- self ::CONSTRUCTION_PATTERN ,
213
- $ loopTextToReplace ,
214
- $ attributes ,
215
- PREG_SET_ORDER
216
- )
217
- ) {
218
-
219
- $ subText = $ loopTextToReplace ;
220
- foreach ($ attributes as $ attribute ) {
221
- $ text = $ this ->getVariable ($ attribute [2 ], '' );
222
- $ subText = str_replace ($ attribute [0 ], $ text , $ subText );
223
- }
224
- $ loopText [] = $ subText ;
225
- }
226
- unset($ this ->templateVars [$ loopItemVariableName ]);
227
-
228
- }
229
- $ replaceText = implode ('' , $ loopText );
190
+ $ replaceText = $ this ->getLoopReplacementText ($ loopData , $ loopItemVariableName , $ loopTextToReplace );
230
191
$ value = str_replace ($ fullTextToReplace , $ replaceText , $ value );
231
192
}
232
193
}
@@ -474,4 +435,53 @@ protected function getStackArgs($stack)
474
435
}
475
436
return $ stack ;
476
437
}
438
+
439
+ /**
440
+ * Process loop text to replace.
441
+ *
442
+ * @param array $loopData
443
+ * @param string $loopItemVariableName
444
+ * @param string $loopTextToReplace
445
+ * @return string
446
+ */
447
+ private function getLoopReplacementText (array $ loopData , $ loopItemVariableName , $ loopTextToReplace )
448
+ {
449
+ $ loopText = [];
450
+ $ loopIndex = 0 ;
451
+ $ loopDataObject = new \Magento \Framework \DataObject ();
452
+
453
+ foreach ($ loopData as $ loopItemDataObject ) {
454
+ // Loop item can be an array or DataObject.
455
+ // If loop item is an array, convert it to DataObject
456
+ // to have unified interface if the collection
457
+ if (!$ loopItemDataObject instanceof \Magento \Framework \DataObject) {
458
+ if (!is_array ($ loopItemDataObject )) {
459
+ continue ;
460
+ }
461
+ $ loopItemDataObject = new \Magento \Framework \DataObject ($ loopItemDataObject );
462
+ }
463
+
464
+ $ loopDataObject ->setData ('index ' , $ loopIndex ++);
465
+ $ this ->templateVars ['loop ' ] = $ loopDataObject ;
466
+ $ this ->templateVars [$ loopItemVariableName ] = $ loopItemDataObject ;
467
+
468
+ if (preg_match_all (
469
+ self ::CONSTRUCTION_PATTERN ,
470
+ $ loopTextToReplace ,
471
+ $ attributes ,
472
+ PREG_SET_ORDER
473
+ )
474
+ ) {
475
+ $ subText = $ loopTextToReplace ;
476
+ foreach ($ attributes as $ attribute ) {
477
+ $ text = $ this ->getVariable ($ attribute [2 ], '' );
478
+ $ subText = str_replace ($ attribute [0 ], $ text , $ subText );
479
+ }
480
+ $ loopText [] = $ subText ;
481
+ }
482
+ unset($ this ->templateVars [$ loopItemVariableName ]);
483
+ }
484
+ $ replaceText = implode ('' , $ loopText );
485
+ return $ replaceText ;
486
+ }
477
487
}
0 commit comments